Completed
Push — master ( 0a2d75...d5a533 )
by Maxence
03:06
created

Provider::parseLinkDown()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 6

Duplication

Lines 12
Ratio 100 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 12
loc 12
rs 9.4285
cc 1
eloc 6
nc 1
nop 4
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\FederatedLink;
12
use OCA\Circles\Model\Member;
13
use OCA\Circles\Model\SharingFrame;
14
use OCA\Circles\Service\CirclesService;
15
use OCA\Circles\Service\MiscService;
16
use OCP\Activity\IEvent;
17
use OCP\Activity\IManager;
18
use OCP\Activity\IProvider;
19
use OCP\IL10N;
20
use OCP\IURLGenerator;
21
22
23
class Provider implements IProvider {
24
25
	/** @var MiscService */
26
	protected $miscService;
27
28
	/** @var IL10N */
29
	protected $l10n;
30
31
	/** @var IURLGenerator */
32
	protected $url;
33
34
	/** @var IManager */
35
	protected $activityManager;
36
37
	public function __construct(
38
		IURLGenerator $url, IManager $activityManager, IL10N $l10n, MiscService $miscService
39
	) {
40
		$this->url = $url;
41
		$this->activityManager = $activityManager;
42
		$this->l10n = $l10n;
43
		$this->miscService = $miscService;
44
	}
45
46
47
	/**
48
	 * @param string $lang
49
	 * @param IEvent $event
50
	 * @param IEvent|null $previousEvent
51
	 *
52
	 * @return IEvent
0 ignored issues
show
Documentation introduced by
Should the return type not be IEvent|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
53
	 */
54
	public function parse($lang, IEvent $event, IEvent $previousEvent = null) {
55
56
		if ($event->getApp() !== 'circles') {
57
			throw new \InvalidArgumentException();
58
		}
59
60
		$event = $this->parseAsMember($lang, $event);
61
		$event = $this->parseAsModerator($lang, $event);
62
63
		return $event;
64
	}
65
66
67
	/**
68
	 * @param string $lang
69
	 * @param IEvent $event
70
	 *
71
	 * @return IEvent
72
	 * @throws Exception
73
	 */
74
	private function parseAsMember($lang, IEvent $event) {
75
		if ($event->getType() !== 'circles_as_member') {
76
			return $event;
77
		}
78
79
		$params = $event->getSubjectParameters();
80
		$circle = Circle::fromJSON($this->l10n, $params['circle']);
81
		if ($circle === null) {
82
			return $event;
83
		}
84
85
		$event->setIcon(CirclesService::getCircleIcon($circle->getType()));
86
		switch ($event->getSubject()) {
87
			case 'circle_create':
88
				return $this->parseCircleCreate($lang, $circle, $event);
89
90
			case 'circle_delete':
91
				return $this->parseCircleDelete($lang, $circle, $event);
92
		}
93
94
		$event = $this->parseMembersAsMember($lang, $circle, $event);
95
96
		return $event;
97
	}
98
99
100
	/**
101
	 * @param $lang
102
	 * @param Circle $circle
103
	 * @param IEvent $event
104
	 *
105
	 * @return IEvent
106
	 */
107
	private function parseMembersAsMember($lang, Circle $circle, IEvent $event) {
108
		$params = $event->getSubjectParameters();
109
110
		$member = Member::fromJSON($this->l10n, $params['member']);
111
		if ($member === null) {
112
			return $event;
113
		}
114
115
		switch ($event->getSubject()) {
116
			case 'member_join':
117
				return $this->parseMemberJoin($lang, $circle, $member, $event);
118
119
			case 'member_add':
120
				return $this->parseMemberAdd($lang, $circle, $member, $event);
121
122
			case 'member_left':
123
				return $this->parseMemberLeft($lang, $circle, $member, $event);
124
125
			case 'member_remove':
126
				return $this->parseMemberRemove($lang, $circle, $member, $event);
127
128
			default:
129
				return $event;
130
		}
131
132
	}
133
134
135
	/**
136
	 * @param string $lang
137
	 * @param IEvent $event
138
	 *
139
	 * @return IEvent
0 ignored issues
show
Documentation introduced by
Should the return type not be IEvent|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
140
	 * @throws Exception
141
	 */
142
	private function parseAsModerator($lang, IEvent $event) {
143
		if ($event->getType() !== 'circles_as_moderator') {
144
			return $event;
145
		}
146
147
		try {
148
			$params = $event->getSubjectParameters();
149
			$circle = Circle::fromJSON($this->l10n, $params['circle']);
150
			$event->setIcon(CirclesService::getCircleIcon($circle->getType()));
151
152
			if (key_exists('member', $params)) {
153
				$member = Member::fromJSON($this->l10n, $params['member']);
154
155
				return $this->parseMemberAsModerator($lang, $circle, $member, $event);
156
			}
157
158
			if (key_exists('link', $params)) {
159
				$link = FederatedLink::fromJSON($params['link']);
160
161
				return $this->parseLinkAsModerator($lang, $circle, $link, $event);
162
			}
163
164
		} catch (Exception $e) {
165
			throw $e;
166
		}
167
168
	}
169
170
171
	/**
172
	 * @param $lang
173
	 * @param Circle $circle
174
	 * @param Member $member
175
	 * @param IEvent $event
176
	 *
177
	 * @return IEvent
178
	 */
179
	private function parseMemberAsModerator($lang, Circle $circle, Member $member, IEvent $event) {
180
		switch ($event->getSubject()) {
181
			case 'member_invited':
182
				return $this->parseMemberInvited($lang, $circle, $member, $event);
183
184
			case 'member_request_invitation':
185
				return $this->parseMemberRequestInvitation($lang, $circle, $member, $event);
186
187
			case 'member_level':
188
				return $this->parseMemberLevel($lang, $circle, $member, $event);
189
190
			case 'member_owner':
191
				return $this->parseMemberOwner($lang, $circle, $member, $event);
192
		}
193
194
		throw new InvalidArgumentException();
195
	}
196
197
198
	/**
199
	 * @param $lang
200
	 * @param Circle $circle
201
	 * @param FederatedLink $link
202
	 * @param IEvent $event
203
	 *
204
	 * @return IEvent
205
	 */
206
	private function parseLinkAsModerator($lang, Circle $circle, FederatedLink $link, IEvent $event
207
	) {
208
209
		switch ($event->getSubject()) {
210
			case 'link_request_sent':
211
				return $this->parseLinkRequestSent($lang, $circle, $link, $event);
212
213
			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...
214
				return $this->parseLinkRequestReceived($lang, $circle, $link, $event);
215
216
			case 'link_request_rejected':
217
				return $this->parseLinkRequestRejected($lang, $circle, $link, $event);
218
219
			case 'link_request_canceled':
220
				return $this->parseLinkRequestCanceled($lang, $circle, $link, $event);
221
222
			case 'link_request_accepted':
223
				return $this->parseLinkRequestAccepted($lang, $circle, $link, $event);
224
225
			case 'link_request_accepting':
226
				return $this->parseLinkRequestAccepting($lang, $circle, $link, $event);
227
228
			case 'link_up':
229
				return $this->parseLinkUp($lang, $circle, $link, $event);
230
231
			case 'link_down':
232
				return $this->parseLinkDown($lang, $circle, $link, $event);
233
234
			case 'link_remove':
235
				return $this->parseLinkRemove($lang, $circle, $link, $event);
236
237
			case 'link_request_removed':
238
				return $this->parseLinkRequestRemoved($lang, $circle, $link, $event);
239
		}
240
241
		throw new InvalidArgumentException();
242
	}
243
244
	/**
245
	 * @param string $lang
246
	 * @param Circle $circle
247
	 * @param IEvent $event
248
	 *
249
	 * @return IEvent
250
	 */
251
	private function parseCircleCreate($lang, Circle $circle, IEvent $event) {
252
		if ($circle->getOwner()
253
				   ->getUserId() === $this->activityManager->getCurrentUserId()
254
		) {
255
			$event->setRichSubject(
256
				$this->l10n->t('You created the circle {circle}'),
257
				['circle' => $this->generateCircleParameter($circle)]
258
			);
259
260
		} else {
261
			$event->setRichSubject(
262
				$this->l10n->t('{author} created the circle {circle}'),
263
				[
264
					'author' => $author = $this->generateUserParameter(
265
						$circle->getOwner()
266
							   ->getUserId()
267
					),
268
					'circle' => $this->generateCircleParameter($circle)
269
				]
270
			);
271
		}
272
273
		return $event;
274
	}
275
276
277
	/**
278
	 * @param string $lang
279
	 * @param Circle $circle
280
	 * @param IEvent $event
281
	 *
282
	 * @return IEvent
283
	 */
284
	private function parseCircleDelete($lang, Circle $circle, IEvent $event) {
285
		if ($circle->getOwner()
286
				   ->getUserId() === $this->activityManager->getCurrentUserId()
287
		) {
288
			$event->setRichSubject(
289
				$this->l10n->t('You deleted {circle}'),
290
				['circle' => $this->generateCircleParameter($circle)]
291
			);
292
		} else {
293
			$event->setRichSubject(
294
				$this->l10n->t('{author} deleted {circle}'),
295
				[
296
					'author' => $this->generateUserParameter(
297
						$circle->getOwner()
298
							   ->getUserId()
299
					),
300
					'circle' => $this->generateCircleParameter($circle)
301
				]
302
			);
303
		}
304
305
		return $event;
306
	}
307
308
309
	/**
310
	 * @param string $lang
311
	 * @param Circle $circle
312
	 * @param Member $member
313
	 * @param IEvent $event
314
	 *
315
	 * @return IEvent
316
	 */
317 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...
318
319
		if ($circle->getUser()
320
				   ->getUserId() === $this->activityManager->getCurrentUserId()
321
		) {
322
			$event->setRichSubject(
323
				$this->l10n->t('You invited {member} into {circle}'),
324
				[
325
					'circle' => $this->generateCircleParameter($circle),
326
					'member' => $this->generateMemberParameter($member)
327
				]
328
			);
329
330
		} elseif ($member->getUserId() === $this->activityManager->getCurrentUserId()) {
331
			$event->setRichSubject(
332
				$this->l10n->t('You have been invited into {circle} by {author}'),
333
				[
334
					'author' => $this->generateUserParameter(
335
						$circle->getUser()
336
							   ->getUserId()
337
					),
338
					'circle' => $this->generateCircleParameter($circle)
339
				]
340
			);
341
342
		} else {
343
			$event->setRichSubject(
344
				$this->l10n->t('{member} have been invited into {circle} by {author}'),
345
				[
346
					'author' => $this->generateUserParameter(
347
						$circle->getUser()
348
							   ->getUserId()
349
					),
350
					'circle' => $this->generateCircleParameter($circle),
351
					'member' => $this->generateMemberParameter($member)
352
				]
353
			);
354
		}
355
356
		return $event;
357
	}
