Completed
Push — master ( 060fe4...9de15f )
by Maxence
02:41
created

Provider::generateParsedSubject()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 3
eloc 8
nc 3
nop 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\FederatedLink;
12
use OCA\Circles\Model\Member;
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
use OpenCloud\Common\Exceptions\InvalidArgumentError;
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
53
	 */
54
	public function parse($lang, IEvent $event, IEvent $previousEvent = null) {
55
56
		if ($event->getApp() !== 'circles') {
57
			throw new \InvalidArgumentException();
58
		}
59
60
		try {
61
62
			$params = $event->getSubjectParameters();
63
			$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...
64
65
			$this->setIcon($event, $circle);
66
			$this->parseAsMember($event, $circle, $params);
67
			$this->parseAsModerator($event, $circle, $params);
68
			$this->generateParsedSubject($event);
69
70
			return $event;
71
		} catch (\Exception $e) {
72
			throw new \InvalidArgumentException();
73
		}
74
	}
75
76
77
	private function setIcon(IEvent &$event, Circle $circle) {
78
		$event->setIcon(
79
			CirclesService::getCircleIcon(
80
				$circle->getType(),
81
				(method_exists($this->activityManager, 'getRequirePNG')
82
				 && $this->activityManager->getRequirePNG())
83
			)
84
		);
85
	}
86
87
	/**
88
	 * @param Circle $circle
89
	 * @param IEvent $event
90
	 * @param array $params
91
	 *
92
	 * @return IEvent
93
	 */
94
	private function parseAsMember(IEvent &$event, Circle $circle, $params) {
95
		if ($event->getType() !== 'circles_as_member') {
96
			return $event;
97
		}
98
99
		switch ($event->getSubject()) {
100
			case 'circle_create':
101
				return $this->parseCircleEvent(
102
					$event, $circle, null,
103
					$this->l10n->t('You created the circle {circle}'),
104
					$this->l10n->t('{author} created the circle {circle}')
105
				);
106
107
			case 'circle_delete':
108
				return $this->parseCircleEvent(
109
					$event, $circle, null,
110
					$this->l10n->t('You deleted {circle}'),
111
					$this->l10n->t('{author} deleted {circle}')
112
				);
113
		}
114
115
		if (key_exists('member', $params)) {
116
			$this->parseMemberAsMember($event, $circle);
117
		}
118
119
		return $event;
120
	}
121
122
123
	/**
124
	 * @param Circle $circle
125
	 * @param IEvent $event
126
	 *
127
	 * @return IEvent
128
	 */
129
	private function parseMemberAsMember(IEvent &$event, Circle $circle) {
130
		$params = $event->getSubjectParameters();
131
		$member = Member::fromJSON($params['member']);
132
133
		switch ($event->getSubject()) {
134
			case 'member_join':
135
				return $this->parseSubjectMemberJoin($event, $circle, $member);
136
137
			case 'member_add':
138
				return $this->parseSubjectMemberAdd($event, $circle, $member);
139
140
			case 'member_left':
141
				return $this->parseSubjectMemberLeft($event, $circle, $member);
142
143
			case 'member_remove':
144
				return $this->parseSubjectMemberRemove($event, $circle, $member);
145
		}
146
147
		return $event;
148
	}
149
150
151
	/**
152
	 * Parse on Subject 'member_join'.
153
	 * If circle is closed, we say that user accepted his invitation.
154
	 *
155
	 * @param IEvent $event
156
	 * @param Circle $circle
157
	 * @param Member $member
158
	 *
159
	 * @return IEvent
160
	 */
161 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...
162
		if ($circle->getType() === Circle::CIRCLES_CLOSED) {
163
			return $this->parseCircleMemberEvent(
164
				$event, $circle, $member,
165
				$this->l10n->t('You accepted the invitation to join {circle}'),
166
				$this->l10n->t('{member} accepted the invitation to join {circle}')
167
			);
168
		} else {
169
			return $this->parseCircleMemberEvent(
170
				$event, $circle, $member,
171
				$this->l10n->t('You joined {circle}'),
172
				$this->l10n->t('{member} joined {circle}')
173
			);
174
		}
175
	}
