Completed
Push — master ( 08f8fe...60e202 )
by Roeland
11s
created

Provider::parse()   C

Complexity

Conditions 9
Paths 8

Size

Total Lines 69
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 0
Metric Value
dl 0
loc 69
ccs 0
cts 55
cp 0
rs 6.2192
c 0
b 0
f 0
cc 9
eloc 45
nc 8
nop 3
crap 90

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * @copyright Copyright (c) 2018 Roeland Jago Douma <[email protected]>
4
 *
5
 * @author Roeland Jago Douma <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
namespace OCA\Files_Antivirus\Activity;
24
25
use OCA\Files_Antivirus\AppInfo\Application;
26
use OCP\Activity\IEvent;
27
use OCP\Activity\IProvider;
28
use OCP\Files\IRootFolder;
29
use OCP\IURLGenerator;
30
use OCP\L10N\IFactory;
31
32
class Provider implements IProvider {
33
34
	const TYPE_VIRUS_DETECTED = 'virus_detected';
35
36
	const SUBJECT_VIRUS_DETECTED = 'virus_detected';
37
	const SUBJECT_VIRUS_DETECTED_UPLOAD = 'virus_detected_upload';
38
	const SUBJECT_VIRUS_DETECTED_SCAN = 'virus_detected_scan';
39
40
	const MESSAGE_FILE_DELETED = 'file_deleted';
41
42
	/** @var IFactory */
43
	private $languageFactory;
44
45
	/** @var IURLGenerator */
46
	private $urlGenerator;
47
48
	public function __construct(IFactory $languageFactory, IURLGenerator $urlGenerator) {
49
		$this->languageFactory = $languageFactory;
50
		$this->urlGenerator = $urlGenerator;
51
	}
52
53
	public function parse($language, IEvent $event, IEvent $previousEvent = null) {
54
		if ($event->getApp() !== Application::APP_NAME || $event->getType() !== self::TYPE_VIRUS_DETECTED) {
55
			throw new \InvalidArgumentException();
56
		}
57
58
		$l = $this->languageFactory->get('files_antivirus', $language);
59
60
		$parameters = [];
61
		$subject = '';
62
63
		if ($event->getSubject() === self::SUBJECT_VIRUS_DETECTED) {
64
			$subject = $l->t('File {file} is infected with {virus}');
65
66
			$params = $event->getSubjectParameters();
67
			$parameters['virus'] = [
68
				'type' => 'highlight',
69
				'id' => $params[1],
70
				'name' => $params[1],
71
			];
72
73
			$parameters['file'] = [
74
				'type' => 'highlight',
75
				'id' => $event->getObjectName(),
76
				'name' => basename($event->getObjectName()),
77
			];
78
79
			if ($event->getMessage() === self::MESSAGE_FILE_DELETED) {
80
				$event->setParsedMessage($l->t('The file has been removed'));
81
			}
82
83
		} else if ($event->getSubject() === self::SUBJECT_VIRUS_DETECTED_UPLOAD) {
84
			$subject = $l->t('File containing {virus} detected');
85
86
			$params = $event->getSubjectParameters();
87
			$parameters['virus'] = [
88
				'type' => 'highlight',
89
				'id' => $params[0],
90
				'name' => $params[0],
91
			];
92
93
			if ($event->getMessage() === self::MESSAGE_FILE_DELETED) {
94
				$event->setParsedMessage($l->t('The file has been removed'));
95
			}
96
			$event->setIcon($this->urlGenerator->imagePath('files_antivirus', 'shield-green.svg'));
97
		} else if ($event->getSubject() === self::SUBJECT_VIRUS_DETECTED_SCAN) {
98
			$subject = $l->t('File {file} is infected with {virus}');
99
100
			$params = $event->getSubjectParameters();
101
			$parameters['virus'] = [
102
				'type' => 'highlight',
103
				'id' => $params[0],
104
				'name' => $params[0],
105
			];
106
107
			if ($event->getMessage() === self::MESSAGE_FILE_DELETED) {
108
				$event->setParsedMessage($l->t('The file has been removed'));
109
110
				$parameters['file'] = $this->getFileDeleted($event);
111
				$event->setIcon($this->urlGenerator->imagePath('files_antivirus', 'shield-green.svg'));
112
			} else {
113
				$parameters['file'] = $this->getFileExisting($event);
114
				$event->setIcon($this->urlGenerator->imagePath('files_antivirus', 'shield-red.svg'));
115
			}
116
		}
117
118
		$this->setSubjects($event, $subject, $parameters);
119
120
		return $event;
121
	}
122
123
	private function setSubjects(IEvent $event, $subject, array $parameters) {
124
		$placeholders = $replacements = [];
125
		foreach ($parameters as $placeholder => $parameter) {
126
			$placeholders[] = '{' . $placeholder . '}';
127
			if ($parameter['type'] === 'file') {
128
				$replacements[] = $parameter['path'];
129
			} else {
130
				$replacements[] = $parameter['name'];
131
			}
132
		}
133
134
		$event->setParsedSubject(str_replace($placeholders, $replacements, $subject))
135
			->setRichSubject($subject, $parameters);
136
	}
137
138
	private function getFileExisting(IEvent $event) {
139
		$res = $this->getFileDeleted($event);
140
		$res['link'] = $this->urlGenerator->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $event->getObjectId()]);
141
		return $res;
142
	}
143
144
	private function getFileDeleted(IEvent $event) {
145
		return [
146
			'type' => 'file',
147
			'id' => $event->getObjectId(),
148
			'name' => basename($event->getObjectName()),
149
			'path' => $event->getObjectName(),
150
		];
151
	}
152
153
}
154