358
359
360
	/**
361
	 * @param $lang
362
	 * @param Circle $circle
363
	 * @param Member $member
364
	 * @param IEvent $event
365
	 *
366
	 * @return IEvent
367
	 */
368 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...
369
		$lang, Circle $circle, Member $member, IEvent $event
370
	) {
371
		if ($member->getUserId() === $this->activityManager->getCurrentUserId()) {
372
			$event->setRichSubject(
373
				$this->l10n->t('You requested an invitation to {circle}'),
374
				['circle' => $this->generateCircleParameter($circle)]
375
			);
376
377
		} else {
378
			$event->setRichSubject(
379
				$this->l10n->t(
380
					'{author} has requested an invitation into {circle}'
381
				), [
382
					'author' => $this->generateMemberParameter($member),
383
					'circle' => $this->generateCircleParameter($circle)
384
				]
385
			);
386
		}
387
388
		return $event;
389
	}
390
391
392
	/**
393
	 * @param $lang
394
	 * @param Circle $circle
395
	 * @param Member $member
396
	 * @param IEvent $event
397
	 *
398
	 * @return IEvent
399
	 */
400 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...
401
		if ($circle->getUser()
402
				   ->getUserId() === $this->activityManager->getCurrentUserId()
403
		) {
404
			$event->setRichSubject(
405
				$this->l10n->t('You joined {circle}'),
406
				['circle' => $this->generateCircleParameter($circle)]
407
			);
408
409
		} else {
410
			$event->setRichSubject(
411
				$this->l10n->t(
412
					'{member} has joined the circle {circle}'
413
				), [
414
					'circle' => $this->generateCircleParameter($circle),
415
					'member' => $this->generateMemberParameter($member)
416
				]
417
			);
418
		}