176
177
178
	/**
179
	 * Parse on Subject 'member_add'.
180
	 * If circle is closed, we say that user's invitation was accepted.
181
	 *
182
	 * @param IEvent $event
183
	 * @param Circle $circle
184
	 * @param Member $member
185
	 *
186
	 * @return IEvent
187
	 */
188 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...
189
		if ($circle->getType() === Circle::CIRCLES_CLOSED) {
190
			return $this->parseCircleMemberAdvancedEvent(
191
				$event, $circle, $member,
192
				$this->l10n->t("You accepted {member}'s request to join {circle}"),
193
				$this->l10n->t('Your request to join {circle} has been accepted by {author}'),
194
				$this->l10n->t("{member}'s request to join {circle} has been accepted by {author}")
195
			);
196
		} else {
197
			return $this->parseCircleMemberAdvancedEvent(
198
				$event, $circle, $member,
199
				$this->l10n->t('You added {member} as member to {circle}'),
200
				$this->l10n->t('You have been added as member to {circle} by {author}'),
201
				$this->l10n->t('{member} has been added as member to {circle} by {author}')
202
			);
203
		}
204
	}
205
206
207
	/**
208
	 * Parse on Subject 'member_left'.
209
	 * If circle is closed and member was not a real member, we send him to
210
	 * parseSubjectNonMemberLeftClosedCircle();
211
	 *
212
	 * @param IEvent $event
213
	 * @param Circle $circle
214
	 * @param Member $member
215
	 *
216
	 * @return IEvent
217
	 */
218
	private function parseSubjectMemberLeft(IEvent &$event, Circle $circle, Member $member) {
219
		if ($circle->getType() === Circle::CIRCLES_CLOSED
220
			&& $member->getLevel() === Member::LEVEL_NONE) {
221
			return $this->parseSubjectNonMemberLeftClosedCircle($event, $circle, $member);
222
		} else {
223
			return $this->parseCircleMemberEvent(
224
				$event, $circle, $member,
225
				$this->l10n->t('You left {circle}'),
226
				$this->l10n->t('{member} left {circle}')
227
			);
228
		}
229
	}
230
231
	/**
232
	 * Parse on Subject 'member_left' on a closed circle when user were not yet a member.
233
	 * If status is Invited we say that member rejected his invitation.
234
	 * If status is Requested we say he dismissed his request.
235
	 *
236
	 * @param IEvent $event
237
	 * @param Circle $circle
238
	 * @param Member $member
239
	 *
240
	 * @return IEvent
241
	 */
242 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...
243
		IEvent &$event, Circle $circle, Member $member
244
	) {
245
		if ($member->getStatus() === Member::STATUS_INVITED) {
246
			return $this->parseCircleMemberEvent(
247
				$event, $circle, $member,
248
				$this->l10n->t("You declined the invitation to join {circle}"),
249
				$this->l10n->t("{member} declined an invitation to join {circle}")
250
			);
251
		}
252
253
		return $this->parseCircleMemberEvent(
254
			$event, $circle, $member,
255
			$this->l10n->t("You cancelled your request to join {circle}"),
256
			$this->l10n->t("{member} cancelled his request to join {circle}")
257
		);
258
	}
259
260
261
	/**
262
	 * Parse on Subject 'member_remove'.
263
	 * If circle is closed and member was not a real member, we send him to
264
	 * parseSubjectNonMemberRemoveClosedCircle();
265
	 *
266
	 * @param IEvent $event
267
	 * @param Circle $circle
268
	 * @param Member $member
269
	 *
270
	 * @return IEvent
271
	 */
272 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...
273
		if ($circle->getType() === Circle::CIRCLES_CLOSED
274
			&& $member->getLevel() === Member::LEVEL_NONE) {
275
			return $this->parseSubjectNonMemberRemoveClosedCircle($event, $circle, $member);
276
277
		} else {
278
			return $this->parseCircleMemberAdvancedEvent(
279
				$event, $circle, $member,
280
				$this->l10n->t('You removed {member} from {circle}'),
281
				$this->l10n->t('You have been removed from {circle} by {author}'),
282
				$this->l10n->t('{member} has been removed from {circle} by {author}')
283
			);
284
		}
285
	}
