Completed
Push — master ( 70edb2...6362ed )
by Maxence
02:18
created

Provider::parseGroupAsModerator()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 35
Code Lines 21

Duplication

Lines 12
Ratio 34.29 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 12
loc 35
rs 8.5806
cc 4
eloc 21
nc 4
nop 2
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']);
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($this->l10n, $params['member']);
132
133
		switch ($event->getSubject()) {
134 View Code Duplication
			case 'member_join':
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...
135
				return $this->parseCircleMemberEvent(
136
					$event, $circle, $member,
137
					$this->l10n->t('You joined {circle}'),
138
					$this->l10n->t('{member} joined {circle}')
139
				);
140
141 View Code Duplication
			case 'member_add':
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...
142
				return $this->parseCircleMemberAdvancedEvent(
143
					$event, $circle, $member,
144
					$this->l10n->t('You added {member} as member to {circle}'),
145
					$this->l10n->t('You were added as member to {circle} by {author}'),
146
					$this->l10n->t('{member} was added as member to {circle} by {author}')
147
				);
148
149 View Code Duplication
			case 'member_left':
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...
150
				return $this->parseCircleMemberEvent(
151
					$event, $circle, $member,
152
					$this->l10n->t('You left {circle}'),
153
					$this->l10n->t('{member} left {circle}')
154
				);
155
156 View Code Duplication
			case 'member_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...
157
				return $this->parseCircleMemberAdvancedEvent(
158
					$event, $circle, $member,
159
					$this->l10n->t('You removed {member} from {circle}'),
160
					$this->l10n->t('You were removed from {circle} by {author}'),
161
					$this->l10n->t('{member} was removed from {circle} by {author}')
162
				);
163
		}
164
165
		return $event;
166
	}
167
168
169
	/**
170
	 * @param Circle $circle
171
	 * @param IEvent $event
172
	 * @param array $params
173
	 *
174
	 * @return IEvent
175
	 * @throws Exception
176
	 */
177
	private function parseAsModerator(IEvent &$event, Circle $circle, $params) {
178
		if ($event->getType() !== 'circles_as_moderator') {
179
			return $event;
180
		}
181
182
		try {
183
			if (key_exists('member', $params)) {
184
				return $this->parseMemberAsModerator($event, $circle);
185
			}
186
187
			if (key_exists('group', $params)) {
188
				return $this->parseGroupAsModerator($event, $circle);
189
			}
190
191
			if (key_exists('link', $params)) {
192
				return $this->parseLinkAsModerator($event, $circle);
193
			}
194
195
			throw new InvalidArgumentError();
196
		} catch (Exception $e) {
197
			throw $e;
198
		}
199
	}
200
201
202
	/**
203
	 * @param Circle $circle
204
	 * @param IEvent $event
205
	 *
206
	 * @return IEvent
207
	 */
208
	private function parseGroupAsModerator(IEvent &$event, Circle $circle) {
209
210
		$params = $event->getSubjectParameters();
211
		$group = Member::fromJSON($this->l10n, $params['group']);
212
213
		switch ($event->getSubject()) {
214
215 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...
216
				return $this->parseCircleMemberEvent(
217
					$event, $circle, $group,
218
					$this->l10n->t('You linked {group} to {circle}'),
219
					$this->l10n->t('{group} was linked to {circle} by {author}')
220
				);
221
222
223 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...
224
				return $this->parseCircleMemberEvent(
225
					$event, $circle, $group,
226
					$this->l10n->t('You unlinked {group} from {circle}'),
227
					$this->l10n->t('{group} was unlinked from {circle} by {author}')
228
				);
229
230
231
			case 'group_level':
232
				$level = [$this->l10n->t($group->getLevelString())];
233
234
				return $this->parseCircleMemberEvent(
235
					$event, $circle, $group,
236
					$this->l10n->t('You changed the level of the linked group {group} in {circle} to %1$s', $level),
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 100 characters; contains 101 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
237
					$this->l10n->t('{author} changed the level of the linked group {group} in {circle} to %1$s', $level)
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 100 characters; contains 105 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
238
				);
239
240
		}
241
		throw new InvalidArgumentException();
242
	}
243
244
245
	/**
246
	 * @param Circle $circle
247
	 * @param IEvent $event
248
	 *
249
	 * @return IEvent
250
	 */