419
420
		return $event;
421
	}
422
423
424
	/**
425
	 * @param $lang
426
	 * @param Circle $circle
427
	 * @param Member $member
428
	 * @param IEvent $event
429
	 *
430
	 * @return IEvent
431
	 */
432 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...
433
434
		if ($circle->getUser()
435
				   ->getUserId() === $this->activityManager->getCurrentUserId()
436
		) {
437
			$event->setRichSubject(
438
				$this->l10n->t('You added {member} as member to {circle}'),
439
				[
440
					'circle' => $this->generateCircleParameter($circle),
441
					'member' => $this->generateMemberParameter($member)
442
				]
443
			);
444
445
		} elseif ($member->getUserId() === $this->activityManager->getCurrentUserId()) {
446
			$event->setRichSubject(
447
				$this->l10n->t('You were added as member to {circle} by {author}'),
448
				[
449
					'author' => $this->generateUserParameter(
450
						$circle->getUser()
451
							   ->getUserId()
452
					),
453
					'circle' => $this->generateCircleParameter($circle)
454
				]
455
			);
456
457
		} else {
458
			$event->setRichSubject(
459
				$this->l10n->t(
460
					'{member} was added as member to {circle} by {author}'
461
				), [
462
					'author' => $this->generateUserParameter(
463
						$circle->getUser()
464
							   ->getUserId()
465
					),
466
					'circle' => $this->generateCircleParameter($circle),
467
					'member' => $this->generateMemberParameter($member)
468
				]
469
			);
470
		}