286
287
288
	/**
289
	 * Parse on Subject 'member_remove' on a closed circle when user were not yet a member.
290
	 * If status is Invited we say that author cancelled his invitation.
291
	 * If status is Requested we say that his invitation was rejected.
292
	 *
293
	 * @param IEvent $event
294
	 * @param Circle $circle
295
	 * @param Member $member
296
	 *
297
	 * @return IEvent
298
	 */
299 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...
300
		IEvent &$event, Circle $circle, Member $member
301
	) {
302
	if ($member->getStatus() === Member::STATUS_REQUEST) {
303
			return $this->parseCircleMemberAdvancedEvent(
304
				$event, $circle, $member,
305
				$this->l10n->t("You dismissed {member}'s request to join {circle}"),
306
				$this->l10n->t('Your request to join {circle} has been dismissed by {author}'),
307
				$this->l10n->t("{member}'s request to join {circle} has been dismissed by {author}")
308
			);
309
		}
310
311
		return $this->parseCircleMemberAdvancedEvent(
312
			$event, $circle, $member,
313
			$this->l10n->t("You cancelled {member}'s invitation to join {circle}"),
314
			$this->l10n->t('Your invitation to join {circle} has been cancelled by {author}'),
315
			$this->l10n->t("{author} cancelled {member}'s invitation to join {circle}")
316
		);
317
	}
318
319
320
	/**
321
	 * @param Circle $circle
322
	 * @param IEvent $event
323
	 * @param array $params
324
	 *
325
	 * @return IEvent
326
	 * @throws Exception
327
	 */
328
	private function parseAsModerator(IEvent &$event, Circle $circle, $params) {
329
		if ($event->getType() !== 'circles_as_moderator') {
330
			return $event;
331
		}
332
333
		try {
334
			if (key_exists('member', $params)) {
335
				return $this->parseMemberAsModerator($event, $circle);
336
			}
337
338
			if (key_exists('group', $params)) {
339
				return $this->parseGroupAsModerator($event, $circle);
340
			}
341
342
			if (key_exists('link', $params)) {
343
				return $this->parseLinkAsModerator($event, $circle);
344
			}
345
346
			throw new InvalidArgumentError();
347
		} catch (Exception $e) {
348
			throw $e;
349
		}
350
	}
351
352
353
	/**
354
	 * @param Circle $circle
355
	 * @param IEvent $event
356
	 *
357
	 * @return IEvent
358
	 */
359
	private function parseGroupAsModerator(IEvent &$event, Circle $circle) {
360
361
		$params = $event->getSubjectParameters();
362
		$group = Member::fromJSON($params['group']);
363
364
		switch ($event->getSubject()) {
365
366 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...
367
				return $this->parseCircleMemberEvent(
368
					$event, $circle, $group,
369
					$this->l10n->t('You linked {group} to {circle}'),
370
					$this->l10n->t('{group} has been linked to {circle} by {author}')
371
				);
372
373 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...
374
				return $this->parseCircleMemberEvent(
375
					$event, $circle, $group,
376
					$this->l10n->t('You unlinked {group} from {circle}'),
377
					$this->l10n->t('{group} has been unlinked from {circle} by {author}')
378
				);
379
380
			case 'group_level':
381
				$level = [$this->l10n->t($group->getLevelString())];
382
383
				return $this->parseCircleMemberEvent(
384
					$event, $circle, $group,
385
					$this->l10n->t(
386
						'You changed the level of the linked group {group} in {circle} to %1$s',
387
						$level
388
					),
389
					$this->l10n->t(
390
						'{author} changed the level of the linked group {group} in {circle} to %1$s',
391
						$level
392
					)
393
				);
394
		}
395
396
		throw new InvalidArgumentException();
397
	}
398
399
400
	/**
401
	 * @param Circle $circle
402
	 * @param IEvent $event
403
	 *
404
	 * @return IEvent
405
	 */