251
	private function parseMemberAsModerator(IEvent &$event, Circle $circle) {
252
253
		$params = $event->getSubjectParameters();
254
		$member = Member::fromJSON($this->l10n, $params['member']);
255
256
		switch ($event->getSubject()) {
257 View Code Duplication
			case 'member_invited':
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...
258
				return $this->parseCircleMemberAdvancedEvent(
259
					$event, $circle, $member,
260
					$this->l10n->t('You invited {member} into {circle}'),
261
					$this->l10n->t('You have been invited into {circle} by {author}'),
262
					$this->l10n->t('{member} have been invited into {circle} by {author}')
263
				);
264
265
			case 'member_level':
266
				$level = [$this->l10n->t($member->getLevelString())];
267
268
				return $this->parseCircleMemberAdvancedEvent(
269
					$event, $circle, $member,
270
					$this->l10n->t('You changed {member}\'s level in {circle} to %1$s', $level),
271
					$this->l10n->t('{author} changed your level in {circle} to %1$s', $level),
272
					$this->l10n->t('{author} changed {member}\'s level in {circle} to %1$s', $level)
273
				);
274
275 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...
276
				return $this->parseMemberEvent(
277
					$event, $circle, $member,
278
					$this->l10n->t('You requested an invitation into {circle}'),
279
					$this->l10n->t(
280
						'{member} has requested an invitation into {circle}'
281
					)
282
				);
283
284 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...
285
				return $this->parseMemberEvent(
286
					$event, $circle, $member,
287
					$this->l10n->t('You are the new owner of {circle}'),
288
					$this->l10n->t('{member} is the new owner of {circle}')
289
				);
290
		}
291
292
		throw new InvalidArgumentException();
293
	}
294
295
296
	/**
297
	 * @param Circle $circle
298
	 * @param IEvent $event
299
	 *
300
	 * @return IEvent
301
	 */
302
	private function parseLinkAsModerator(IEvent &$event, Circle $circle) {
303
304
		$params = $event->getSubjectParameters();
305
		$remote = FederatedLink::fromJSON($params['link']);
306
307
		switch ($event->getSubject()) {
308 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...
309
				return $this->parseCircleEvent(
310
					$event, $circle, $remote,
311
					$this->l10n->t('You sent a request to link {circle} with {remote}'),
312
					$this->l10n->t('{author} sent a request to link {circle} with {remote}')
313
				);
314
315
			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...
316
				return $this->parseLinkEvent(
317
					$event, $circle, $remote,
318
					$this->l10n->t('{remote} requested a link with {circle}')
319
				);
320
321
			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...
322
				return $this->parseLinkEvent(
323
					$event, $circle, $remote, $this->l10n->t(
324
					'The request to link {circle} with {remote} has been rejected'
325
				)
326
				);
327
328
			case 'link_request_canceled':
329
				return $this->parseLinkEvent(
330
					$event, $circle, $remote,
331
					$this->l10n->t(
332
						'The request to link {remote} with {circle} has been canceled remotely'
333
					)
334
				);
335
336
			case 'link_request_accepted':
337
				return $this->parseLinkEvent(
338
					$event, $circle, $remote,
339
					$this->l10n->t('The request to link {circle} with {remote} has been accepted')
340
				);
341
342 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...
343
				return $this->parseCircleEvent(
344
					$event, $circle, $remote,
345
					$this->l10n->t('You dismissed the request to link {remote} with {circle}'),
346
					$this->l10n->t('{author} dismissed the request to link {remote} with {circle}')
347
				);
348
349 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...
350
				return $this->parseCircleEvent(
351
					$event, $circle, $remote,
352
					$this->l10n->t('You canceled the request to link {circle} with {remote}'),
353
					$this->l10n->t('{author} canceled the request to link {circle} with {remote}')
354
				);
355
356 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...
357
				return $this->parseCircleEvent(
358
					$event, $circle, $remote,
359
					$this->l10n->t('You accepted the request to link {remote} with {circle}'),
360
					$this->l10n->t('{author} accepted the request to link {remote} with {circle}')
361
				);
362
363
			case 'link_up':
364
				return $this->parseLinkEvent(
365
					$event, $circle, $remote,
366
					$this->l10n->t('A link between {circle} and {remote} is now up and running')
367
				);
368
369
			case 'link_down':
370
				return $this->parseLinkEvent(
371
					$event, $circle, $remote,
372
					$this->l10n->t(
373
						'The link between {circle} and {remote} has been shutdown remotely'
374
					)
375
				);
376
377 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...
378
				return $this->parseCircleEvent(
379
					$event, $circle, $remote,
380
					$this->l10n->t('You closed the link between {circle} and {remote}'),
381
					$this->l10n->t('{author} closed the link between {circle} and {remote}')
382
				);
383
		}
384
385
		throw new InvalidArgumentException();
386
	}
