RuleController::save()   A
last analyzed

Complexity

Conditions 4
Paths 8

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 25
ccs 0
cts 20
cp 0
rs 9.52
c 0
b 0
f 0
cc 4
nc 8
nop 5
crap 20
1
<?php
2
/**
3
 * Copyright (c) 2015 Victor Dubiniuk <[email protected]>
4
 * This file is licensed under the Affero General Public License version 3 or
5
 * later.
6
 * See the COPYING-README file.
7
 */
8
9
namespace OCA\Files_Antivirus\Controller;
10
11
use \OCP\AppFramework\Controller;
12
use \OCP\IRequest;
13
use OCP\AppFramework\Http\JSONResponse;
14
15
use \OCA\Files_Antivirus\Db\Rule;
16
use \OCA\Files_Antivirus\Db\RuleMapper;
17
18
class RuleController extends Controller {
19
	
20
	/** @var RuleMapper */
21
	private $ruleMapper;
22
	
23
	public function __construct($appName, IRequest $request, RuleMapper $ruleMapper) {
24
		parent::__construct($appName, $request);
25
		$this->ruleMapper = $ruleMapper;
26
	}
27
	
28
	/**
29
	 * Returns all rules
30
	 * @return JSONResponse
31
	 */
32
	public function listAll() {
33
		$statuses = $this->ruleMapper->findAll();
34
		return new JSONResponse(['statuses' => $statuses]);
35
	}
36
	
37
	/**
38
	 * Removes all rules
39
	 * @return JSONResponse
40
	 */
41
	public function clear() {
42
		$this->ruleMapper->deleteAll();
43
		return new JSONResponse();
44
	}
45
	
46
	/**
47
	 * Resets a table to initial state
48
	 * @return JSONResponse
49
	 */
50
	public function reset() {
51
		$this->ruleMapper->deleteAll();
52
		$this->ruleMapper->populate();
53
		return new JSONResponse();
54
	}
55
	
56
	/**
57
	 * Adds/Updates a rule
58
	 * @param int $id
59
	 * @param int $statusType
60
	 * @param string $match
61
	 * @param string $description
62
	 * @param int $status
63
	 * @return JSONResponse
64
	 */
65
	public function save($id, $statusType, $match, $description, $status) {
66
		if ($id) {
67
			$rule = $this->ruleMapper->find($id);
68
		} else {
69
			$rule = new Rule();
70
		}
71
		
72
		$rule->setStatusType($statusType);
73
		$rule->setDescription($description);
74
		$rule->setStatus($status);
75
		
76
		if ($statusType === Rule::RULE_TYPE_CODE) {
77
			$rule->setResult($match);
78
		} else {
79
			$rule->setMatch($match);
80
		}
81
			
82
		if ($id) {
83
			$newRule = $this->ruleMapper->update($rule);
0 ignored issues
show
Deprecated Code introduced by
The method OCP\AppFramework\Db\Mapper::update() has been deprecated with message: 14.0.0 Move over to QBMapper

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
84
		} else {
85
			$newRule = $this->ruleMapper->insert($rule);
0 ignored issues
show
Deprecated Code introduced by
The method OCP\AppFramework\Db\Mapper::insert() has been deprecated with message: 14.0.0 Move over to QBMapper

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
86
		}
87
		
88
		return new JSONResponse($newRule);
89
	}
90
	
91
	/**
92
	 * Deletes a rule
93
	 * @param int $id
94
	 * @return JSONResponse
95
	 */
96
	public function delete($id) {
97
		try {
98
			$rule = $this->ruleMapper->find($id);
99
			$this->ruleMapper->delete($rule);
0 ignored issues
show
Deprecated Code introduced by
The method OCP\AppFramework\Db\Mapper::delete() has been deprecated with message: 14.0.0 Move over to QBMapper

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
100
		} catch (\Exception $e) {
101
			//TODO: Handle
102
		}
103
		return new JSONResponse($rule);
104
	}
105
}
106