Completed
Push — master ( 6b7b93...3c2c59 )
by Maxence
02:59
created

Provider::parseActivityHeader()   B

Complexity

Conditions 3
Paths 2

Size

Total Lines 42
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 42
rs 8.8571
cc 3
eloc 23
nc 2
nop 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A Provider::generateMemberParameter() 0 5 1
A Provider::generateCircleParameter() 0 10 1
A Provider::generateUserParameter() 0 9 1
1
<?php
2
3
4
namespace OCA\Circles\Activity;
5
6
7
use Exception;
8
use InvalidArgumentException;
9
use OCA\Circles\Api\v1\Circles;
10
use OCA\Circles\Model\Circle;
11
use OCA\Circles\Model\Member;
12
use OCA\Circles\Model\SharingFrame;
13
use OCA\Circles\Service\CirclesService;
14
use OCA\Circles\Service\MiscService;
15
use OCP\Activity\IEvent;
16
use OCP\Activity\IManager;
17
use OCP\Activity\IProvider;
18
use OCP\IL10N;
19
use OCP\IURLGenerator;
20
21
22
class Provider implements IProvider {
23
24
	/** @var MiscService */
25
	protected $miscService;
26
27
	/** @var IL10N */
28
	protected $l10n;
29
30
	/** @var IURLGenerator */
31
	protected $url;
32
33
	/** @var IManager */
34
	protected $activityManager;
35
36
	public function __construct(
37
		IURLGenerator $url, IManager $activityManager, IL10N $l10n, MiscService $miscService
38
	) {
39
		$this->url = $url;
40
		$this->activityManager = $activityManager;
41
		$this->l10n = $l10n;
42
		$this->miscService = $miscService;
43
	}
44
45
46
	/**
47
	 * @param string $lang
48
	 * @param IEvent $event
49
	 * @param IEvent|null $previousEvent
50
	 *
51
	 * @return IEvent
52
	 */
53
	public function parse($lang, IEvent $event, IEvent $previousEvent = null) {
54
55
		if ($event->getApp() !== 'circles') {
56
			throw new \InvalidArgumentException();
57
		}
58
59
		$event = $this->parseAsMember($lang, $event);
60
		$event = $this->parseAsModerator($lang, $event);
61
62
		return $event;
63
	}
64
65
66
	/**
67
	 * @param string $lang
68
	 * @param IEvent $event
69
	 *
70
	 * @return IEvent
71
	 * @throws Exception
72
	 */
73
	private function parseAsMember($lang, IEvent $event) {
74
		if ($event->getType() !== 'circles_as_member') {
75
			return $event;
76
		}
77
78
		$params = $event->getSubjectParameters();
79
		$circle = Circle::fromJSON($this->l10n, $params['circle']);
80
		if ($circle === null) {
81
			return $event;
82
		}
83
84
		$event->setIcon(CirclesService::getCircleIcon($circle->getType()));
85
		switch ($event->getSubject()) {
86
			case 'circle_create':
87
				return $this->parseCircleCreate($lang, $circle, $event);
88
89
			case 'circle_delete':
90
				return $this->parseCircleDelete($lang, $circle, $event);
91
		}
92
93
		$event = $this->parseMembersAsMember($lang, $circle, $event);
94
95
		return $event;
96
	}
97
98
99
	/**
100
	 * @param $lang
101
	 * @param Circle $circle
102
	 * @param IEvent $event
103
	 *
104
	 * @return IEvent
105
	 */
106
	private function parseMembersAsMember($lang, Circle $circle, IEvent $event) {
107
		$params = $event->getSubjectParameters();
108
109
		$member = Member::fromJSON($this->l10n, $params['member']);
110
		if ($member === null) {
111
			return $event;
112
		}
113
114
		switch ($event->getSubject()) {
115
			case 'member_join':
116
				return $this->parseMemberJoin($lang, $circle, $member, $event);
117
118
			case 'member_add':
119
				return $this->parseMemberAdd($lang, $circle, $member, $event);
120
121
			case 'member_left':
122
				return $this->parseMemberLeft($lang, $circle, $member, $event);
123
124
			case 'member_remove':
125
				return $this->parseMemberRemove($lang, $circle, $member, $event);
126
127
			default:
128
				return $event;
129
		}
130
131
	}
132
133
134
	/**
135
	 * @param string $lang
136
	 * @param IEvent $event
137
	 *
138
	 * @return IEvent
139
	 */
140
	private function parseAsModerator($lang, IEvent $event) {
141
		if ($event->getType() !== 'circles_as_moderator') {
142
			return $event;
143
		}
144
145
		$params = $event->getSubjectParameters();
146
		$circle = Circle::fromJSON($this->l10n, $params['circle']);
147
		$member = Member::fromJSON($this->l10n, $params['member']);
148
		if ($member === null || $circle === null) {
149
			return $event;
150
		}
151
152
		$event->setIcon(CirclesService::getCircleIcon($circle->getType()));
153
154
		switch ($event->getSubject()) {
155
			case 'member_invited':
156
				return $this->parseMemberInvited($lang, $circle, $member, $event);
157
158
			case 'member_request_invitation':
159
				return $this->parseMemberRequestInvitation($lang, $circle, $member, $event);
160
161
			case 'member_level':
162
				return $this->parseMemberLevel($lang, $circle, $member, $event);
163
164
			case 'member_owner':
165
				return $this->parseMemberOwner($lang, $circle, $member, $event);
166
167
			default:
168
				throw new InvalidArgumentException();
169
		}
170
	}
171
172
	/**
173
	 * @param string $lang
174
	 * @param Circle $circle
175
	 * @param IEvent $event
176
	 *
177
	 * @return IEvent
178
	 */
179
	private function parseCircleCreate($lang, Circle $circle, IEvent $event) {
180
		if ($circle->getOwner()
181
				   ->getUserId() === $this->activityManager->getCurrentUserId()
182
		) {
183
			$event->setRichSubject(
184
				$this->l10n->t('You created the circle {circle}'),
185
				['circle' => $this->generateCircleParameter($circle)]
186
			);
187
188
		} else {
189
			$event->setRichSubject(
190
				$this->l10n->t('{author} created the circle {circle}'),
191
				[
192
					'author' => $author = $this->generateUserParameter(
193
						$circle->getOwner()
194
							   ->getUserId()
195
					),
196
					'circle' => $this->generateCircleParameter($circle)
197
				]
198
			);
199
		}
200
201
		return $event;
202
	}
203
204
205
	/**
206
	 * @param string $lang
207
	 * @param Circle $circle
208
	 * @param IEvent $event
209
	 *
210
	 * @return IEvent
211
	 */
212
	private function parseCircleDelete($lang, Circle $circle, IEvent $event) {
213
		if ($circle->getOwner()
214
				   ->getUserId() === $this->activityManager->getCurrentUserId()
215
		) {
216
			$event->setRichSubject(
217
				$this->l10n->t('You deleted {circle}'),
218
				['circle' => $this->generateCircleParameter($circle)]
219
			);
220
		} else {
221
			$event->setRichSubject(
222
				$this->l10n->t('{author} deleted {circle}'),
223
				[
224
					'author' => $this->generateUserParameter(
225
						$circle->getOwner()
226
							   ->getUserId()
227
					),
228
					'circle' => $this->generateCircleParameter($circle)
229
				]
230
			);
231
		}
232
233
		return $event;
234
	}
235
236
237
	/**
238
	 * @param string $lang
239
	 * @param Circle $circle
240
	 * @param Member $member
241
	 * @param IEvent $event
242
	 *
243
	 * @return IEvent
244
	 */
245 View Code Duplication
	private function parseMemberInvited($lang, Circle $circle, Member $member, IEvent $event) {
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...
246
247
		if ($circle->getUser()
248
				   ->getUserId() === $this->activityManager->getCurrentUserId()
249
		) {
250
			$event->setRichSubject(
251
				$this->l10n->t('You invited {member} into {circle}'),
252
				[
253
					'circle' => $this->generateCircleParameter($circle),
254
					'member' => $this->generateMemberParameter($member)
255
				]
256
			);
257
258
		} elseif ($member->getUserId() === $this->activityManager->getCurrentUserId()) {
259
			$event->setRichSubject(
260
				$this->l10n->t('You have been invited into {circle} by {author}'),
261
				[
262
					'author' => $this->generateUserParameter(
263
						$circle->getUser()
264
							   ->getUserId()
265
					),
266
					'circle' => $this->generateCircleParameter($circle)
267
				]
268
			);
269
270
		} else {
271
			$event->setRichSubject(
272
				$this->l10n->t('{member} have been invited into {circle} by {author}'),
273
				[
274
					'author' => $this->generateUserParameter(
275
						$circle->getUser()
276
							   ->getUserId()
277
					),
278
					'circle' => $this->generateCircleParameter($circle),
279
					'member' => $this->generateMemberParameter($member)
280
				]
281
			);
282
		}
283
284
		return $event;
285
	}
286
287
288
	/**
289
	 * @param $lang
290
	 * @param Circle $circle
291
	 * @param Member $member
292
	 * @param IEvent $event
293
	 *
294
	 * @return IEvent
295
	 */
296 View Code Duplication
	private function parseMemberRequestInvitation(
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...
297
		$lang, Circle $circle, Member $member, IEvent $event
298
	) {
299
		if ($member->getUserId() === $this->activityManager->getCurrentUserId()) {
300
			$event->setRichSubject(
301
				$this->l10n->t('You requested an invitation to {circle}'),
302
				['circle' => $this->generateCircleParameter($circle)]
303
			);
304
305
		} else {
306
			$event->setRichSubject(
307
				$this->l10n->t(
308
					'{author} has requested an invitation into {circle}'
309
				), [
310
					'author' => $this->generateMemberParameter($member),
311
					'circle' => $this->generateCircleParameter($circle)
312
				]
313
			);
314
		}
315
316
		return $event;
317
	}
318
319
320
	/**
321
	 * @param $lang
322
	 * @param Circle $circle
323
	 * @param Member $member
324
	 * @param IEvent $event
325
	 *
326
	 * @return IEvent
327
	 */
328 View Code Duplication
	private function parseMemberJoin($lang, Circle $circle, Member $member, IEvent $event) {
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...
329
		if ($circle->getUser()
330
				   ->getUserId() === $this->activityManager->getCurrentUserId()
331
		) {
332
			$event->setRichSubject(
333
				$this->l10n->t('You joined {circle}'),
334
				['circle' => $this->generateCircleParameter($circle)]
335
			);
336
337
		} else {
338
			$event->setRichSubject(
339
				$this->l10n->t(
340
					'{member} has joined the circle {circle}'
341
				), [
342
					'circle' => $this->generateCircleParameter($circle),
343
					'member' => $this->generateMemberParameter($member)
344
				]
345
			);
346
		}
347
348
		return $event;
349
	}
350
351
352
	/**
353
	 * @param $lang
354
	 * @param Circle $circle
355
	 * @param Member $member
356
	 * @param IEvent $event
357
	 *
358
	 * @return IEvent
359
	 */
360 View Code Duplication
	private function parseMemberAdd($lang, Circle $circle, Member $member, IEvent $event) {
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...
361
362
		if ($circle->getUser()
363
				   ->getUserId() === $this->activityManager->getCurrentUserId()
364
		) {
365
			$event->setRichSubject(
366
				$this->l10n->t('You added {member} as member to {circle}'),
367
				[
368
					'circle' => $this->generateCircleParameter($circle),
369
					'member' => $this->generateMemberParameter($member)
370
				]
371
			);
372
373
		} elseif ($member->getUserId() === $this->activityManager->getCurrentUserId()) {
374
			$event->setRichSubject(
375
				$this->l10n->t('You were added as member to {circle} by {author}'),
376
				[
377
					'author' => $this->generateUserParameter(
378
						$circle->getUser()
379
							   ->getUserId()
380
					),
381
					'circle' => $this->generateCircleParameter($circle)
382
				]
383
			);
384
385
		} else {
386
			$event->setRichSubject(
387
				$this->l10n->t(
388
					'{member} was added as member to {circle} by {author}'
389
				), [
390
					'author' => $this->generateUserParameter(
391
						$circle->getUser()
392
							   ->getUserId()
393
					),
394
					'circle' => $this->generateCircleParameter($circle),
395
					'member' => $this->generateMemberParameter($member)
396
				]
397
			);
398
		}
399
400
		return $event;
401
	}
402
403
404
	/**
405
	 * @param $lang
406
	 * @param Circle $circle
407
	 * @param Member $member
408
	 * @param IEvent $event
409
	 *
410
	 * @return IEvent
411
	 */
412 View Code Duplication
	private function parseMemberLeft($lang, Circle $circle, Member $member, IEvent $event) {
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...
413
		if ($circle->getUser()
414
				   ->getUserId() === $this->activityManager->getCurrentUserId()
415
		) {
416
			$event->setRichSubject(
417
				$this->l10n->t('You left {circle}'),
418
				['circle' => $this->generateCircleParameter($circle)]
419
			);
420
421
		} else {
422
			$event->setRichSubject(
423
				$this->l10n->t(
424
					'{member} has left {circle}'
425
				), [
426
					'circle' => $this->generateCircleParameter($circle),
427
					'member' => $this->generateMemberParameter($member)
428
				]
429
			);
430
		}
431
432
		return $event;
433
	}
434
435
436
	/**
437
	 * @param $lang
438
	 * @param Circle $circle
439
	 * @param Member $member
440
	 * @param IEvent $event
441
	 *
442
	 * @return IEvent
443
	 */
444 View Code Duplication
	private function parseMemberRemove($lang, Circle $circle, Member $member, IEvent $event) {
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...
445
446
		if ($circle->getUser()
447
				   ->getUserId() === $this->activityManager->getCurrentUserId()
448
		) {
449
			$event->setRichSubject(
450
				$this->l10n->t('You removed {member} from {circle}'),
451
				[
452
					'circle' => $this->generateCircleParameter($circle),
453
					'member' => $this->generateMemberParameter($member)
454
				]
455
			);
456
457
		} elseif ($member->getUserId() === $this->activityManager->getCurrentUserId()) {
458
			$event->setRichSubject(
459
				$this->l10n->t('You were removed from {circle} by {author}'),
460
				[
461
					'author' => $this->generateUserParameter(
462
						$circle->getUser()
463
							   ->getUserId()
464
					),
465
					'circle' => $this->generateCircleParameter($circle)
466
				]
467
			);
468
469
		} else {
470
			$event->setRichSubject(
471
				$this->l10n->t(
472
					'{member} was removed from {circle} by {author}'
473
				), [
474
					'author' => $this->generateUserParameter(
475
						$circle->getUser()
476
							   ->getUserId()
477
					),
478
					'circle' => $this->generateCircleParameter($circle),
479
					'member' => $this->generateMemberParameter($member)
480
				]
481
			);
482
		}
483
484
		return $event;
485
	}
486
487
488
	/**
489
	 * @param $lang
490
	 * @param Circle $circle
491
	 * @param Member $member
492
	 * @param IEvent $event
493
	 *
494
	 * @return IEvent
495
	 */
496
	private function parseMemberLevel($lang, Circle $circle, Member $member, IEvent $event) {
497
498
		if ($circle->getUser()
499
				   ->getUserId() === $this->activityManager->getCurrentUserId()
500
		) {
501
			$event->setRichSubject(
502
				$this->l10n->t(
503
					'You changed {member}\'s level in {circle} to %1$s',
504
					[$this->l10n->t($member->getLevelString())]
505
				),
506
				[
507
					'circle' => $this->generateCircleParameter($circle),
508
					'member' => $this->generateMemberParameter($member)
509
				]
510
			);
511
512
		} elseif ($member->getUserId() === $this->activityManager->getCurrentUserId()) {
513
			$event->setRichSubject(
514
				$this->l10n->t(
515
					'{author} changed your level in {circle} to %1$s',
516
					[$this->l10n->t($member->getLevelString())]
517
				),
518
				[
519
					'author' => $this->generateUserParameter(
520
						$circle->getUser()
521
							   ->getUserId()
522
					),
523
					'circle' => $this->generateCircleParameter($circle),
524
					'level'  => $this->l10n->t($member->getLevelString())
525
				]
526
			);
527
528
		} else {
529
			$event->setRichSubject(
530
				$this->l10n->t(
531
					'{author} changed {member}\'s level in {circle} to %1$s',
532
					[$this->l10n->t($member->getLevelString())]
533
				), [
534
					'author' => $this->generateUserParameter(
535
						$circle->getUser()
536
							   ->getUserId()
537
					),
538
					'circle' => $this->generateCircleParameter($circle),
539
					'member' => $this->generateMemberParameter($member),
540
					'level'  => $this->l10n->t($member->getLevelString())
541
				]
542
			);
543
		}
544
545
		return $event;
546
	}
547
548
549
	/**
550
	 * @param $lang
551
	 * @param Circle $circle
552
	 * @param Member $member
553
	 * @param IEvent $event
554
	 *
555
	 * @return IEvent
556
	 */
557 View Code Duplication
	private function parseMemberOwner($lang, Circle $circle, Member $member, IEvent $event) {
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...
558
		if ($member->getUserId() === $this->activityManager->getCurrentUserId()
559
		) {
560
			$event->setRichSubject(
561
				$this->l10n->t('You are the new owner of {circle}'),
562
				['circle' => $this->generateCircleParameter($circle)]
563
			);
564
565
		} else {
566
			$event->setRichSubject(
567
				$this->l10n->t(
568
					'{member} is the new owner of {circle}'
569
				), [
570
					'circle' => $this->generateCircleParameter($circle),
571
					'member' => $this->generateMemberParameter($member)
572
				]
573
			);
574
		}
575
576
		return $event;
577
	}
578
579
580
	private function generateMemberParameter(
581
		Member $member
582
	) {
583
		return $this->generateUserParameter($member->getUserId());
584
	}
585
586
587
	private function generateCircleParameter(
588
		Circle $circle
589
	) {
590
		return [
591
			'type' => 'circle',
592
			'id'   => $circle->getId(),
593
			'name' => $circle->getName(),
594
			'link' => Circles::generateLink($circle->getId())
595
		];
596
	}
597
598
	/**
599
	 * @param $userId
600
	 *
601
	 * @return array
602
	 */
603
	private function generateUserParameter(
604
		$userId
605
	) {
606
		return [
607
			'type' => 'user',
608
			'id'   => $userId,
609
			'name' => $userId
610
		];
611
	}
612
}
613