471
472
		return $event;
473
	}
474
475
476
	/**
477
	 * @param $lang
478
	 * @param Circle $circle
479
	 * @param Member $member
480
	 * @param IEvent $event
481
	 *
482
	 * @return IEvent
483
	 */
484 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...
485
		if ($circle->getUser()
486
				   ->getUserId() === $this->activityManager->getCurrentUserId()
487
		) {
488
			$event->setRichSubject(
489
				$this->l10n->t('You left {circle}'),
490
				['circle' => $this->generateCircleParameter($circle)]
491
			);
492
493
		} else {
494
			$event->setRichSubject(
495
				$this->l10n->t(
496
					'{member} has left {circle}'
497
				), [
498
					'circle' => $this->generateCircleParameter($circle),
499
					'member' => $this->generateMemberParameter($member)
500
				]
501
			);
502
		}
503
504
		return $event;
505
	}
506
507
508
	/**
509
	 * @param $lang
510
	 * @param Circle $circle
511
	 * @param Member $member
512
	 * @param IEvent $event
513
	 *
514
	 * @return IEvent
515
	 */
516 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...
517
518
		if ($circle->getUser()
519
				   ->getUserId() === $this->activityManager->getCurrentUserId()
520
		) {
521
			$event->setRichSubject(
522
				$this->l10n->t('You removed {member} from {circle}'),
523
				[
524
					'circle' => $this->generateCircleParameter($circle),
525
					'member' => $this->generateMemberParameter($member)
526
				]
527
			);
528
529
		} elseif ($member->getUserId() === $this->activityManager->getCurrentUserId()) {
530
			$event->setRichSubject(
531
				$this->l10n->t('You were removed from {circle} by {author}'),
532
				[
533
					'author' => $this->generateUserParameter(
534
						$circle->getUser()
535
							   ->getUserId()
536
					),
537
					'circle' => $this->generateCircleParameter($circle)
538
				]
539
			);
540
541
		} else {
542
			$event->setRichSubject(
543
				$this->l10n->t(
544
					'{member} was removed from {circle} by {author}'
545
				), [
546
					'author' => $this->generateUserParameter(
547
						$circle->getUser()
548
							   ->getUserId()
549
					),
550
					'circle' => $this->generateCircleParameter($circle),
551
					'member' => $this->generateMemberParameter($member)
552
				]
553
			);
554
		}
555
556
		return $event;
557
	}
