Completed
Pull Request — master (#69)
by Roeland
03:12
created

Provider::getFileExisting()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 0
cts 5
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
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\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
		if ($event->getSubject() === self::SUBJECT_VIRUS_DETECTED) {
61
			$event->setParsedSubject($l->t('File %s is infected with %s', $event->getSubjectParameters()));
62
			if ($event->getMessage() === self::MESSAGE_FILE_DELETED) {
63
				$event->setParsedMessage($l->t('The file has been removed'));
64
			}
65
66
		} else if ($event->getSubject() === self::SUBJECT_VIRUS_DETECTED_UPLOAD) {
67
			$event->setParsedSubject($l->t('File containing %s detected', $event->getSubjectParameters()));
68
69
			if ($event->getMessage() === self::MESSAGE_FILE_DELETED) {
70
				$event->setParsedMessage($l->t('The file has been removed'));
71
			}
72
			$event->setIcon($this->urlGenerator->imagePath('files_antivirus', 'shield-green.svg'));
73
		} else if ($event->getSubject() === self::SUBJECT_VIRUS_DETECTED_SCAN) {
74
			$subject = $l->t('File {file} is infected with {virus}');
75
76
			$subject = str_replace(['{virus}'], $event->getSubjectParameters(), $subject);
77
78
			if ($event->getMessage() === self::MESSAGE_FILE_DELETED) {
79
				$event->setParsedMessage($l->t('The file has been removed'));
80
81
				$file = $this->getFileDeleted($event);
82
				$event->setIcon($this->urlGenerator->imagePath('files_antivirus', 'shield-green.svg'));
83
			} else {
84
				$file = $this->getFileExisting($event);
85
				$event->setIcon($this->urlGenerator->imagePath('files_antivirus', 'shield-red.svg'));
86
			}
87
88
			$event->setParsedSubject(str_replace('{file}', $file['name'], $subject))
89
				->setRichSubject($subject, ['file' => $file]);
90
		}
91
92
		return $event;
93
	}
94
95
	private function getFileExisting(IEvent $event) {
96
		$res = $this->getFileDeleted($event);
97
		$res['link'] = $this->urlGenerator->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $event->getObjectId()]);
98
		return $res;
99
	}
100
101
	private function getFileDeleted(IEvent $event) {
102
		return [
103
			'id' => $event->getObjectId(),
104
			'name' => basename($event->getObjectName()),
105
			'path' => $event->getObjectName(),
106
		];
107
	}
108
109
}
110