Completed
Pull Request — master (#50)
by Roeland
01:32
created

RuleMapper::findByResult()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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