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