Completed
Pull Request — master (#5628)
by Joas
17:04
created

FavoriteProvider::parseShortVersion()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 22
Code Lines 16

Duplication

Lines 17
Ratio 77.27 %

Importance

Changes 0
Metric Value
cc 5
eloc 16
nc 5
nop 1
dl 17
loc 22
rs 8.6737
c 0
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\Activity;
23
24
use OCP\Activity\IEvent;
25
use OCP\Activity\IEventMerger;
26
use OCP\Activity\IManager;
27
use OCP\Activity\IProvider;
28
use OCP\IL10N;
29
use OCP\IURLGenerator;
30
use OCP\L10N\IFactory;
31
32
class FavoriteProvider implements IProvider {
33
34
	const SUBJECT_ADDED = 'added_favorite';
35
	const SUBJECT_REMOVED = 'removed_favorite';
36
37
	/** @var IFactory */
38
	protected $languageFactory;
39
40
	/** @var IL10N */
41
	protected $l;
42
43
	/** @var IURLGenerator */
44
	protected $url;
45
46
	/** @var IManager */
47
	protected $activityManager;
48
49
	/** @var IEventMerger */
50
	protected $eventMerger;
51
52
	/**
53
	 * @param IFactory $languageFactory
54
	 * @param IURLGenerator $url
55
	 * @param IManager $activityManager
56
	 * @param IEventMerger $eventMerger
57
	 */
58 View Code Duplication
	public function __construct(IFactory $languageFactory, IURLGenerator $url, IManager $activityManager, IEventMerger $eventMerger) {
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...
59
		$this->languageFactory = $languageFactory;
60
		$this->url = $url;
61
		$this->activityManager = $activityManager;
62
		$this->eventMerger = $eventMerger;
63
	}
64
65
	/**
66
	 * @param string $language
67
	 * @param IEvent $event
68
	 * @param IEvent|null $previousEvent
69
	 * @return IEvent
70
	 * @throws \InvalidArgumentException
71
	 * @since 11.0.0
72
	 */
73 View Code Duplication
	public function parse($language, IEvent $event, IEvent $previousEvent = null) {
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...
74
		if ($event->getApp() !== 'files' || $event->getType() !== 'favorite') {
75
			throw new \InvalidArgumentException();
76
		}
77
78
		$this->l = $this->languageFactory->get('files', $language);
79
80
		if ($this->activityManager->isFormattingFilteredObject()) {
81
			try {
82
				return $this->parseShortVersion($event);
83
			} catch (\InvalidArgumentException $e) {
84
				// Ignore and simply use the long version...
85
			}
86
		}
87
88
		return $this->parseLongVersion($event, $previousEvent);
89
	}
90
91
	/**
92
	 * @param IEvent $event
93
	 * @return IEvent
94
	 * @throws \InvalidArgumentException
95
	 * @since 11.0.0
96
	 */
97
	public function parseShortVersion(IEvent $event) {
98
99 View Code Duplication
		if ($event->getSubject() === self::SUBJECT_ADDED) {
100
			$event->setParsedSubject($this->l->t('Added to favorites'));
101
			if ($this->activityManager->getRequirePNG()) {
102
				$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/starred.png')));
103
			} else {
104
				$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/starred.svg')));
105
			}
106
		} else if ($event->getSubject() === self::SUBJECT_REMOVED) {
107
			$event->setParsedSubject($this->l->t('Removed from favorites'));
108
			if ($this->activityManager->getRequirePNG()) {
109
				$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/star.png')));
110
			} else {
111
				$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/star.svg')));
112
			}
113
		} else {
114
			throw new \InvalidArgumentException();
115
		}
116
117
		return $event;
118
	}
119
120
	/**
121
	 * @param IEvent $event
122
	 * @param IEvent|null $previousEvent
123
	 * @return IEvent
124
	 * @throws \InvalidArgumentException
125
	 * @since 11.0.0
126
	 */
127
	public function parseLongVersion(IEvent $event, IEvent $previousEvent = null) {
128
129 View Code Duplication
		if ($event->getSubject() === self::SUBJECT_ADDED) {
130
			$subject = $this->l->t('You added {file} to your favorites');
131
			if ($this->activityManager->getRequirePNG()) {
132
				$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/starred.png')));
133
			} else {
134
				$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/starred.svg')));
135
			}
136
		} else if ($event->getSubject() === self::SUBJECT_REMOVED) {
137
			$subject = $this->l->t('You removed {file} from your favorites');
138
			if ($this->activityManager->getRequirePNG()) {
139
				$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/star.png')));
140
			} else {
141
				$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/star.svg')));
142
			}
143
		} else {
144
			throw new \InvalidArgumentException();
145
		}
146
147
		$this->setSubjects($event, $subject);
148
		$event = $this->eventMerger->mergeEvents('file', $event, $previousEvent);
149
		return $event;
150
	}
151
152
	/**
153
	 * @param IEvent $event
154
	 * @param string $subject
155
	 */
156
	protected function setSubjects(IEvent $event, $subject) {
157
		$subjectParams = $event->getSubjectParameters();
158
		if (empty($subjectParams)) {
159
			// Try to fall back to the old way, but this does not work for emails.
160
			// But at least old activities still work.
161
			$subjectParams = [
162
				'id' => $event->getObjectId(),
163
				'path' => $event->getObjectName(),
164
			];
165
		}
166
		$parameter = [
167
			'type' => 'file',
168
			'id' => $subjectParams['id'],
169
			'name' => basename($subjectParams['path']),
170
			'path' => trim($subjectParams['path'], '/'),
171
			'link' => $this->url->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $subjectParams['id']]),
172
		];
173
174
		$event->setParsedSubject(str_replace('{file}', $parameter['path'], $subject))
175
			->setRichSubject($subject, ['file' => $parameter]);
176
	}
177
}
178