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

Activity   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 222
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 25.53%

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 0
dl 0
loc 222
ccs 12
cts 47
cp 0.2553
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getL10N() 0 3 1
A getNotificationTypes() 0 9 1
A getDefaultTypes() 0 7 1
A getTypeIcon() 0 7 2
A translate() 0 14 4
A getSpecialParameterList() 0 3 1
A getGroupParameter() 0 3 1
A getNavigation() 0 16 1
A isFilterValid() 0 3 1
A filterNotificationTypes() 0 3 2
A getQueryForFilter() 0 9 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;
10
11
use OC\L10N\Factory;
12
use OCP\Activity\IExtension;
13
use OCP\IURLGenerator;
14
15
class Activity implements IExtension {
16
	
17
	const APP_NAME = 'files_antivirus';
18
19
	/**
20
	 * Filter with all app related activities
21
	 */
22
	const FILTER_AVIR = 'files_antivirus';
23
24
	/**
25
	 * Activity types known to this extension
26
	 */
27
	const TYPE_VIRUS_DETECTED = 'virus_detected';
28
29
	/**
30
	 * Subject keys for translation of the subjections
31
	 */
32
	const SUBJECT_VIRUS_DETECTED = 'virus_detected';
33
	
34
	/**
35
	 * Infected file deletion notice
36
	 */
37
	const MESSAGE_FILE_DELETED = 'file_deleted';
38
39
40
	/**
41
	 * @var Factory
42
	 */
43
	protected $languageFactory;
44
45
	/**
46
	 * @var IURLGenerator
47
	 */
48
	protected $URLGenerator;
49
50
	/**
51
	 * @param Factory $languageFactory
52
	 * @param IURLGenerator $URLGenerator
53
	 */
54 3
	public function __construct(Factory $languageFactory, IURLGenerator $URLGenerator) {
55 3
		$this->languageFactory = $languageFactory;
56 3
		$this->URLGenerator = $URLGenerator;
57 3
	}
58
59
	protected function getL10N($languageCode = null) {
60
		return $this->languageFactory->get(self::APP_NAME, $languageCode);
61
	}
62
63
	/**
64
	 * The extension can return an array of additional notification types.
65
	 * If no additional types are to be added false is to be returned
66
	 *
67
	 * @param string $languageCode
68
	 *
69
	 * @return array|false
70
	 */
71
	public function getNotificationTypes($languageCode) {
72
		$l = $this->getL10N($languageCode);
73
74
		return [
75
			self::TYPE_VIRUS_DETECTED => (string) $l->t(
76
				'<strong>Infected file</strong> has been <strong>found</strong>'
77
			),
78
		];
79
	}
80
81
	/**
82
	 * For a given method additional types to be displayed in the settings can be returned.
83
	 * In case no additional types are to be added false is to be returned.
84
	 *
85
	 * @param string $method
86
	 * @return array|false
87
	 */
88
	public function getDefaultTypes($method) {
89
		$defaultTypes = [
90
			self::TYPE_VIRUS_DETECTED,
91
		];
92
93
		return $defaultTypes;
94
	}
95
96
	/**
97
	 * A string naming the css class for the icon to be used can be returned.
98
	 * If no icon is known for the given type false is to be returned.
99
	 *
100
	 * @param string $type
101
	 * @return string|false
102
	 */
103 1
	public function getTypeIcon($type) {
104
		switch ($type) {
105 1
			case self::TYPE_VIRUS_DETECTED:
106 1
				return 'icon-info';
107
		}
108 1
		return false;
109
	}
110
111
	/**
112
	 * The extension can translate a given message to the requested languages.
113
	 * If no translation is available false is to be returned.
114
	 *
115
	 * @param string $app
116
	 * @param string $text
117
	 * @param array $params
118
	 * @param boolean $stripPath
119
	 * @param boolean $highlightParams
120
	 * @param string $languageCode
121
	 *
122
	 * @return string|false
123
	 */
124
	public function translate($app, $text, $params, $stripPath, $highlightParams, $languageCode) {
125
		$l = $this->getL10N($languageCode);
126
127
		if ($app === self::APP_NAME) {
128
			switch ($text) {
129
				case self::SUBJECT_VIRUS_DETECTED:
130
					return (string) $l->t('File %s is infected with %s', $params);
131
				case self::MESSAGE_FILE_DELETED:
132
					return (string) $l->t('It is going to be deleted', $params);
133
			}
134
		}
135
136
		return false;
137
	}
138
139
	/**
140
	 * The extension can define the type of parameters for translation
141
	 *
142
	 * Currently known types are:
143
	 * * file		=> will strip away the path of the file and add a tooltip with it
144
	 * * username	=> will add the avatar of the user
145
	 *
146
	 * @param string $app
147
	 * @param string $text
148
	 *
149
	 * @return array|false
150
	 */
151 1
	public function getSpecialParameterList($app, $text) {
152 1
		return false;
153
	}
154
155
	/**
156
	 * The extension can define the parameter grouping by returning the index as integer.
157
	 * In case no grouping is required false is to be returned.
158
	 *
159
	 * @param array $activity
160
	 *
161
	 * @return integer|false
162
	 */
163 1
	public function getGroupParameter($activity) {
164 1
		return false;
165
	}
166
167
	/**
168
	 * The extension can define additional navigation entries. The array returned has to contain two keys 'top'
169
	 * and 'apps' which hold arrays with the relevant entries.
170
	 * If no further entries are to be added false is no be returned.
171
	 *
172
	 * @return array|false
173
	 */
174
	public function getNavigation() {
175
		$l = $this->getL10N();
176
		return [
177
			'apps' => [
178
				self::FILTER_AVIR => [
179
					'id' => self::FILTER_AVIR,
180
					'name' => (string) $l->t('Antivirus'),
181
					'url' => $this->URLGenerator->linkToRoute(
182
						'activity.Activities.showList',
183
						['filter' => self::FILTER_AVIR]
184
					),
185
				],
186
			],
187
			'top' => []
188
		];
189
	}
190
191
	/**
192
	 * The extension can check if a customer filter (given by
193
	 * a query string like filter=abc) is valid or not.
194
	 *
195
	 * @param string $filterValue
196
	 *
197
	 * @return boolean
198
	 */
199
	public function isFilterValid($filterValue) {
200
		return $filterValue === self::FILTER_AVIR;
201
	}
202
203
	/**
204
	 * The extension can filter the types based on the filter if required.
205
	 * In case no filter is to be applied false is to be returned unchanged.
206
	 *
207
	 * @param array $types
208
	 * @param string $filter
209
	 *
210
	 * @return array|false
211
	 */
212
	public function filterNotificationTypes($types, $filter) {
213
		return $filter === self::FILTER_AVIR ? [self::TYPE_VIRUS_DETECTED] : $types;
214
	}
215
216
	/**
217
	 * For a given filter the extension can specify the sql query conditions
218
	 * including parameters for that query.
219
	 * In case the extension does not know the filter false is to be returned.
220
	 * The query condition and the parameters are to be returned as array with two elements.
221
	 * E.g. return ['`app` = ? and `message` like ?', array('mail', 'ownCloud%')];
222
	 *
223
	 * @param string $filter
224
	 *
225
	 * @return array|false
226
	 */
227
	public function getQueryForFilter($filter) {
228
		if ($filter === self::FILTER_AVIR) {
229
			return [
230
				'(`app` = ?)',
231
				[self::APP_NAME],
232
			];
233
		}
234
		return false;
235
	}
236
}
237