406
	private function parseMemberAsModerator(IEvent &$event, Circle $circle) {
407
408
		$params = $event->getSubjectParameters();
409
		$member = Member::fromJSON($params['member']);
410
411
		switch ($event->getSubject()) {
412
			case 'member_invited':
413
				return $this->parseCircleMemberAdvancedEvent(
414
					$event, $circle, $member,
415
					$this->l10n->t('You invited {member} to join {circle}'),
416
					$this->l10n->t('You have been invited to join {circle} by {author}'),
417
					$this->l10n->t('{member} has been invited to join {circle} by {author}')
418
				);
419
420
			case 'member_level':
421
				$level = [$this->l10n->t($member->getLevelString())];
422
423
				return $this->parseCircleMemberAdvancedEvent(
424
					$event, $circle, $member,
425
					$this->l10n->t('You changed {member}\'s level in {circle} to %1$s', $level),
426
					$this->l10n->t('{author} changed your level in {circle} to %1$s', $level),
427
					$this->l10n->t('{author} changed {member}\'s level in {circle} to %1$s', $level)
428
				);
429
430 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...
431
				return $this->parseMemberEvent(
432
					$event, $circle, $member,
433
					$this->l10n->t('You sent a request to join {circle}'),
434
					$this->l10n->t('{member} sent a request to join {circle}')
435
				);
436
437 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...
438
				return $this->parseMemberEvent(
439
					$event, $circle, $member,
440
					$this->l10n->t('You are the new owner of {circle}'),
441
					$this->l10n->t('{member} is the new owner of {circle}')
442
				);
443
		}
444
445
		throw new InvalidArgumentException();
446
	}
447
448
449
	/**
450
	 * @param Circle $circle
451
	 * @param IEvent $event
452
	 *
453
	 * @return IEvent
454
	 */
455
	private function parseLinkAsModerator(IEvent &$event, Circle $circle) {
456
457
		$params = $event->getSubjectParameters();
458
		$remote = FederatedLink::fromJSON($params['link']);
459
460
		switch ($event->getSubject()) {
461 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...
462
				return $this->parseCircleEvent(
463
					$event, $circle, $remote,
464
					$this->l10n->t('You sent a request to link {circle} with {remote}'),
465
					$this->l10n->t('{author} sent a request to link {circle} with {remote}')
466
				);
467
468
			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...
469
				return $this->parseLinkEvent(
470
					$event, $circle, $remote,
471
					$this->l10n->t('{remote} requested a link with {circle}')
472
				);
473
474
			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...
475
				return $this->parseLinkEvent(
476
					$event, $circle, $remote, $this->l10n->t(
477
					'The request to link {circle} with {remote} has been rejected'
478
				)
479
				);
480
481
			case 'link_request_canceled':
482
				return $this->parseLinkEvent(
483
					$event, $circle, $remote,
484
					$this->l10n->t(
485
						'The request to link {remote} with {circle} has been canceled remotely'
486
					)
487
				);
488
489
			case 'link_request_accepted':
490
				return $this->parseLinkEvent(
491
					$event, $circle, $remote,
492
					$this->l10n->t('The request to link {circle} with {remote} has been accepted')
493
				);
494
495 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...
496
				return $this->parseCircleEvent(
497
					$event, $circle, $remote,
498
					$this->l10n->t('You dismissed the request to link {remote} with {circle}'),
499
					$this->l10n->t('{author} dismissed the request to link {remote} with {circle}')
500
				);
501
502 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...
503
				return $this->parseCircleEvent(
504
					$event, $circle, $remote,
505
					$this->l10n->t('You canceled the request to link {circle} with {remote}'),
506
					$this->l10n->t('{author} canceled the request to link {circle} with {remote}')
507
				);
508
509 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...
510
				return $this->parseCircleEvent(
511
					$event, $circle, $remote,
512
					$this->l10n->t('You accepted the request to link {remote} with {circle}'),
513
					$this->l10n->t('{author} accepted the request to link {remote} with {circle}')
514
				);
515
516
			case 'link_up':
517
				return $this->parseLinkEvent(
518
					$event, $circle, $remote,
519
					$this->l10n->t('A link between {circle} and {remote} is now up and running')
520
				);
521
522
			case 'link_down':
523
				return $this->parseLinkEvent(
524
					$event, $circle, $remote,
525
					$this->l10n->t(
526
						'The link between {circle} and {remote} has been shutdown remotely'
527
					)
528
				);
529
530 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...
531
				return $this->parseCircleEvent(
532
					$event, $circle, $remote,
533
					$this->l10n->t('You closed the link between {circle} and {remote}'),
534
					$this->l10n->t('{author} closed the link between {circle} and {remote}')
535
				);
536
		}
537
538
		throw new InvalidArgumentException();
539
	}