558
559
560
	/**
561
	 * @param $lang
562
	 * @param Circle $circle
563
	 * @param Member $member
564
	 * @param IEvent $event
565
	 *
566
	 * @return IEvent
567
	 */
568
	private function parseMemberLevel($lang, Circle $circle, Member $member, IEvent $event) {
569
570
		if ($circle->getUser()
571
				   ->getUserId() === $this->activityManager->getCurrentUserId()
572
		) {
573
			$event->setRichSubject(
574
				$this->l10n->t(
575
					'You changed {member}\'s level in {circle} to %1$s',
576
					[$this->l10n->t($member->getLevelString())]
577
				),
578
				[
579
					'circle' => $this->generateCircleParameter($circle),
580
					'member' => $this->generateMemberParameter($member)
581
				]
582
			);
583
584
		} elseif ($member->getUserId() === $this->activityManager->getCurrentUserId()) {
585
			$event->setRichSubject(
586
				$this->l10n->t(
587
					'{author} changed your level in {circle} to %1$s',
588
					[$this->l10n->t($member->getLevelString())]
589
				),
590
				[
591
					'author' => $this->generateUserParameter(
592
						$circle->getUser()
593
							   ->getUserId()
594
					),
595
					'circle' => $this->generateCircleParameter($circle),
596
					'level'  => $this->l10n->t($member->getLevelString())
597
				]
598
			);
599
600
		} else {
601
			$event->setRichSubject(
602
				$this->l10n->t(
603
					'{author} changed {member}\'s level in {circle} to %1$s',
604
					[$this->l10n->t($member->getLevelString())]
605
				), [
606
					'author' => $this->generateUserParameter(
607
						$circle->getUser()
608
							   ->getUserId()
609
					),
610
					'circle' => $this->generateCircleParameter($circle),
611
					'member' => $this->generateMemberParameter($member),
612
					'level'  => $this->l10n->t($member->getLevelString())
613
				]
614
			);
615
		}
616
617
		return $event;
618
	}
619
620
621
	/**
622
	 * @param $lang
623
	 * @param Circle $circle
624
	 * @param Member $member
625
	 * @param IEvent $event
626
	 *
627
	 * @return IEvent
628
	 */
629 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...
630
		if ($member->getUserId() === $this->activityManager->getCurrentUserId()
631
		) {
632
			$event->setRichSubject(
633
				$this->l10n->t('You are the new owner of {circle}'),
634
				['circle' => $this->generateCircleParameter($circle)]
635
			);
636
637
		} else {
638
			$event->setRichSubject(
639
				$this->l10n->t(
640
					'{member} is the new owner of {circle}'
641
				), [
642
					'circle' => $this->generateCircleParameter($circle),
643
					'member' => $this->generateMemberParameter($member)
644
				]
645
			);
646
		}
647
648
		return $event;
649
	}
650
651
652
	/**
653
	 * @param $lang
654
	 * @param Circle $circle
655
	 * @param FederatedLink $link
656
	 * @param IEvent $event
657
	 *
658
	 * @return IEvent
659
	 */
660 View Code Duplication
	private function parseLinkRequestSent($lang, Circle $circle, FederatedLink $link, 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...
661
	) {
662
		if ($circle->getUser()
663
				   ->getUserId() === $this->activityManager->getCurrentUserId()
664
		) {
665
			$event->setRichSubject(
666
				$this->l10n->t('You sent a request to link {circle} with {remote}'),
667
				[
668
					'circle' => $this->generateCircleParameter($circle),
669
					'remote' => $this->generateLinkParameter($link)
670
				]
671
			);
672
		} else {
673
			$event->setRichSubject(
674
				$this->l10n->t('{author} sent a request to link {circle} with {remote}'),
675
				[
676
					'author' => $this->generateUserParameter(
677
						$circle->getUser()
678
							   ->getUserId()
679
					),
680
					'circle' => $this->generateCircleParameter($circle),
681
					'remote' => $this->generateLinkParameter($link)
682
				]
683
			);
684
		}
685
686
		return $event;
687
	}
688
689
690
	/**
691
	 * @param $lang
692
	 * @param Circle $circle
693
	 * @param FederatedLink $link
694
	 * @param IEvent $event
695
	 *
696
	 * @return IEvent
697
	 */
