Completed
Push — master ( 9de15f...4a54fd )
by Maxence
02:21
created

Provider::parseMemberAsModerator()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 41
Code Lines 28

Duplication

Lines 14
Ratio 34.15 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 14
loc 41
rs 8.439
cc 5
eloc 28
nc 5
nop 2
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\Model\Circle;
33
use OCA\Circles\Model\FederatedLink;
34
use OCA\Circles\Model\Member;
35
use OCA\Circles\Service\CirclesService;
36
use OCP\Activity\IEvent;
37
use OCP\Activity\IProvider;
38
use OpenCloud\Common\Exceptions\InvalidArgumentError;
39
40
class Provider extends BaseProvider implements IProvider {
41
42
43
	/**
44
	 * @param string $lang
45
	 * @param IEvent $event
46
	 * @param IEvent|null $previousEvent
47
	 *
48
	 * @return IEvent
49
	 */
50
	public function parse($lang, IEvent $event, IEvent $previousEvent = null) {
51
52
		if ($event->getApp() !== 'circles') {
53
			throw new \InvalidArgumentException();
54
		}
55
56
		try {
57
58
			$params = $event->getSubjectParameters();
59
			$circle = Circle::fromJSON($this->l10n, $params['circle']);
0 ignored issues
show
Deprecated Code introduced by
The method OCA\Circles\Model\Circle::fromJSON() has been deprecated.

This method has been deprecated.

Loading history...
60
61
			$this->setIcon($event, $circle);
62
			$this->parseAsMember($event, $circle, $params);
63
			$this->parseAsModerator($event, $circle, $params);
64
			$this->generateParsedSubject($event);
65
66
			return $event;
67
		} catch (\Exception $e) {
68
			throw new \InvalidArgumentException();
69
		}
70
	}
71
72
73
	private function setIcon(IEvent &$event, Circle $circle) {
74
		$event->setIcon(
75
			CirclesService::getCircleIcon(
76
				$circle->getType(),
77
				(method_exists($this->activityManager, 'getRequirePNG')
78
				 && $this->activityManager->getRequirePNG())
79
			)
80
		);
81
	}
82
83
	/**
84
	 * @param Circle $circle
85
	 * @param IEvent $event
86
	 * @param array $params
87
	 *
88
	 * @return IEvent
89
	 */
90
	private function parseAsMember(IEvent &$event, Circle $circle, $params) {
91
		if ($event->getType() !== 'circles_as_member') {
92
			return $event;
93
		}
94
95
		switch ($event->getSubject()) {
96
			case 'circle_create':
97
				return $this->parseCircleEvent(
98
					$event, $circle, null,
99
					$this->l10n->t('You created the circle {circle}'),
100
					$this->l10n->t('{author} created the circle {circle}')
101
				);
102
103
			case 'circle_delete':
104
				return $this->parseCircleEvent(
105
					$event, $circle, null,
106
					$this->l10n->t('You deleted {circle}'),
107
					$this->l10n->t('{author} deleted {circle}')
108
				);
109
		}
110
111
		if (key_exists('member', $params)) {
112
			$this->parseMemberAsMember($event, $circle);
113
		}
114
115
		return $event;
116
	}
117
118
119
	/**
120
	 * @param Circle $circle
121
	 * @param IEvent $event
122
	 * @param array $params
123
	 *
124
	 * @return IEvent
125
	 * @throws Exception
126
	 */
127
	private function parseAsModerator(IEvent &$event, Circle $circle, $params) {
128
		if ($event->getType() !== 'circles_as_moderator') {
129
			return $event;
130
		}
131
132
		try {
133
			if (key_exists('member', $params)) {
134
				return $this->parseMemberAsModerator($event, $circle);
135
			}
136
137
			if (key_exists('group', $params)) {
138
				return $this->parseGroupAsModerator($event, $circle);
139
			}
140
141
			if (key_exists('link', $params)) {
142
				return $this->parseLinkAsModerator($event, $circle);
143
			}
144
145
			throw new InvalidArgumentError();
146
		} catch (Exception $e) {
147
			throw $e;
148
		}
149
	}
150
151
152
	/**
153
	 * @param Circle $circle
154
	 * @param IEvent $event
155
	 *
156
	 * @return IEvent
157
	 */
158
	private function parseMemberAsMember(IEvent &$event, Circle $circle) {
159
		$params = $event->getSubjectParameters();
160
		$member = Member::fromJSON($params['member']);
161
162
		switch ($event->getSubject()) {
163
			case 'member_join':
164
				return $this->parseSubjectMemberJoin($event, $circle, $member);
165
166
			case 'member_add':
167
				return $this->parseSubjectMemberAdd($event, $circle, $member);
168
169
			case 'member_left':
170
				return $this->parseSubjectMemberLeft($event, $circle, $member);
171
172
			case 'member_remove':
173
				return $this->parseSubjectMemberRemove($event, $circle, $member);
174
		}
175
176
		return $event;
177
	}
178
179
180
	/**
181
	 * Parse on Subject 'member_join'.
182
	 * If circle is closed, we say that user accepted his invitation.
183
	 *
184
	 * @param IEvent $event
185
	 * @param Circle $circle
186
	 * @param Member $member
187
	 *
188
	 * @return IEvent
189
	 */
190 View Code Duplication
	private function parseSubjectMemberJoin(IEvent &$event, Circle $circle, Member $member) {
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...
191
		if ($circle->getType() === Circle::CIRCLES_CLOSED) {
192
			return $this->parseCircleMemberEvent(
193
				$event, $circle, $member,
194
				$this->l10n->t('You accepted the invitation to join {circle}'),
195
				$this->l10n->t('{member} accepted the invitation to join {circle}')
196
			);
197
		} else {
198
			return $this->parseCircleMemberEvent(
199
				$event, $circle, $member,
200
				$this->l10n->t('You joined {circle}'),
201
				$this->l10n->t('{member} joined {circle}')
202
			);
203
		}
204
	}
205
206
207
	/**
208
	 * Parse on Subject 'member_add'.
209
	 * If circle is closed, we say that user's invitation was accepted.
210
	 *
211
	 * @param IEvent $event
212
	 * @param Circle $circle
213
	 * @param Member $member
214
	 *
215
	 * @return IEvent
216
	 */
217 View Code Duplication
	private function parseSubjectMemberAdd(IEvent &$event, Circle $circle, Member $member) {
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...
218
		if ($circle->getType() === Circle::CIRCLES_CLOSED) {
219
			return $this->parseCircleMemberAdvancedEvent(
220
				$event, $circle, $member,
221
				$this->l10n->t("You accepted {member}'s request to join {circle}"),
222
				$this->l10n->t('Your request to join {circle} has been accepted by {author}'),
223
				$this->l10n->t("{member}'s request to join {circle} has been accepted by {author}")
224
			);
225
		} else {
226
			return $this->parseCircleMemberAdvancedEvent(
227
				$event, $circle, $member,
228
				$this->l10n->t('You added {member} as member to {circle}'),
229
				$this->l10n->t('You have been added as member to {circle} by {author}'),
230
				$this->l10n->t('{member} has been added as member to {circle} by {author}')
231
			);
232
		}
233
	}
234
235
236
	/**
237
	 * Parse on Subject 'member_left'.
238
	 * If circle is closed and member was not a real member, we send him to
239
	 * parseSubjectNonMemberLeftClosedCircle();
240
	 *
241
	 * @param IEvent $event
242
	 * @param Circle $circle
243
	 * @param Member $member
244
	 *
245
	 * @return IEvent
246
	 */
247
	private function parseSubjectMemberLeft(IEvent &$event, Circle $circle, Member $member) {
248
		if ($circle->getType() === Circle::CIRCLES_CLOSED
249
			&& $member->getLevel() === Member::LEVEL_NONE) {
250
			return $this->parseSubjectNonMemberLeftClosedCircle($event, $circle, $member);
251
		} else {
252
			return $this->parseCircleMemberEvent(
253
				$event, $circle, $member,
254
				$this->l10n->t('You left {circle}'),
255
				$this->l10n->t('{member} left {circle}')
256
			);
257
		}
258
	}
259
260
261
	/**
262
	 * Parse on Subject 'member_left' on a closed circle when user were not yet a member.
263
	 * If status is Invited we say that member rejected his invitation.
264
	 * If status is Requested we say he dismissed his request.
265
	 *
266
	 * @param IEvent $event
267
	 * @param Circle $circle
268
	 * @param Member $member
269
	 *
270
	 * @return IEvent
271
	 */
272 View Code Duplication
	private function parseSubjectNonMemberLeftClosedCircle(
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...
273
		IEvent &$event, Circle $circle, Member $member
274
	) {
275
		if ($member->getStatus() === Member::STATUS_INVITED) {
276
			return $this->parseCircleMemberEvent(
277
				$event, $circle, $member,
278
				$this->l10n->t("You declined the invitation to join {circle}"),
279
				$this->l10n->t("{member} declined an invitation to join {circle}")
280
			);
281
		}
282
283
		return $this->parseCircleMemberEvent(
284
			$event, $circle, $member,
285
			$this->l10n->t("You cancelled your request to join {circle}"),
286
			$this->l10n->t("{member} cancelled his request to join {circle}")
287
		);
288
	}
289
290
291
	/**
292
	 * Parse on Subject 'member_remove'.
293
	 * If circle is closed and member was not a real member, we send him to
294
	 * parseSubjectNonMemberRemoveClosedCircle();
295
	 *
296
	 * @param IEvent $event
297
	 * @param Circle $circle
298
	 * @param Member $member
299
	 *
300
	 * @return IEvent
301
	 */
302 View Code Duplication
	private function parseSubjectMemberRemove(IEvent &$event, Circle $circle, Member $member) {
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...
303
		if ($circle->getType() === Circle::CIRCLES_CLOSED
304
			&& $member->getLevel() === Member::LEVEL_NONE) {
305
			return $this->parseSubjectNonMemberRemoveClosedCircle($event, $circle, $member);
306
307
		} else {
308
			return $this->parseCircleMemberAdvancedEvent(
309
				$event, $circle, $member,
310
				$this->l10n->t('You removed {member} from {circle}'),
311
				$this->l10n->t('You have been removed from {circle} by {author}'),
312
				$this->l10n->t('{member} has been removed from {circle} by {author}')
313
			);
314
		}
315
	}
316
317
318
	/**
319
	 * Parse on Subject 'member_remove' on a closed circle when user were not yet a member.
320
	 * If status is Invited we say that author cancelled his invitation.
321
	 * If status is Requested we say that his invitation was rejected.
322
	 *
323
	 * @param IEvent $event
324
	 * @param Circle $circle
325
	 * @param Member $member
326
	 *
327
	 * @return IEvent
328
	 */
329 View Code Duplication
	private function parseSubjectNonMemberRemoveClosedCircle(
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...
330
		IEvent &$event, Circle $circle, Member $member
331
	) {
332
		if ($member->getStatus() === Member::STATUS_REQUEST) {
333
			return $this->parseCircleMemberAdvancedEvent(
334
				$event, $circle, $member,
335
				$this->l10n->t("You dismissed {member}'s request to join {circle}"),
336
				$this->l10n->t('Your request to join {circle} has been dismissed by {author}'),
337
				$this->l10n->t("{member}'s request to join {circle} has been dismissed by {author}")
338
			);
339
		}
340
341
		return $this->parseCircleMemberAdvancedEvent(
342
			$event, $circle, $member,
343
			$this->l10n->t("You cancelled {member}'s invitation to join {circle}"),
344
			$this->l10n->t('Your invitation to join {circle} has been cancelled by {author}'),
345
			$this->l10n->t("{author} cancelled {member}'s invitation to join {circle}")
346
		);
347
	}
348
349
350
	/**
351
	 * @param Circle $circle
352
	 * @param IEvent $event
353
	 *
354
	 * @return IEvent
355
	 */
356
	private function parseGroupAsModerator(IEvent &$event, Circle $circle) {
357
358
		$params = $event->getSubjectParameters();
359
		$group = Member::fromJSON($params['group']);
360
361
		switch ($event->getSubject()) {
362
363 View Code Duplication
			case 'group_link':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
364
				return $this->parseCircleMemberEvent(
365
					$event, $circle, $group,
366
					$this->l10n->t('You linked {group} to {circle}'),
367
					$this->l10n->t('{group} has been linked to {circle} by {author}')
368
				);
369
370 View Code Duplication
			case 'group_unlink':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
371
				return $this->parseCircleMemberEvent(
372
					$event, $circle, $group,
373
					$this->l10n->t('You unlinked {group} from {circle}'),
374
					$this->l10n->t('{group} has been unlinked from {circle} by {author}')
375
				);
376
377
			case 'group_level':
378
				$level = [$this->l10n->t($group->getLevelString())];
379
380
				return $this->parseCircleMemberEvent(
381
					$event, $circle, $group,
382
					$this->l10n->t(
383
						'You changed the level of the linked group {group} in {circle} to %1$s',
384
						$level
385
					),
386
					$this->l10n->t(
387
						'{author} changed the level of the linked group {group} in {circle} to %1$s',
388
						$level
389
					)
390
				);
391
		}
392
393
		throw new InvalidArgumentException();
394
	}
395
396
397
	/**
398
	 * @param Circle $circle
399
	 * @param IEvent $event
400
	 *
401
	 * @return IEvent
402
	 */
403
	private function parseMemberAsModerator(IEvent &$event, Circle $circle) {
404
405
		$params = $event->getSubjectParameters();
406
		$member = Member::fromJSON($params['member']);
407
408
		switch ($event->getSubject()) {
409
			case 'member_invited':
410
				return $this->parseCircleMemberAdvancedEvent(
411
					$event, $circle, $member,
412
					$this->l10n->t('You invited {member} to join {circle}'),
413
					$this->l10n->t('You have been invited to join {circle} by {author}'),
414
					$this->l10n->t('{member} has been invited to join {circle} by {author}')
415
				);
416
417
			case 'member_level':
418
				$level = [$this->l10n->t($member->getLevelString())];
419
420
				return $this->parseCircleMemberAdvancedEvent(
421
					$event, $circle, $member,
422
					$this->l10n->t('You changed {member}\'s level in {circle} to %1$s', $level),
423
					$this->l10n->t('{author} changed your level in {circle} to %1$s', $level),
424
					$this->l10n->t('{author} changed {member}\'s level in {circle} to %1$s', $level)
425
				);
426
427 View Code Duplication
			case 'member_request_invitation':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
428
				return $this->parseMemberEvent(
429
					$event, $circle, $member,
430
					$this->l10n->t('You sent a request to join {circle}'),
431
					$this->l10n->t('{member} sent a request to join {circle}')
432
				);
433
434 View Code Duplication
			case 'member_owner':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
435
				return $this->parseMemberEvent(
436
					$event, $circle, $member,
437
					$this->l10n->t('You are the new owner of {circle}'),
438
					$this->l10n->t('{member} is the new owner of {circle}')
439
				);
440
		}
441
442
		throw new InvalidArgumentException();
443
	}
444
445
446
	/**
447
	 * @param Circle $circle
448
	 * @param IEvent $event
449
	 *
450
	 * @return IEvent
451
	 */
452
	private function parseLinkAsModerator(IEvent &$event, Circle $circle) {
453
454
		$params = $event->getSubjectParameters();
455
		$remote = FederatedLink::fromJSON($params['link']);
456
457
		switch ($event->getSubject()) {
458 View Code Duplication
			case 'link_request_sent':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
459
				return $this->parseCircleEvent(
460
					$event, $circle, $remote,
461
					$this->l10n->t('You sent a request to link {circle} with {remote}'),
462
					$this->l10n->t('{author} sent a request to link {circle} with {remote}')
463
				);
464
465
			case 'link_request_received';
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
466
				return $this->parseLinkEvent(
467
					$event, $circle, $remote,
468
					$this->l10n->t('{remote} requested a link with {circle}')
469
				);
470
471
			case 'link_request_rejected';
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
472
				return $this->parseLinkEvent(
473
					$event, $circle, $remote, $this->l10n->t(
474
					'The request to link {circle} with {remote} has been rejected'
475
				)
476
				);
477
478
			case 'link_request_canceled':
479
				return $this->parseLinkEvent(
480
					$event, $circle, $remote,
481
					$this->l10n->t(
482
						'The request to link {remote} with {circle} has been canceled remotely'
483
					)
484
				);
485
486
			case 'link_request_accepted':
487
				return $this->parseLinkEvent(
488
					$event, $circle, $remote,
489
					$this->l10n->t('The request to link {circle} with {remote} has been accepted')
490
				);
491
492 View Code Duplication
			case 'link_request_removed':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
493
				return $this->parseCircleEvent(
494
					$event, $circle, $remote,
495
					$this->l10n->t('You dismissed the request to link {remote} with {circle}'),
496
					$this->l10n->t('{author} dismissed the request to link {remote} with {circle}')
497
				);
498
499 View Code Duplication
			case 'link_request_canceling':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
500
				return $this->parseCircleEvent(
501
					$event, $circle, $remote,
502
					$this->l10n->t('You canceled the request to link {circle} with {remote}'),
503
					$this->l10n->t('{author} canceled the request to link {circle} with {remote}')
504
				);
505
506 View Code Duplication
			case 'link_request_accepting':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
507
				return $this->parseCircleEvent(
508
					$event, $circle, $remote,
509
					$this->l10n->t('You accepted the request to link {remote} with {circle}'),
510
					$this->l10n->t('{author} accepted the request to link {remote} with {circle}')
511
				);
512
513
			case 'link_up':
514
				return $this->parseLinkEvent(
515
					$event, $circle, $remote,
516
					$this->l10n->t('A link between {circle} and {remote} is now up and running')
517
				);
518
519
			case 'link_down':
520
				return $this->parseLinkEvent(
521
					$event, $circle, $remote,
522
					$this->l10n->t(
523
						'The link between {circle} and {remote} has been shutdown remotely'
524
					)
525
				);
526
527 View Code Duplication
			case 'link_remove':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
528
				return $this->parseCircleEvent(
529
					$event, $circle, $remote,
530
					$this->l10n->t('You closed the link between {circle} and {remote}'),
531
					$this->l10n->t('{author} closed the link between {circle} and {remote}')
532
				);
533
		}
534
535
		throw new InvalidArgumentException();
536
	}
537
538
}
539