Completed
Push — master ( f692ea...2eca9c )
by Lukas
08:20
created

Downloads::getParsedParameters()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 24
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 17
nc 5
nop 1
dl 0
loc 24
rs 8.5125
c 1
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016 Joas Schilling <[email protected]>
4
 *
5
 * @license GNU AGPL version 3 or any later version
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Affero General Public License as
9
 * published by the Free Software Foundation, either version 3 of the
10
 * License, or (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 *
20
 */
21
22
namespace OCA\Files_Sharing\Activity\Providers;
23
24
use OCP\Activity\IEvent;
25
use OCP\Activity\IManager;
26
use OCP\Activity\IProvider;
27
use OCP\IL10N;
28
use OCP\IURLGenerator;
29
30
class Downloads implements IProvider {
31
32
	/** @var IL10N */
33
	protected $l;
34
35
	/** @var IURLGenerator */
36
	protected $url;
37
38
	/** @var IManager */
39
	protected $activityManager;
40
41
	const SUBJECT_PUBLIC_SHARED_FILE_DOWNLOADED = 'public_shared_file_downloaded';
42
	const SUBJECT_PUBLIC_SHARED_FOLDER_DOWNLOADED = 'public_shared_folder_downloaded';
43
44
	const SUBJECT_SHARED_FILE_BY_EMAIL_DOWNLOADED = 'file_shared_with_email_downloaded';
45
	const SUBJECT_SHARED_FOLDER_BY_EMAIL_DOWNLOADED = 'folder_shared_with_email_downloaded';
46
47
	/**
48
	 * @param IL10N $l
49
	 * @param IURLGenerator $url
50
	 * @param IManager $activityManager
51
	 */
52
	public function __construct(IL10N $l, IURLGenerator $url, IManager $activityManager) {
53
		$this->l = $l;
54
		$this->url = $url;
55
		$this->activityManager = $activityManager;
56
	}
57
58
	/**
59
	 * @param IEvent $event
60
	 * @param IEvent|null $previousEvent
61
	 * @return IEvent
62
	 * @throws \InvalidArgumentException
63
	 * @since 11.0.0
64
	 */
65
	public function parse(IEvent $event, IEvent $previousEvent = null) {
66
		if ($event->getApp() !== 'files_sharing') {
67
			throw new \InvalidArgumentException();
68
		}
69
70
		if ($this->activityManager->isFormattingFilteredObject()) {
71
			try {
72
				return $this->parseShortVersion($event);
73
			} catch (\InvalidArgumentException $e) {
74
				// Ignore and simply use the long version...
75
			}
76
		}
77
78
		return $this->parseLongVersion($event);
79
	}
80
81
	/**
82
	 * @param IEvent $event
83
	 * @return IEvent
84
	 * @throws \InvalidArgumentException
85
	 * @since 11.0.0
86
	 */
87
	public function parseShortVersion(IEvent $event) {
88
		$parsedParameters = $this->getParsedParameters($event);
89
90
		if ($event->getSubject() === self::SUBJECT_PUBLIC_SHARED_FILE_DOWNLOADED ||
91
			$event->getSubject() === self::SUBJECT_PUBLIC_SHARED_FOLDER_DOWNLOADED) {
92
			$event->setParsedSubject($this->l->t('Downloaded via public link'))
93
				->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/download.svg')));
94
		} else if ($event->getSubject() === self::SUBJECT_SHARED_FILE_BY_EMAIL_DOWNLOADED ||
95
			$event->getSubject() === self::SUBJECT_SHARED_FOLDER_BY_EMAIL_DOWNLOADED) {
96
			$event->setParsedSubject($this->l->t('Downloaded by %1$s', $parsedParameters['email']['name']))
97
				->setRichSubject($this->l->t('Downloaded by {email}'), [
98
					'email' => $parsedParameters['email'],
99
				])
100
				->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/download.svg')));
101
		} else {
102
			throw new \InvalidArgumentException();
103
		}
104
105
		return $event;
106
	}
107
108
	/**
109
	 * @param IEvent $event
110
	 * @return IEvent
111
	 * @throws \InvalidArgumentException
112
	 * @since 11.0.0
113
	 */
114
	public function parseLongVersion(IEvent $event) {
115
		$parsedParameters = $this->getParsedParameters($event);
116
117
		if ($event->getSubject() === self::SUBJECT_PUBLIC_SHARED_FILE_DOWNLOADED ||
118
			$event->getSubject() === self::SUBJECT_PUBLIC_SHARED_FOLDER_DOWNLOADED) {
119
			$event->setParsedSubject($this->l->t('%1$s downloaded via public link', [
120
					$parsedParameters['file']['path'],
121
				]))
122
				->setRichSubject($this->l->t('{file} downloaded via public link'), [
123
					'file' => $parsedParameters['file'],
124
				])
125
				->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/download.svg')));
126
		} else if ($event->getSubject() === self::SUBJECT_SHARED_FILE_BY_EMAIL_DOWNLOADED ||
127
			$event->getSubject() === self::SUBJECT_SHARED_FOLDER_BY_EMAIL_DOWNLOADED) {
128
			$event->setParsedSubject($this->l->t('%1$s downloaded %2$s', [
129
					$parsedParameters['email']['name'],
130
					$parsedParameters['file']['path'],
131
				]))
132
				->setRichSubject($this->l->t('{email} downloaded {file}'), [
133
					'email' => $parsedParameters['email'],
134
					'file' => $parsedParameters['file'],
135
				])
136
				->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/download.svg')));
137
		} else {
138
			throw new \InvalidArgumentException();
139
		}
140
141
		return $event;
142
	}
143
144
	/**
145
	 * @param IEvent $event
146
	 * @return array
147
	 */
148
	protected function getParsedParameters(IEvent $event) {
149
		$subject = $event->getSubject();
150
		$parameters = $event->getSubjectParameters();
151
152
		switch ($subject) {
153
			case self::SUBJECT_PUBLIC_SHARED_FILE_DOWNLOADED:
154
			case self::SUBJECT_PUBLIC_SHARED_FOLDER_DOWNLOADED:
155
				return [
156
					'file' => $this->generateFileParameter($event->getObjectId(), $parameters[0]),
157
				];
158
			case self::SUBJECT_SHARED_FILE_BY_EMAIL_DOWNLOADED:
159
			case self::SUBJECT_SHARED_FOLDER_BY_EMAIL_DOWNLOADED:
160
				return [
161
					'file' => $this->generateFileParameter($event->getObjectId(), $parameters[0]),
162
					'email' => [
163
						'type' => 'email',
164
						'id' => $parameters[1],
165
						'name' => $parameters[1],
166
					],
167
				];
168
		}
169
170
		throw new \InvalidArgumentException();
171
	}
172
173
	/**
174
	 * @param int $id
175
	 * @param string $path
176
	 * @return array
177
	 */
178 View Code Duplication
	protected function generateFileParameter($id, $path) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
179
		return [
180
			'type' => 'file',
181
			'id' => $id,
182
			'name' => basename($path),
183
			'path' => $path,
184
			'link' => $this->url->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $id]),
185
		];
186
	}
187
}
188