387
388
389
	/**
390
	 * general function to generate Circle event.
391
	 *
392
	 * @param IEvent $event
393
	 * @param Circle $circle
394
	 * @param FederatedLink $remote
395
	 * @param string $ownEvent
396
	 * @param string $othersEvent
397
	 *
398
	 * @return IEvent
399
	 */
400 View Code Duplication
	private function parseCircleEvent(
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
		IEvent &$event, Circle $circle, $remote, $ownEvent, $othersEvent
402
	) {
403
		$data = [
404
			'author' => $this->generateViewerParameter($circle),
405
			'circle' => $this->generateCircleParameter($circle),
406
			'remote' => ($remote === null) ? '' : $this->generateRemoteCircleParameter($remote)
407
		];
408
409
		if ($this->isViewerTheAuthor($circle, $this->activityManager->getCurrentUserId())) {
410
			return $event->setRichSubject($ownEvent, $data);
411
		}
412
413
		return $event->setRichSubject($othersEvent, $data);
414
	}
415
416
417
	/**
418
	 * general function to generate Member event.
419
	 *
420
	 * @param Circle $circle
421
	 * @param $member
422
	 * @param IEvent $event
423
	 * @param $ownEvent
424
	 * @param $othersEvent
425
	 *
426
	 * @return IEvent
427
	 */
428
	private function parseMemberEvent(
429
		IEvent &$event, Circle $circle, Member $member, $ownEvent, $othersEvent
430
	) {
431
		$data = [
432
			'circle' => $this->generateCircleParameter($circle),
433
			'member' => $this->generateMemberParameter($member)
434
		];
435
436
		if ($member->getUserId() === $this->activityManager->getCurrentUserId()
437
		) {
438
			return $event->setRichSubject($ownEvent, $data);
439
		}
440
441
		return $event->setRichSubject($othersEvent, $data);
442
	}
443
444
445
	/**
446
	 * general function to generate Link event.
447
	 *
448
	 * @param Circle $circle
449
	 * @param FederatedLink $remote
450
	 * @param IEvent $event
451
	 * @param string $line
452
	 *
453
	 * @return IEvent
454
	 */
455
	private function parseLinkEvent(IEvent &$event, Circle $circle, FederatedLink $remote, $line) {
456
		$data = [
457
			'circle' => $this->generateCircleParameter($circle),
458
			'remote' => $this->generateRemoteCircleParameter($remote)
459
		];
460
461
		return $event->setRichSubject($line, $data);
462
	}
463
464
465
	/**
466
	 * general function to generate Circle+Member event.
467
	 *
468
	 * @param Circle $circle
469
	 * @param Member $member
470
	 * @param IEvent $event
471
	 * @param string $ownEvent
472
	 * @param string $othersEvent
473
	 *
474
	 * @return IEvent
475
	 */
476 View Code Duplication
	private function parseCircleMemberEvent(
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...
477
		IEvent &$event, Circle $circle, Member $member, $ownEvent, $othersEvent
478
	) {
479
		$data = [
480
			'author' => $this->generateViewerParameter($circle),
481
			'circle' => $this->generateCircleParameter($circle),
482
			'member' => $this->generateMemberParameter($member),
483
			'group' => $this->generateMemberParameter($member),
484
		];
485
486
		if ($this->isViewerTheAuthor($circle, $this->activityManager->getCurrentUserId())) {
487
			return $event->setRichSubject($ownEvent, $data);
488
		}
489
490
		return $event->setRichSubject($othersEvent, $data);
491
	}
492
493
494
	/**
495
	 * general function to generate Circle+Member advanced event.
496
	 *
497
	 * @param Circle $circle
498
	 * @param Member $member
499
	 * @param IEvent $event
500
	 *\
501
	 * @param $ownEvent
502
	 * @param $targetEvent
503
	 * @param $othersEvent
504
	 *
505
	 * @return IEvent
506
	 */
