Completed
Pull Request — stable9 (#144)
by Victor
02:42
created

RuleMapper::find()   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 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
cc 1
eloc 3
nc 1
nop 1
crap 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 4
	public function __construct(IDb $db) {
18 4
		parent::__construct($db, 'files_avir_status', '\OCA\Files_Antivirus\Db\Rule');
19 4
	}
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 1
	public function findAllMatchedByStatus($status){
64 1
		$sql = 'SELECT * FROM `*PREFIX*files_avir_status` WHERE `status_type`=? and `status`=?';
65 1
		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
				'groupId' => 0,
75
				'statusType' => Rule::RULE_TYPE_CODE,
76
				'result' => 0,
77
				'match' => '',
78
				'description' => "",
79
				'status' => \OCA\Files_Antivirus\Status::SCANRESULT_CLEAN
80 4
			),
81
82
			array(
83
				'groupId' => 0,
84
				'statusType' => Rule::RULE_TYPE_CODE,
85
				'result' => 1,
86
				'match' => '',
87
				'description' => "",
88
				'status' => \OCA\Files_Antivirus\Status::SCANRESULT_INFECTED
89
			),
90
		
91
			array(
92
				'groupId' => 0,
93
				'statusType' => Rule::RULE_TYPE_CODE,
94
				'result' => 40,
95
				'match' => '',
96
				'description' => "Unknown option passed.",
97
				'status' => \OCA\Files_Antivirus\Status::SCANRESULT_UNCHECKED
98
			),
99
			
100
			array(
101
				'groupId' => 0,
102
				'statusType' => Rule::RULE_TYPE_CODE,
103
				'result' => 50,
104
				'match' => '',
105
				'description' => "Database initialization error.",
106
				'status' => \OCA\Files_Antivirus\Status::SCANRESULT_UNCHECKED
107
			),
108
			
109
			array(
110
				'groupId' => 0,
111
				'statusType' => Rule::RULE_TYPE_CODE,
112
				'result' => 52,
113
				'match' => '',
114
				'description' => "Not supported file type.",
115
				'status' => \OCA\Files_Antivirus\Status::SCANRESULT_UNCHECKED
116
			),
117
			
118
			array(
119
				'groupId' => 0,
120
				'statusType' => Rule::RULE_TYPE_CODE,
121
				'result' => 53,
122
				'match' => '',
123
				'description' => "Can't open directory.",
124
				'status' => \OCA\Files_Antivirus\Status::SCANRESULT_UNCHECKED
125
			),
126
			
127
			array(
128
				'groupId' => 0,
129
				'statusType' => Rule::RULE_TYPE_CODE,
130
				'result' => 54,
131
				'match' => '',
132
				'description' => "Can't open file. (ofm)",
133
				'status' => \OCA\Files_Antivirus\Status::SCANRESULT_UNCHECKED
134
			),
135
			
136
			array(
137
				'groupId' => 0,
138
				'statusType' => Rule::RULE_TYPE_CODE,
139
				'result' => 55,
140
				'match' => '',
141
				'description' => "Error reading file. (ofm)",
142
				'status' => \OCA\Files_Antivirus\Status::SCANRESULT_UNCHECKED
143
			),
144
			
145
			array(
146
				'groupId' => 0,
147
				'statusType' => Rule::RULE_TYPE_CODE,
148
				'result' => 56,
149
				'match' => '',
150
				'description' => "Can't stat input file / directory.",
151
				'status' => \OCA\Files_Antivirus\Status::SCANRESULT_UNCHECKED
152
			),
153
			
154
			array(
155
				'groupId' => 0,
156
				'statusType' => Rule::RULE_TYPE_CODE,
157
				'result' => 57,
158
				'match' => '',
159
				'description' => "Can't get absolute path name of current working directory.",
160
				'status' => \OCA\Files_Antivirus\Status::SCANRESULT_UNCHECKED
161
			),
162
			
163
			array(
164
				'groupId' => 0,
165
				'statusType' => Rule::RULE_TYPE_CODE,
166
				'result' => 58,
167
				'match' => '',
168
				'description' => "I/O error, please check your file system.",
169
				'status' => \OCA\Files_Antivirus\Status::SCANRESULT_UNCHECKED
170
			),
171
			
172
			array(
173
				'groupId' => 0,
174
				'statusType' => Rule::RULE_TYPE_CODE,
175
				'result' => 62,
176
				'match' => '',
177
				'description' => "Can't initialize logger.",
178
				'status' => \OCA\Files_Antivirus\Status::SCANRESULT_UNCHECKED
179
			),
180
			
181
			array(
182
				'groupId' => 0,
183
				'statusType' => Rule::RULE_TYPE_CODE,
184
				'result' => 63,
185
				'match' => '',
186
				'description' => "Can't create temporary files/directories (check permissions).",
187
				'status' => \OCA\Files_Antivirus\Status::SCANRESULT_UNCHECKED
188
			),
189
			
190
			array(
191
				'groupId' => 0,
192
				'statusType' => Rule::RULE_TYPE_CODE,
193
				'result' => 64,
194
				'match' => '',
195
				'description' => "Can't write to temporary directory (please specify another one).",
196
				'status' => \OCA\Files_Antivirus\Status::SCANRESULT_UNCHECKED
197
			),
198
			
199
			array(
200
				'groupId' => 0,
201
				'statusType' => Rule::RULE_TYPE_CODE,
202
				'result' => 70,
203
				'match' => '',
204
				'description' => "Can't allocate memory (calloc).",
205
				'status' => \OCA\Files_Antivirus\Status::SCANRESULT_UNCHECKED
206
			),
207
			
208
			array(
209
				'groupId' => 0,
210
				'statusType' => Rule::RULE_TYPE_CODE,
211
				'result' => 71,
212
				'match' => '',
213
				'description' => "Can't allocate memory (malloc).",
214
				'status' => \OCA\Files_Antivirus\Status::SCANRESULT_UNCHECKED
215
			),
216
			
217
			array(
218
				'groupId' => 0,
219
				'statusType' => Rule::RULE_TYPE_MATCH,
220
				'result' => 0,
221
				'match' => '/.*: OK$/',
222
				'description' => '',
223
				'status' => \OCA\Files_Antivirus\Status::SCANRESULT_CLEAN
224
			),
225
			
226
			array(
227
				'groupId' => 0,
228
				'statusType' => Rule::RULE_TYPE_MATCH,
229
				'result' => 0,
230
				'match' => '/.*: (.*) FOUND$/',
231
				'description' => '',
232
				'status' => \OCA\Files_Antivirus\Status::SCANRESULT_INFECTED
233
			),
234
			
235
			array(
236
				'groupId' => 0,
237
				'statusType' => Rule::RULE_TYPE_MATCH,
238
				'result' => 0,
239
				'match' => '/.*: (.*) ERROR$/',
240
				'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