698 View Code Duplication
	private function parseLinkRequestReceived(
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...
699
		$lang, Circle $circle, FederatedLink $link, IEvent $event
700
	) {
701
		$event->setRichSubject(
702
			$this->l10n->t('{remote} requested a link with {circle}'),
703
			[
704
				'circle' => $this->generateCircleParameter($circle),
705
				'remote' => $this->generateLinkParameter($link)
706
			]
707
		);
708
709
		return $event;
710
	}
711
712
713
	/**
714
	 * @param $lang
715
	 * @param Circle $circle
716
	 * @param FederatedLink $link
717
	 * @param IEvent $event
718
	 *
719
	 * @return IEvent
720
	 */
721 View Code Duplication
	private function parseLinkRequestRejected(
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...
722
		$lang, Circle $circle, FederatedLink $link, IEvent $event
723
	) {
724
		$event->setRichSubject(
725
			$this->l10n->t('The request to link {circle} with {remote} has been rejected remotely'),
726
			[
727
				'circle' => $this->generateCircleParameter($circle),
728
				'remote' => $this->generateLinkParameter($link)
729
			]
730
		);
731
732
		return $event;
733
	}
734
735
	/**
736
	 * @param $lang
737
	 * @param Circle $circle
738
	 * @param FederatedLink $link
739
	 * @param IEvent $event
740
	 *
741
	 * @return IEvent
742
	 */
743 View Code Duplication
	private function parseLinkRequestCanceled(
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...
744
		$lang, Circle $circle, FederatedLink $link, IEvent $event
745
	) {
746
		$event->setRichSubject(
747
			$this->l10n->t(
748
				'The request to link {remote} with {circle}  has been canceled remotely'
749
			),
750
			[
751
				'circle' => $this->generateCircleParameter($circle),
752
				'remote' => $this->generateLinkParameter($link)
753
			]
754
		);
755
756
		return $event;
757
	}
758
759
760
	/**
761
	 * @param $lang
762
	 * @param Circle $circle
763
	 * @param FederatedLink $link
764
	 * @param IEvent $event
765
	 *
766
	 * @return IEvent
767
	 */
768 View Code Duplication
	private function parseLinkRequestRemoved(
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...
769
		$lang, Circle $circle, FederatedLink $link, IEvent $event
770
	) {
771
		if ($circle->getUser()
772
				   ->getUserId() === $this->activityManager->getCurrentUserId()
773
		) {
774
			$event->setRichSubject(
775
				$this->l10n->t('You dismissed the request to link {remote} with {circle}'),
776
				[
777
					'circle' => $this->generateCircleParameter($circle),
778
					'remote' => $this->generateLinkParameter($link)
779
				]
780
			);
781
		} else {
782
			$event->setRichSubject(
783
				$this->l10n->t('{author} dismissed the request to link {remote} with {circle}'),
784
				[
785
					'author' => $this->generateUserParameter(
786
						$circle->getUser()
787
							   ->getUserId()
788
					),
789
					'circle' => $this->generateCircleParameter($circle),
790
					'remote' => $this->generateLinkParameter($link)
791
				]
792
			);
793
		}
794
795
		return $event;
796
	}
797
798
799
	/**
800
	 * @param $lang
801
	 * @param Circle $circle
802
	 * @param FederatedLink $link
803
	 * @param IEvent $event
804
	 *
805
	 * @return IEvent
806
	 */
807 View Code Duplication
	private function parseLinkRequestAccepted(
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...
808
		$lang, Circle $circle, FederatedLink $link, IEvent $event
809
	) {
810
		$event->setRichSubject(
811
			$this->l10n->t('The request to link {circle} with {remote} has been accepted'),
812
			[
813
				'circle' => $this->generateCircleParameter($circle),
814
				'remote' => $this->generateLinkParameter($link)
815
			]
816
		);
817
818
		return $event;
819
	}
820
821
822
	/**
823
	 * @param $lang
824
	 * @param Circle $circle
825
	 * @param FederatedLink $link
826
	 * @param IEvent $event
827
	 *
828
	 * @return IEvent
829
	 */
