Provider::getFileExisting()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
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\IURLGenerator;
29
use OCP\L10N\IFactory;
30
31
class Provider implements IProvider {
32
	public const TYPE_VIRUS_DETECTED = 'virus_detected';
33
34
	public const SUBJECT_VIRUS_DETECTED = 'virus_detected';
35
	public const SUBJECT_VIRUS_DETECTED_UPLOAD = 'virus_detected_upload';
36
	public const SUBJECT_VIRUS_DETECTED_SCAN = 'virus_detected_scan';
37
38
	public const MESSAGE_FILE_DELETED = 'file_deleted';
39
40
	/** @var IFactory */
41
	private $languageFactory;
42
43
	/** @var IURLGenerator */
44
	private $urlGenerator;
45
46
	public function __construct(IFactory $languageFactory, IURLGenerator $urlGenerator) {
47
		$this->languageFactory = $languageFactory;
48
		$this->urlGenerator = $urlGenerator;
49
	}
50
51
	public function parse($language, IEvent $event, IEvent $previousEvent = null) {
52
		if ($event->getApp() !== Application::APP_NAME || $event->getType() !== self::TYPE_VIRUS_DETECTED) {
53
			throw new \InvalidArgumentException();
54
		}
55
56
		$l = $this->languageFactory->get('files_antivirus', $language);
57
58
		$parameters = [];
59
		$subject = '';
60
61
		if ($event->getSubject() === self::SUBJECT_VIRUS_DETECTED) {
62
			$subject = $l->t('File {file} is infected with {virus}');
63
64
			$params = $event->getSubjectParameters();
65
			$parameters['virus'] = [
66
				'type' => 'highlight',
67
				'id' => $params[1],
68
				'name' => $params[1],
69
			];
70
71
			$parameters['file'] = [
72
				'type' => 'highlight',
73
				'id' => $event->getObjectName(),
74
				'name' => basename($event->getObjectName()),
75
			];
76
77
			if ($event->getMessage() === self::MESSAGE_FILE_DELETED) {
78
				$event->setParsedMessage($l->t('The file has been removed'));
79
			}
80
		} elseif ($event->getSubject() === self::SUBJECT_VIRUS_DETECTED_UPLOAD) {
81
			$subject = $l->t('File containing {virus} detected');
82
83
			$params = $event->getSubjectParameters();
84
			$parameters['virus'] = [
85
				'type' => 'highlight',
86
				'id' => $params[0],
87
				'name' => $params[0],
88
			];
89
90
			if ($event->getMessage() === self::MESSAGE_FILE_DELETED) {
91
				$event->setParsedMessage($l->t('The file has been removed'));
92
			}
93
			$event->setIcon($this->urlGenerator->imagePath('files_antivirus', 'shield-dark.svg'));
94
		} elseif ($event->getSubject() === self::SUBJECT_VIRUS_DETECTED_SCAN) {
95
			$subject = $l->t('File {file} is infected with {virus}');
96
97
			$params = $event->getSubjectParameters();
98
			$parameters['virus'] = [
99
				'type' => 'highlight',
100
				'id' => $params[0],
101
				'name' => $params[0],
102
			];
103
104
			if ($event->getMessage() === self::MESSAGE_FILE_DELETED) {
105
				$event->setParsedMessage($l->t('The file has been removed'));
106
107
				$parameters['file'] = $this->getFileDeleted($event);
108
				$event->setIcon($this->urlGenerator->imagePath('files_antivirus', 'shield-dark.svg'));
109
			} else {
110
				$parameters['file'] = $this->getFileExisting($event);
111
				$event->setIcon($this->urlGenerator->imagePath('files_antivirus', 'shield-red.svg'));
112
			}
113
		}
114
115
		$this->setSubjects($event, $subject, $parameters);
116
117
		return $event;
118
	}
119
120
	private function setSubjects(IEvent $event, $subject, array $parameters) {
121
		$placeholders = $replacements = [];
122
		foreach ($parameters as $placeholder => $parameter) {
123
			$placeholders[] = '{' . $placeholder . '}';
124
			if ($parameter['type'] === 'file') {
125
				$replacements[] = $parameter['path'];
126
			} else {
127
				$replacements[] = $parameter['name'];
128
			}
129
		}
130
131
		$event->setParsedSubject(str_replace($placeholders, $replacements, $subject))
132
			->setRichSubject($subject, $parameters);
133
	}
134
135
	private function getFileExisting(IEvent $event) {
136
		$res = $this->getFileDeleted($event);
137
		$res['link'] = $this->urlGenerator->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $event->getObjectId()]);
138
		return $res;
139
	}
140
141
	private function getFileDeleted(IEvent $event) {
142
		return [
143
			'type' => 'file',
144
			'id' => $event->getObjectId(),
145
			'name' => basename($event->getObjectName()),
146
			'path' => $event->getObjectName(),
147
		];
148
	}
149
}
150