-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathRuleController.php
More file actions
126 lines (113 loc) · 2.55 KB
/
RuleController.php
File metadata and controls
126 lines (113 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
/**
* ownCloud - Files_antivirus
*
* This file is licensed under the Affero General Public License version 3 or
* later. See the COPYING file.
*
* @author Viktar Dubiniuk <dubiniuk@owncloud.com>
*
* @copyright Viktar Dubiniuk 2015-2018
* @license AGPL-3.0
*/
namespace OCA\Files_Antivirus\Controller;
use \OCP\AppFramework\Controller;
use \OCP\IRequest;
use OCP\AppFramework\Http\JSONResponse;
use \OCA\Files_Antivirus\Db\Rule;
use \OCA\Files_Antivirus\Db\RuleMapper;
class RuleController extends Controller {
private $ruleMapper;
/**
* RuleController constructor.
*
* @param string $appName
* @param IRequest $request
* @param RuleMapper $ruleMapper
*/
public function __construct(
$appName,
IRequest $request,
RuleMapper $ruleMapper
) {
parent::__construct($appName, $request);
$this->ruleMapper = $ruleMapper;
}
/**
* Returns all rules
*
* @return JSONResponse
*/
public function listAll() {
$statuses = $this->ruleMapper->findAll();
return new JSONResponse(['statuses' => $statuses]);
}
/**
* Removes all rules
*
* @return JSONResponse
*/
public function clear() {
$this->ruleMapper->deleteAll();
return new JSONResponse();
}
/**
* Resets a table to initial state
*
* @return JSONResponse
*/
public function reset() {
$this->ruleMapper->deleteAll();
$this->ruleMapper->populate();
return new JSONResponse();
}
/**
* Adds/Updates a rule
*
* @param int $id
* @param int $statusType
* @param string $match
* @param string $description
* @param int $status
*
* @return JSONResponse
*/
public function save($id, $statusType, $match, $description, $status) {
if ($id) {
$rule = $this->ruleMapper->find($id);
} else {
$rule = new Rule();
}
$rule->setStatusType($statusType); // @phpstan-ignore-line
$rule->setDescription($description); // @phpstan-ignore-line
$rule->setStatus($status); // @phpstan-ignore-line
if ($statusType === Rule::RULE_TYPE_CODE) {
$rule->setResult($match); // @phpstan-ignore-line
} else {
$rule->setMatch($match); // @phpstan-ignore-line
}
if ($id) {
$newRule = $this->ruleMapper->update($rule);
} else {
$newRule = $this->ruleMapper->insert($rule);
}
return new JSONResponse($newRule);
}
/**
* Deletes a rule
*
* @param int $id
*
* @return JSONResponse
*/
public function delete($id) {
try {
$rule = $this->ruleMapper->find($id);
$this->ruleMapper->delete($rule);
return new JSONResponse($rule);
} catch (\Exception $e) {
//TODO: Handle
}
return new JSONResponse([]);
}
}