507
	private function parseCircleMemberAdvancedEvent(
508
		IEvent &$event, Circle $circle, Member $member, $ownEvent, $targetEvent, $othersEvent
509
	) {
510
511
		$data = [
512
			'author' => $this->generateViewerParameter($circle),
513
			'circle' => $this->generateCircleParameter($circle),
514
			'member' => $this->generateMemberParameter($member)
515
		];
516
517
		if ($this->isViewerTheAuthor($circle, $this->activityManager->getCurrentUserId())) {
518
			return $event->setRichSubject($ownEvent, $data);
519
		}
520
521
		if ($member->getUserId() === $this->activityManager->getCurrentUserId()) {
522
			return $event->setRichSubject($targetEvent, $data);
523
		}
524
525
		return $event->setRichSubject($othersEvent, $data);
526
	}
527
528
529
	/**
530
	 * @param IEvent $event
531
	 */
532
	private function generateParsedSubject(IEvent &$event) {
533
		$subject = $event->getRichSubject();
534
		$params = $event->getRichSubjectParameters();
535
		$ak = array_keys($params);
536
		foreach ($ak as $k) {
537
			if (is_array($params[$k])) {
538
				$subject = str_replace('{' . $k . '}', $params[$k]['parsed'], $subject);
539
			}
540
		}
541
542
		$event->setParsedSubject($subject);
543
	}
544
545
546
	/**
547
	 * @param Circle $circle
548
	 * @param string $userId
549
	 *
550
	 * @return bool
551
	 */
552
	private function isViewerTheAuthor(Circle $circle, $userId) {
553
		if ($circle->getViewer() === null) {
554
			return false;
555
		}
556
557
		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...
558
				   ->getUserId() === $userId) {
559
			return true;
560
		}
561
562
		return false;
563
	}
564
565
566
	/**
567
	 * @param Member $member
568
	 *
569
	 * @return array<string,string|integer>
570
	 */
571
	private function generateMemberParameter(Member $member) {
572
		if ($member->getViewerType() === 'user') {
573
			return $this->generateUserParameter($member->getUserId());
574
		}
575
576
		return $this->generateGroupParameter($member->getGroupId());
577
	}
578
579
	/**
580
	 * @param Circle $circle
581
	 *
582
	 * @return array <string,string|integer>
0 ignored issues
show
Documentation introduced by
Should the return type not be string|array<string,string|integer>?

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...
583
	 */
584
	private function generateViewerParameter(Circle $circle) {
585
		if ($circle->getViewer() === null) {
586
			return '';
587
		}
588
589
		return $this->generateUserParameter(
590
			$circle->getViewer()
591
				   ->getUserId()
592
		);
593
	}
594
595
596
	/**
597
	 * @param Circle $circle
598
	 *
599
	 * @return array<string,string|integer>
600
	 */
601
	private function generateCircleParameter(Circle $circle) {
602
		return [
603
			'type'   => 'circle',
604
			'id'     => $circle->getId(),
605
			'name'   => $circle->getName(),
606
			'parsed' => $circle->getName(),
607
			'link'   => Circles::generateLink($circle->getUniqueId())
608
		];
609
	}
610
611
612
	/**
613
	 * @param FederatedLink $link
614
	 *
615
	 * @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...
616
	 */
617
	private function generateRemoteCircleParameter(FederatedLink $link) {
618
		return [
619
			'type'   => 'circle',
620
			'id'     => $link->getUniqueId(),
621
			'name'   => $link->getToken() . '@' . $link->getAddress(),
622
			'parsed' => $link->getToken() . '@' . $link->getAddress()
623
		];
624
//			'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...
625
	}
626
627
628
	/**
629
	 * @param $userId
630
	 *
631
	 * @return array<string,string|integer>
632
	 */
633
	private function generateUserParameter($userId) {
634
		return [
635
			'type'   => 'user',
636
			'id'     => $userId,
637
			'name'   => \OC::$server->getUserManager()
638
									->get($userId)
639
									->getDisplayName(),
640
			'parsed' => \OC::$server->getUserManager()
641
									->get($userId)
642
									->getDisplayName()
643
		];
644
	}
645
646
647
	/**
648
	 * @param $groupId
649
	 *
650
	 * @return array<string,string|integer>
651
	 */
652
	private function generateGroupParameter($groupId) {
653
		return [
654
			'type'   => 'group',
655
			'id'     => $groupId,
656
			'name'   => $groupId,
657
			'parsed' => $groupId
658
		];
659
	}
660
}
661