540
541
542
	/**
543
	 * general function to generate Circle event.
544
	 *
545
	 * @param IEvent $event
546
	 * @param Circle $circle
547
	 * @param FederatedLink $remote
548
	 * @param string $ownEvent
549
	 * @param string $othersEvent
550
	 *
551
	 * @return IEvent
552
	 */
553
	private function parseCircleEvent(
554
		IEvent &$event, Circle $circle, $remote, $ownEvent, $othersEvent
555
	) {
556
		$data = [
557
			'author' => $this->generateViewerParameter($circle),
558
			'circle' => $this->generateCircleParameter($circle),
559
			'remote' => ($remote === null) ? '' : $this->generateRemoteCircleParameter($remote)
560
		];
561
562
		if ($this->isViewerTheAuthor($circle, $this->activityManager->getCurrentUserId())) {
563
			return $event->setRichSubject($ownEvent, $data);
564
		}
565
566
		return $event->setRichSubject($othersEvent, $data);
567
	}
568
569
570
	/**
571
	 * general function to generate Member event.
572
	 *
573
	 * @param Circle $circle
574
	 * @param $member
575
	 * @param IEvent $event
576
	 * @param $ownEvent
577
	 * @param $othersEvent
578
	 *
579
	 * @return IEvent
580
	 */
581
	private function parseMemberEvent(
582
		IEvent &$event, Circle $circle, Member $member, $ownEvent, $othersEvent
583
	) {
584
		$data = [
585
			'circle' => $this->generateCircleParameter($circle),
586
			'member' => $this->generateUserParameter($member)
587
		];
588
589
		if ($member->getUserId() === $this->activityManager->getCurrentUserId()
590
		) {
591
			return $event->setRichSubject($ownEvent, $data);
592
		}
593
594
		return $event->setRichSubject($othersEvent, $data);
595
	}
596
597
598
	/**
599
	 * general function to generate Link event.
600
	 *
601
	 * @param Circle $circle
602
	 * @param FederatedLink $remote
603
	 * @param IEvent $event
604
	 * @param string $line
605
	 *
606
	 * @return IEvent
607
	 */
608
	private function parseLinkEvent(IEvent &$event, Circle $circle, FederatedLink $remote, $line) {
609
		$data = [
610
			'circle' => $this->generateCircleParameter($circle),
611
			'remote' => $this->generateRemoteCircleParameter($remote)
612
		];
613
614
		return $event->setRichSubject($line, $data);
615
	}
616
617
618
	/**
619
	 * general function to generate Circle+Member event.
620
	 *
621
	 * @param Circle $circle
622
	 * @param Member $member
623
	 * @param IEvent $event
624
	 * @param string $ownEvent
625
	 * @param string $othersEvent
626
	 *
627
	 * @return IEvent
628
	 */
629
	private function parseCircleMemberEvent(
630
		IEvent &$event, Circle $circle, Member $member, $ownEvent, $othersEvent
631
	) {
632
		$data = [
633
			'author' => $this->generateViewerParameter($circle),
634
			'circle' => $this->generateCircleParameter($circle),
635
			'member' => $this->generateUserParameter($member),
636
			'group'  => $this->generateGroupParameter($member),
637
		];
638
639
		if ($this->isViewerTheAuthor($circle, $this->activityManager->getCurrentUserId())) {
640
			return $event->setRichSubject($ownEvent, $data);
641
		}
642
643
		return $event->setRichSubject($othersEvent, $data);
644
	}
645
646
647
	/**
648
	 * general function to generate Circle+Member advanced event.
649
	 *
650
	 * @param Circle $circle
651
	 * @param Member $member
652
	 * @param IEvent $event
653
	 *\
654
	 * @param $ownEvent
655
	 * @param $targetEvent
656
	 * @param $othersEvent
657
	 *
658
	 * @return IEvent
659
	 */
