Completed
Pull Request — master (#225)
by Victor
05:30 queued 04:01
created

RuleMapper::findAll()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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