Completed
Push — master ( 4a1ba1...57797d )
by Maxence
02:13
created

Provider::parseAsModerator()   A

Complexity

Conditions 4
Paths 9

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 18
rs 9.2
cc 4
eloc 12
nc 9
nop 3
1
<?php
2
3
/**
4
 * Circles - Bring cloud-users closer together.
5
 *
6
 * This file is licensed under the Affero General Public License version 3 or
7
 * later. See the COPYING file.
8
 *
9
 * @author Maxence Lange <[email protected]>
10
 * @copyright 2017
11
 * @license GNU AGPL version 3 or any later version
12
 *
13
 * This program is free software: you can redistribute it and/or modify
14
 * it under the terms of the GNU Affero General Public License as
15
 * published by the Free Software Foundation, either version 3 of the
16
 * License, or (at your option) any later version.
17
 *
18
 * This program is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
 * GNU Affero General Public License for more details.
22
 *
23
 * You should have received a copy of the GNU Affero General Public License
24
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25
 *
26
 */
27
28
namespace OCA\Circles\Activity;
29
30
use Exception;
31
use InvalidArgumentException;
32
use OCA\Circles\AppInfo\Application;
33
use OCA\Circles\Exceptions\FakeException;
34
use OCA\Circles\Model\Circle;
35
use OCA\Circles\Model\FederatedLink;
36
use OCA\Circles\Model\Member;
37
use OCA\Circles\Service\CirclesService;
38
use OCP\Activity\IEvent;
39
use OCP\Activity\IProvider;
40
use OpenCloud\Common\Exceptions\InvalidArgumentError;
41
42
class Provider extends SubjectProvider implements IProvider {
43
44
45
	/**
46
	 * @param string $lang
47
	 * @param IEvent $event
48
	 * @param IEvent|null $previousEvent
49
	 *
50
	 * @return IEvent
51
	 */
52
	public function parse($lang, IEvent $event, IEvent $previousEvent = null) {
53
54
		if ($event->getApp() !== Application::APP_NAME) {
55
			throw new \InvalidArgumentException();
56
		}
57
58
		try {
59
60
			$params = $event->getSubjectParameters();
61
			$circle = Circle::fromJSON($params['circle']);
62
63
			$this->setIcon($event, $circle);
64
65
			$this->parseAsMember($event, $circle, $params);
66
			$this->parseAsModerator($event, $circle, $params);
67
68
		} catch (FakeException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
69
		} catch (\Exception $e) {
70
			throw new \InvalidArgumentException();
71
		}
72
73
		$this->generateParsedSubject($event);
74
75
		return $event;
76
	}
77
78
79
	/**
80
	 * @param IEvent $event
81
	 * @param Circle $circle
82
	 */
83
	private function setIcon(IEvent &$event, Circle $circle) {
84
		$event->setIcon(
85
			CirclesService::getCircleIcon(
86
				$circle->getType(),
87
				(method_exists($this->activityManager, 'getRequirePNG')
88
				 && $this->activityManager->getRequirePNG())
89
			)
90
		);
91
	}
92
93
94
	/**
95
	 * @param Circle $circle
96
	 * @param IEvent $event
97
	 * @param array $params
98
	 */
99
	private function parseAsMember(IEvent &$event, Circle $circle, $params) {
100
		if ($event->getType() !== 'circles_as_member') {
101
			return;
102
		}
103
104
		switch ($event->getSubject()) {
105
			case 'circle_create':
106
				$this->parseCircleEvent(
107
					$event, $circle, null,
108
					$this->l10n->t('You created the circle {circle}'),
109
					$this->l10n->t('{author} created the circle {circle}')
110
				);
111
112
				return;
113
114
			case 'circle_delete':
115
				$this->parseCircleEvent(
116
					$event, $circle, null,
117
					$this->l10n->t('You deleted {circle}'),
118
					$this->l10n->t('{author} deleted {circle}')
119
				);
120
121
				return;
122
		}
123
124
		if (key_exists('member', $params)) {
125
			$this->parseMemberAsMember($event, $circle);
126
		}
127
	}
128
129
130
	/**
131
	 * @param Circle $circle
132
	 * @param IEvent $event
133
	 * @param array $params
134
	 *
135
	 * @throws Exception
136
	 */
137
	private function parseAsModerator(IEvent &$event, Circle $circle, $params) {
138
		if ($event->getType() !== 'circles_as_moderator') {
139
			return;
140
		}
141
142
		try {
143
			$this->parseMemberAsModerator($event, $circle, $params);
144
			$this->parseGroupAsModerator($event, $circle, $params);
145
			$this->parseLinkAsModerator($event, $circle, $params);
146
147
148
			throw new InvalidArgumentError();
149
		} catch (FakeException $e) {
150
			return;
151
		} catch (Exception $e) {
152
			throw $e;
153
		}
154
	}
155
156
157
	/**
158
	 * @param Circle $circle
159
	 * @param IEvent $event
160
	 *
161
	 * @return IEvent
162
	 */
163
	private function parseMemberAsMember(IEvent &$event, Circle $circle) {
164
		$params = $event->getSubjectParameters();
165
		$member = Member::fromJSON($params['member']);
166
167
		try {
168
			$this->parseSubjectMemberJoin($event, $circle, $member);
169
			$this->parseSubjectMemberAdd($event, $circle, $member);
170
			$this->parseSubjectMemberLeft($event, $circle, $member);
171
			$this->parseSubjectMemberRemove($event, $circle, $member);
172
		} catch (FakeException $e) {
173
			return $event;
174
		}
175
176
177
		return $event;
178
	}
179
180
181
	/**
182
	 * @param IEvent $event
183
	 * @param Circle $circle
184
	 * @param array $params
185
	 */
186
	private function parseGroupAsModerator(IEvent &$event, Circle $circle, $params) {
187
188
		if (!key_exists('group', $params)) {
189
			return;
190
		}
191
		$group = Member::fromJSON($params['group']);
192
193
		$this->parseGroupLink($event, $circle, $group);
194
		$this->parseGroupUnlink($event, $circle, $group);
195
		$this->parseGroupLevel($event, $circle, $group);
196
197
		throw new InvalidArgumentException();
198
	}
199
200
201
	/**
202
	 * @param IEvent $event
203
	 * @param Circle $circle
204
	 * @param array $params
205
	 */
206
	private function parseMemberAsModerator(IEvent &$event, Circle $circle, $params) {
207
208
		if (!key_exists('member', $params)) {
209
			return;
210
		}
211
		$member = Member::fromJSON($params['member']);
212
213
		$this->parseMemberInvited($event, $circle, $member);
214
		$this->parseMemberLevel($event, $circle, $member);
215
		$this->parseMemberRequestInvitation($event, $circle, $member);
216
		$this->parseMemberOwner($event, $circle, $member);
217
218
		throw new InvalidArgumentException();
219
	}
220
221
222
	/**
223
	 * @param IEvent $event
224
	 * @param Circle $circle
225
	 * @param array $params
226
	 */
227
	private function parseLinkAsModerator(IEvent &$event, Circle $circle, $params) {
228
229
		if (!key_exists('link', $params)) {
230
			return;
231
		}
232
233
		$remote = FederatedLink::fromJSON($params['link']);
234
235
		$this->parseLinkRequestSent($event, $circle, $remote);
236
		$this->parseLinkRequestReceived($event, $circle, $remote);
237
		$this->parseLinkRequestRejected($event, $circle, $remote);
238
		$this->parseLinkRequestCanceled($event, $circle, $remote);
239
		$this->parseLinkRequestAccepted($event, $circle, $remote);
240
		$this->parseLinkRequestRemoved($event, $circle, $remote);
241
		$this->parseLinkRequestCanceling($event, $circle, $remote);
242
		$this->parseLinkRequestAccepting($event, $circle, $remote);
243
		$this->parseLinkUp($event, $circle, $remote);
244
		$this->parseLinkDown($event, $circle, $remote);
245
		$this->parseLinkRemove($event, $circle, $remote);
246
247
		throw new InvalidArgumentException();
248
	}
249
250
251
}