660
	private function parseCircleMemberAdvancedEvent(
661
		IEvent &$event, Circle $circle, Member $member, $ownEvent, $targetEvent, $othersEvent
662
	) {
663
		$data = [
664
			'author' => $this->generateViewerParameter($circle),
665
			'circle' => $this->generateCircleParameter($circle),
666
			'member' => $this->generateUserParameter($member)
667
		];
668
669
		if ($this->isViewerTheAuthor($circle, $this->activityManager->getCurrentUserId())) {
670
			return $event->setRichSubject($ownEvent, $data);
671
		}
672
673
		if ($member->getUserId() === $this->activityManager->getCurrentUserId()) {
674
			return $event->setRichSubject($targetEvent, $data);
675
		}
676
677
		return $event->setRichSubject($othersEvent, $data);
678
	}
679
680
681
	/**
682
	 * @param IEvent $event
683
	 */
684
	private function generateParsedSubject(IEvent &$event) {
685
		$subject = $event->getRichSubject();
686
		$params = $event->getRichSubjectParameters();
687
		$ak = array_keys($params);
688
		foreach ($ak as $k) {
689
			if (is_array($params[$k])) {
690
				$subject = str_replace('{' . $k . '}', $params[$k]['parsed'], $subject);
691
			}
692
		}
693
694
		$event->setParsedSubject($subject);
695
	}
696
697
698
	/**
699
	 * @param Circle $circle
700
	 * @param string $userId
701
	 *
702
	 * @return bool
703
	 */
704
	private function isViewerTheAuthor(Circle $circle, $userId) {
705
		if ($circle->getViewer() === null) {
706
			return false;
707
		}
708
709
		if ($circle->getViewer()
0 ignored issues
show
Unused Code introduced by
This if statement, and the following return statement can be replaced with return $circle->getViewe...etUserId() === $userId;.
Loading history...
710
				   ->getUserId() === $userId) {
711
			return true;
712
		}
713
714
		return false;
715
	}
716
717
718
	/**
719
	 * @param Circle $circle
720
	 *
721
	 * @return string|array <string,string|integer>
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use string|array<string,string>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
722
	 */
723
	private function generateViewerParameter(Circle $circle) {
724
		if ($circle->getViewer() === null) {
725
			return '';
726
		}
727
728
		return $this->generateUserParameter($circle->getViewer());
729
	}
730
731
732
	/**
733
	 * @param Circle $circle
734
	 *
735
	 * @return array<string,string|integer>
736
	 */
737
	private function generateCircleParameter(Circle $circle) {
738
		return [
739
			'type'   => 'circle',
740
			'id'     => $circle->getId(),
741
			'name'   => $circle->getName(),
742
			'parsed' => $circle->getName(),
743
			'link'   => Circles::generateLink($circle->getUniqueId())
744
		];
745
	}
746
747
748
	/**
749
	 * @param FederatedLink $link
750
	 *
751
	 * @return array<string,string|integer>
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<string,string>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
752
	 */
753
	private function generateRemoteCircleParameter(FederatedLink $link) {
754
		return [
755
			'type'   => 'circle',
756
			'id'     => $link->getUniqueId(),
757
			'name'   => $link->getToken() . '@' . $link->getAddress(),
758
			'parsed' => $link->getToken() . '@' . $link->getAddress()
759
		];
760
	}
761
762
763
	/**
764
	 * @param Member $member
765
	 *
766
	 * @return array <string,string|integer>
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<string,string>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
767
	 */
768
	private function generateUserParameter(Member $member) {
769
		return [
770
			'type'   => 'user',
771
			'id'     => $member->getUserId(),
772
			'name'   => $this->miscService->getDisplayName($member->getUserId(), true),
773
			'parsed' => $this->miscService->getDisplayName($member->getUserId(), true)
774
		];
775
	}
776
777
778
	/**
779
	 * @param Member $group
780
	 *
781
	 * @return array <string,string|integer>
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<string,string>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
782
	 */
783
	private function generateGroupParameter($group) {
784
		return [
785
			'type'   => 'group',
786
			'id'     => $group->getUserId(),
787
			'name'   => $group->getUserId(),
788
			'parsed' => $group->getUserId()
789
		];
790
	}
791
}
792