Passed
Push — master ( b1a0d4...6df6fc )
by Roeland
10:53 queued 10s
created

Groups::parseLongVersion()   B

Complexity

Conditions 7
Paths 11

Size

Total Lines 25
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 19
nc 11
nop 1
dl 0
loc 25
rs 8.8333
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016 Joas Schilling <[email protected]>
4
 *
5
 * @author Joas Schilling <[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
24
namespace OCA\Files_Sharing\Activity\Providers;
25
26
use OCP\Activity\IEvent;
27
use OCP\Activity\IManager;
28
use OCP\IGroup;
29
use OCP\IGroupManager;
30
use OCP\IURLGenerator;
31
use OCP\IUserManager;
32
use OCP\L10N\IFactory;
33
34
class Groups extends Base {
35
36
	const SUBJECT_SHARED_GROUP_SELF = 'shared_group_self';
37
	const SUBJECT_RESHARED_GROUP_BY = 'reshared_group_by';
38
39
	const SUBJECT_UNSHARED_GROUP_SELF = 'unshared_group_self';
40
	const SUBJECT_UNSHARED_GROUP_BY = 'unshared_group_by';
41
42
	const SUBJECT_EXPIRED_GROUP = 'expired_group';
43
44
	/** @var IGroupManager */
45
	protected $groupManager;
46
47
	/** @var string[] */
48
	protected $groupDisplayNames = [];
49
50
	/**
51
	 * @param IFactory $languageFactory
52
	 * @param IURLGenerator $url
53
	 * @param IManager $activityManager
54
	 * @param IUserManager $userManager
55
	 * @param IGroupManager $groupManager
56
	 */
57
	public function __construct(IFactory $languageFactory, IURLGenerator $url, IManager $activityManager, IUserManager $userManager, IGroupManager $groupManager) {
58
		parent::__construct($languageFactory, $url, $activityManager, $userManager);
59
		$this->groupManager = $groupManager;
60
	}
61
62
	/**
63
	 * @param IEvent $event
64
	 * @return IEvent
65
	 * @throws \InvalidArgumentException
66
	 * @since 11.0.0
67
	 */
68
	public function parseShortVersion(IEvent $event) {
69
		$parsedParameters = $this->getParsedParameters($event);
70
71
		if ($event->getSubject() === self::SUBJECT_SHARED_GROUP_SELF) {
72
			$subject = $this->l->t('Shared with group {group}');
73
		} else if ($event->getSubject() === self::SUBJECT_UNSHARED_GROUP_SELF) {
74
			$subject = $this->l->t('Removed share for group {group}');
75
		} else if ($event->getSubject() === self::SUBJECT_RESHARED_GROUP_BY) {
76
			$subject = $this->l->t('{actor} shared with group {group}');
77
		} else if ($event->getSubject() === self::SUBJECT_UNSHARED_GROUP_BY) {
78
			$subject = $this->l->t('{actor} removed share for group {group}');
79
		} else if ($event->getSubject() === self::SUBJECT_EXPIRED_GROUP) {
80
			$subject = $this->l->t('Share for group {group} expired');
81
		} else {
82
			throw new \InvalidArgumentException();
83
		}
84
85
		if ($this->activityManager->getRequirePNG()) {
86
			$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.png')));
87
		} else {
88
			$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
89
		}
90
		$this->setSubjects($event, $subject, $parsedParameters);
91
92
		return $event;
93
	}
94
95
	/**
96
	 * @param IEvent $event
97
	 * @return IEvent
98
	 * @throws \InvalidArgumentException
99
	 * @since 11.0.0
100
	 */
101
	public function parseLongVersion(IEvent $event) {
102
		$parsedParameters = $this->getParsedParameters($event);
103
104
		if ($event->getSubject() === self::SUBJECT_SHARED_GROUP_SELF) {
105
			$subject = $this->l->t('You shared {file} with group {group}');
106
		} else if ($event->getSubject() === self::SUBJECT_UNSHARED_GROUP_SELF) {
107
			$subject = $this->l->t('You removed group {group} from {file}');
108
		} else if ($event->getSubject() === self::SUBJECT_RESHARED_GROUP_BY) {
109
			$subject = $this->l->t('{actor} shared {file} with group {group}');
110
		} else if ($event->getSubject() === self::SUBJECT_UNSHARED_GROUP_BY) {
111
			$subject = $this->l->t('{actor} removed group {group} from {file}');
112
		} else if ($event->getSubject() === self::SUBJECT_EXPIRED_GROUP) {
113
			$subject = $this->l->t('Share for file {file} with group {group} expired');
114
		} else {
115
			throw new \InvalidArgumentException();
116
		}
117
118
		if ($this->activityManager->getRequirePNG()) {
119
			$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.png')));
120
		} else {
121
			$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
122
		}
123
		$this->setSubjects($event, $subject, $parsedParameters);
124
125
		return $event;
126
	}
127
128
	protected function getParsedParameters(IEvent $event) {
129
		$subject = $event->getSubject();
130
		$parameters = $event->getSubjectParameters();
131
132
		switch ($subject) {
133
			case self::SUBJECT_RESHARED_GROUP_BY:
134
			case self::SUBJECT_UNSHARED_GROUP_BY:
135
				return [
136
					'file' => $this->getFile($parameters[0], $event),
137
					'group' => $this->generateGroupParameter($parameters[2]),
138
					'actor' => $this->getUser($parameters[1]),
139
				];
140
			case self::SUBJECT_SHARED_GROUP_SELF:
141
			case self::SUBJECT_UNSHARED_GROUP_SELF:
142
			case self::SUBJECT_EXPIRED_GROUP:
143
				return [
144
					'file' => $this->getFile($parameters[0], $event),
145
					'group' => $this->generateGroupParameter($parameters[1]),
146
				];
147
		}
148
		return [];
149
	}
150
151
	/**
152
	 * @param string $gid
153
	 * @return array
154
	 */
155
	protected function generateGroupParameter($gid) {
156
		if (!isset($this->groupDisplayNames[$gid])) {
157
			$this->groupDisplayNames[$gid] = $this->getGroupDisplayName($gid);
158
		}
159
160
		return [
161
			'type' => 'user-group',
162
			'id' => $gid,
163
			'name' => $this->groupDisplayNames[$gid],
164
		];
165
	}
166
167
	/**
168
	 * @param string $gid
169
	 * @return string
170
	 */
171
	protected function getGroupDisplayName($gid) {
172
		$group = $this->groupManager->get($gid);
173
		if ($group instanceof IGroup) {
0 ignored issues
show
introduced by
$group is always a sub-type of OCP\IGroup.
Loading history...
174
			return $group->getDisplayName();
175
		}
176
		return $gid;
177
	}
178
}
179