Completed
Pull Request — master (#225)
by Victor
11:27 queued 09:57
created

RuleMapper   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 251
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 95.04%

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 0
dl 0
loc 251
ccs 115
cts 121
cp 0.9504
rs 10
c 0
b 0
f 0

7 Methods

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