Completed
Pull Request — master (#42)
by Roeland
06:32
created

RuleMapper   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 235
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 75%

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 0
dl 0
loc 235
ccs 18
cts 24
cp 0.75
rs 10
c 0
b 0
f 0

7 Methods

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