Completed
Push — master ( 5c07ba...e845b9 )
by Victor
14s
created

RuleMapper   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 235
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 94.92%

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 0
dl 0
loc 235
ccs 112
cts 118
cp 0.9492
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A deleteAll() 0 4 1
A find() 0 4 1
A findAll() 0 4 1
A findByResult() 0 4 1
A findAllMatchedByStatus() 0 4 1
B populate() 0 179 2
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\Db;
10
11
use OCP\IDb;
12
use OCP\AppFramework\Db\Mapper;
13
14
use OCA\Files_Antivirus\Db\Rule;
15
16
class RuleMapper extends Mapper {
17 6
	public function __construct(IDb $db) {
18 6
		parent::__construct($db, 'files_avir_status', '\OCA\Files_Antivirus\Db\Rule');
19 6
	}
20
	
21
	/**
22
	 * Empty the table
23
	 * @return bool
24
	 */
25 4
	public function deleteAll(){
26 4
		$sql = 'DELETE FROM `*PREFIX*files_avir_status`';
27 4
		return $this->execute($sql);
28
	}
29
	
30
	/**
31
	 * Find rule by id
32
	 * @param int $id
33
	 * @return Rule
34
	 */
35
	public function find($id){
36
        $sql = 'SELECT * FROM `*PREFIX*files_avir_status` WHERE `id` = ?';
37
        return $this->findEntity($sql, array($id));
38
	}
39
	
40
	/**
41
	 * Get all rules
42
	 */
43
	public function findAll(){
44
		$sql = 'SELECT * FROM `*PREFIX*files_avir_status`';
45
        return $this->findEntities($sql);
46
	}
47
48
	/**
49
	 * Get collection of rules by given exit code
50
	 * @param int $result
51
	 * @return array
52
	 */
53 3
	public function findByResult($result){
54 3
		$sql = 'SELECT * FROM `*PREFIX*files_avir_status` WHERE `status_type`=? and `result`=?';
55 3
		return $this->findEntities($sql, array(Rule::RULE_TYPE_CODE, $result));
56
	}
57
	
58
	/**
59
	 * Get collection of rules of type Match
60
	 * @param int $status
61
	 * @return array
62
	 */
63 3
	public function findAllMatchedByStatus($status){
64 3
		$sql = 'SELECT * FROM `*PREFIX*files_avir_status` WHERE `status_type`=? and `status`=?';
65 3
		return $this->findEntities($sql, array(Rule::RULE_TYPE_MATCH, $status));
66
	}
67
	
68
	/**
69
	 * Fill the table with rules used with clamav
70
	 */
71 4
	public function populate(){
72
		$descriptions = array(
73
			array(
74 4
				'groupId' => 0,
75 4
				'statusType' => Rule::RULE_TYPE_CODE,
76 4
				'result' => 0,
77 4
				'match' => '',
78 4
				'description' => "",
79
				'status' => \OCA\Files_Antivirus\Status::SCANRESULT_CLEAN
80
			),
81
82
			array(
83 4
				'groupId' => 0,
84 4
				'statusType' => Rule::RULE_TYPE_CODE,
85 4
				'result' => 1,
86 4
				'match' => '',
87 4
				'description' => "",
88
				'status' => \OCA\Files_Antivirus\Status::SCANRESULT_INFECTED
89
			),
90
		
91
			array(
92 4
				'groupId' => 0,
93 4
				'statusType' => Rule::RULE_TYPE_CODE,
94 4
				'result' => 40,
95 4
				'match' => '',
96 4
				'description' => "Unknown option passed.",
97
				'status' => \OCA\Files_Antivirus\Status::SCANRESULT_UNCHECKED
98
			),
99
			
100
			array(
101 4
				'groupId' => 0,
102 4
				'statusType' => Rule::RULE_TYPE_CODE,
103 4
				'result' => 50,
104 4
				'match' => '',
105 4
				'description' => "Database initialization error.",
106
				'status' => \OCA\Files_Antivirus\Status::SCANRESULT_UNCHECKED
107
			),
108
			
109
			array(
110 4
				'groupId' => 0,
111 4
				'statusType' => Rule::RULE_TYPE_CODE,
112 4
				'result' => 52,
113 4
				'match' => '',
114 4
				'description' => "Not supported file type.",
115
				'status' => \OCA\Files_Antivirus\Status::SCANRESULT_UNCHECKED
116
			),
117
			
118
			array(
119 4
				'groupId' => 0,
120 4
				'statusType' => Rule::RULE_TYPE_CODE,
121 4
				'result' => 53,
122 4
				'match' => '',
123 4
				'description' => "Can't open directory.",
124
				'status' => \OCA\Files_Antivirus\Status::SCANRESULT_UNCHECKED
125
			),
126
			
127
			array(
128 4
				'groupId' => 0,
129 4
				'statusType' => Rule::RULE_TYPE_CODE,
130 4
				'result' => 54,
131 4
				'match' => '',
132 4
				'description' => "Can't open file. (ofm)",
133
				'status' => \OCA\Files_Antivirus\Status::SCANRESULT_UNCHECKED
134
			),
135
			
136
			array(
137 4
				'groupId' => 0,
138 4
				'statusType' => Rule::RULE_TYPE_CODE,
139 4
				'result' => 55,
140 4
				'match' => '',
141 4
				'description' => "Error reading file. (ofm)",
142
				'status' => \OCA\Files_Antivirus\Status::SCANRESULT_UNCHECKED
143
			),
144
			
145
			array(
146 4
				'groupId' => 0,
147 4
				'statusType' => Rule::RULE_TYPE_CODE,
148 4
				'result' => 56,
149 4
				'match' => '',
150 4
				'description' => "Can't stat input file / directory.",
151
				'status' => \OCA\Files_Antivirus\Status::SCANRESULT_UNCHECKED
152
			),
153
			
154
			array(
155 4
				'groupId' => 0,
156 4
				'statusType' => Rule::RULE_TYPE_CODE,
157 4
				'result' => 57,
158 4
				'match' => '',
159 4
				'description' => "Can't get absolute path name of current working directory.",
160
				'status' => \OCA\Files_Antivirus\Status::SCANRESULT_UNCHECKED
161
			),
162
			
163
			array(
164 4
				'groupId' => 0,
165 4
				'statusType' => Rule::RULE_TYPE_CODE,
166 4
				'result' => 58,
167 4
				'match' => '',
168 4
				'description' => "I/O error, please check your file system.",
169
				'status' => \OCA\Files_Antivirus\Status::SCANRESULT_UNCHECKED
170
			),
171
			
172
			array(
173 4
				'groupId' => 0,
174 4
				'statusType' => Rule::RULE_TYPE_CODE,
175 4
				'result' => 62,
176 4
				'match' => '',
177 4
				'description' => "Can't initialize logger.",
178
				'status' => \OCA\Files_Antivirus\Status::SCANRESULT_UNCHECKED
179
			),
180
			
181
			array(
182 4
				'groupId' => 0,
183 4
				'statusType' => Rule::RULE_TYPE_CODE,
184 4
				'result' => 63,
185 4
				'match' => '',
186 4
				'description' => "Can't create temporary files/directories (check permissions).",
187
				'status' => \OCA\Files_Antivirus\Status::SCANRESULT_UNCHECKED
188
			),
189
			
190
			array(
191 4
				'groupId' => 0,
192 4
				'statusType' => Rule::RULE_TYPE_CODE,
193 4
				'result' => 64,
194 4
				'match' => '',
195 4
				'description' => "Can't write to temporary directory (please specify another one).",
196
				'status' => \OCA\Files_Antivirus\Status::SCANRESULT_UNCHECKED
197
			),
198
			
199
			array(
200 4
				'groupId' => 0,
201 4
				'statusType' => Rule::RULE_TYPE_CODE,
202 4
				'result' => 70,
203 4
				'match' => '',
204 4
				'description' => "Can't allocate memory (calloc).",
205
				'status' => \OCA\Files_Antivirus\Status::SCANRESULT_UNCHECKED
206
			),
207
			
208
			array(
209 4
				'groupId' => 0,
210 4
				'statusType' => Rule::RULE_TYPE_CODE,
211 4
				'result' => 71,
212 4
				'match' => '',
213 4
				'description' => "Can't allocate memory (malloc).",
214
				'status' => \OCA\Files_Antivirus\Status::SCANRESULT_UNCHECKED
215
			),
216
			
217
			array(
218 4
				'groupId' => 0,
219 4
				'statusType' => Rule::RULE_TYPE_MATCH,
220 4
				'result' => 0,
221 4
				'match' => '/.*: OK$/',
222 4
				'description' => '',
223
				'status' => \OCA\Files_Antivirus\Status::SCANRESULT_CLEAN
224
			),
225
			
226
			array(
227 4
				'groupId' => 0,
228 4
				'statusType' => Rule::RULE_TYPE_MATCH,
229 4
				'result' => 0,
230 4
				'match' => '/.*: (.*) FOUND$/',
231 4
				'description' => '',
232
				'status' => \OCA\Files_Antivirus\Status::SCANRESULT_INFECTED
233
			),
234
			
235
			array(
236 4
				'groupId' => 0,
237 4
				'statusType' => Rule::RULE_TYPE_MATCH,
238 4
				'result' => 0,
239 4
				'match' => '/.*: (.*) ERROR$/',
240 4
				'description' => '',
241
				'status' => \OCA\Files_Antivirus\Status::SCANRESULT_UNCHECKED
242
			),
243
		);
244
		
245 4
		foreach ($descriptions as $description){
246 4
			$rule = Rule::fromParams($description);
247 4
			$this->insert($rule);
248
		}
249 4
	}
250
}
251