830 View Code Duplication
	private function parseLinkRequestAccepting(
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...
831
		$lang, Circle $circle, FederatedLink $link, IEvent $event
832
	) {
833
		if ($circle->getUser()
834
				   ->getUserId() === $this->activityManager->getCurrentUserId()
835
		) {
836
			$event->setRichSubject(
837
				$this->l10n->t('You accepted the request to link {remote} with {circle}'),
838
				[
839
					'circle' => $this->generateCircleParameter($circle),
840
					'remote' => $this->generateLinkParameter($link)
841
				]
842
			);
843
		} else {
844
			$event->setRichSubject(
845
				$this->l10n->t('{author} accepted the request to link {remote} with {circle}'),
846
				[
847
					'author' => $this->generateUserParameter(
848
						$circle->getUser()
849
							   ->getUserId()
850
					),
851
					'circle' => $this->generateCircleParameter($circle),
852
					'remote' => $this->generateLinkParameter($link)
853
				]
854
			);
855
		}
856
857
		return $event;
858
	}
859
860
861
	/**
862
	 * @param $lang
863
	 * @param Circle $circle
864
	 * @param FederatedLink $link
865
	 * @param IEvent $event
866
	 *
867
	 * @return IEvent
868
	 */
869 View Code Duplication
	private function parseLinkUp($lang, Circle $circle, FederatedLink $link, 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...
870
	) {
871
		$event->setRichSubject(
872
			$this->l10n->t('A link between {circle} and {remote} is now up and running'),
873
			[
874
				'circle' => $this->generateCircleParameter($circle),
875
				'remote' => $this->generateLinkParameter($link)
876
			]
877
		);
878
879
		return $event;
880
	}
881
882
883
	/**
884
	 * @param $lang
885
	 * @param Circle $circle
886
	 * @param FederatedLink $link
887
	 * @param IEvent $event
888
	 *
889
	 * @return IEvent
890
	 */
891 View Code Duplication
	private function parseLinkDown($lang, Circle $circle, FederatedLink $link, 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...
892
	) {
893
		$event->setRichSubject(
894
			$this->l10n->t('The link between {circle} and {remote} has been shutdown remotely'),
895
			[
896
				'circle' => $this->generateCircleParameter($circle),
897
				'remote' => $this->generateLinkParameter($link)
898
			]
899
		);
900
901
		return $event;
902
	}
903
904
905
	/**
906
	 * @param $lang
907
	 * @param Circle $circle
908
	 * @param FederatedLink $link
909
	 * @param IEvent $event
910
	 *
911
	 * @return IEvent
912
	 */
913 View Code Duplication
	private function parseLinkRemove($lang, Circle $circle, FederatedLink $link, 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...
914
	) {
915
		$event->setRichSubject(
916
			$this->l10n->t('The link between {circle} and {remote} has been canceled locally'),
917
			[
918
				'circle' => $this->generateCircleParameter($circle),
919
				'remote' => $this->generateLinkParameter($link)
920
			]
921
		);
922
923
		return $event;
924
	}
925
926
927
	private function generateMemberParameter(
928
		Member $member
929
	) {
930
		return $this->generateUserParameter($member->getUserId());
931
	}
932
933
934
	private function generateCircleParameter(
935
		Circle $circle
936
	) {
937
		return [
938
			'type' => 'circle',
939
			'id'   => $circle->getId(),
940
			'name' => $circle->getName(),
941
			'link' => Circles::generateLink($circle->getId())
942
		];
943
	}
944
945
946
	private function generateLinkParameter(FederatedLink $link) {
947
		return [
948
			'type' => 'circle',
949
			'id'   => $link->getUniqueId(),
950
			'name' => $link->getToken() . '@' . $link->getAddress()
951
		];
952
//			'link' => Circles::generateRemoteLink($link)
0 ignored issues
show
Unused Code Comprehensibility introduced by
55% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
953
954
	}
955
956
957
	/**
958
	 * @param $userId
959
	 *
960
	 * @return array
961
	 */
962
	private function generateUserParameter(
963
		$userId
964
	) {
965
		return [
966
			'type' => 'user',
967
			'id'   => $userId,
968
			'name' => $userId
969
		];
970
	}
971
}
972