Completed
Push — activities ( a974df...eafd5e )
by Maxence
03:09
created

Provider::parseMemberRequestInvitation()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 26
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 26
rs 8.8571
cc 2
eloc 14
nc 2
nop 4
1
<?php
2
3
4
namespace OCA\Circles\Activity;
5
6
7
use Exception;
8
use InvalidArgumentException;
9
use OCA\Circles\Model\Circle;
10
use OCA\Circles\Model\Member;
11
use OCA\Circles\Model\SharingFrame;
12
use OCA\Circles\Service\CirclesService;
13
use OCA\Circles\Service\MiscService;
14
use OCP\Activity\IEvent;
15
use OCP\Activity\IManager;
16
use OCP\Activity\IProvider;
17
use OCP\IL10N;
18
use OCP\IURLGenerator;
19
20
21
class Provider implements IProvider {
22
23
	/** @var MiscService */
24
	protected $miscService;
25
26
	/** @var IL10N */
27
	protected $l10n;
28
29
	/** @var IURLGenerator */
30
	protected $url;
31
32
	/** @var IManager */
33
	protected $activityManager;
34
35
	public function __construct(
36
		IURLGenerator $url, IManager $activityManager, IL10N $l10n, MiscService $miscService
37
	) {
38
		$this->url = $url;
39
		$this->activityManager = $activityManager;
40
		$this->l10n = $l10n;
41
		$this->miscService = $miscService;
42
	}
43
44
45
	/**
46
	 * @param string $lang
47
	 * @param IEvent $event
48
	 * @param IEvent|null $previousEvent
49
	 *
50
	 * @return IEvent
51
	 * @since 11.0.0
52
	 */
53
	public function parse($lang, IEvent $event, IEvent $previousEvent = null) {
54
55
		if ($event->getApp() !== 'circles') {
56
			throw new \InvalidArgumentException();
57
		}
58
59
		$event = $this->parseAsMember($lang, $event);
60
		$event = $this->parseAsModerator($lang, $event);
61
//		$event = $this->parsePopulation($lang, $event);
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% 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...
62
//		$event = $this->parseRights($lang, $event);
63
//		$event = $this->parseShares($lang, $event);
64
65
		return $event;
66
	}
67
68
69
	/**
70
	 * @param string $lang
71
	 * @param IEvent $event
72
	 *
73
	 * @return IEvent
74
	 * @throws Exception
75
	 */
76
	private function parseAsMember($lang, IEvent $event) {
77
		if ($event->getType() !== 'circles_as_member') {
78
			return $event;
79
		}
80
81
		$params = $event->getSubjectParameters();
82
		$circle = Circle::fromJSON($this->l10n, $params['circle']);
83
		if ($circle === null) {
84
			return $event;
85
		}
86
87
		$event->setIcon(CirclesService::getCircleIcon($circle->getType()));
88
		switch ($event->getSubject()) {
89
			case 'circle_create':
90
				return $this->parseCircleCreate($lang, $circle, $event);
91
92
			case 'circle_delete':
93
				return $this->parseCircleDelete($lang, $circle, $event);
94
		}
95
96
		$event = $this->parseMembersAsMember($lang, $circle, $event);
97
98
		return $event;
99
	}
100
101
102
	/**
103
	 * @param $lang
104
	 * @param Circle $circle
105
	 * @param IEvent $event
106
	 *
107
	 * @return IEvent
108
	 */
109
	private function parseMembersAsMember($lang, Circle $circle, IEvent $event) {
110
		$params = $event->getSubjectParameters();
111
112
		$member = Member::fromJSON($this->l10n, $params['member']);
0 ignored issues
show
Bug introduced by
The method fromJSON() does not seem to exist on object<OCA\Circles\Model\Member>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
113
		if ($member === null) {
114
			return $event;
115
		}
116
117
		switch ($event->getSubject()) {
118
			case 'member_join':
119
				return $this->parseMemberJoin($lang, $circle, $member, $event);
120
121
			default:
122
				return $event;
123
		}
124
125
	}
126
127
128
	/**
129
	 * @param string $lang
130
	 * @param IEvent $event
131
	 *
132
	 * @return IEvent
133
	 */
134
	private function parseAsModerator($lang, IEvent $event) {
135
		if ($event->getType() !== 'circles_as_moderator') {
136
			return $event;
137
		}
138
139
		$params = $event->getSubjectParameters();
140
		$circle = Circle::fromJSON($this->l10n, $params['circle']);
141
		$member = Member::fromJSON($this->l10n, $params['member']);
0 ignored issues
show
Bug introduced by
The method fromJSON() does not seem to exist on object<OCA\Circles\Model\Member>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
142
		if ($member === null || $circle === null) {
143
			return $event;
144
		}
145
146
		$event->setIcon(CirclesService::getCircleIcon($circle->getType()));
147
148
		switch ($event->getSubject()) {
149
			case 'member_invited':
150
				return $this->parseMemberInvited($lang, $circle, $member, $event);
151
152
			case 'member_request_invitation':
153
				return $this->parseMemberRequestInvitation($lang, $circle, $member, $event);
154
155
			default:
156
				throw new InvalidArgumentException();
157
		}
158
	}
159
160
	/**
161
	 * @param string $lang
162
	 * @param Circle $circle
163
	 * @param IEvent $event
164
	 *
165
	 * @return IEvent
166
	 */
167 View Code Duplication
	private function parseCircleCreate($lang, Circle $circle, 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...
168
		if ($circle->getOwner()
169
				   ->getUserId() === $this->activityManager->getCurrentUserId()
170
		) {
171
			$event->setRichSubject(
172
				$this->l10n->t(
173
					'You created the circle {circle}'
174
				),
175
				['circle' => $this->generateCircleParameter($circle)]
176
			);
177
178
		} else {
179
			$event->setRichSubject(
180
				$this->l10n->t(
181
					'{author} created the circle {circle}'
182
				), [
183
					'author' => $author = $this->generateUserParameter(
184
						$circle->getOwner()
185
							   ->getUserId()
186
					),
187
					'circle' => $this->generateCircleParameter($circle)
188
				]
189
			);
190
		}
191
192
		return $event;
193
	}
194
195
196
	/**
197
	 * @param string $lang
198
	 * @param Circle $circle
199
	 * @param IEvent $event
200
	 *
201
	 * @return IEvent
202
	 */
203 View Code Duplication
	private function parseCircleDelete($lang, Circle $circle, 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...
204
		if ($circle->getOwner()
205
				   ->getUserId() === $this->activityManager->getCurrentUserId()
206
		) {
207
			$event->setRichSubject(
208
				$this->l10n->t(
209
					'You deleted circle {circle}'
210
				),
211
				['circle' => $this->generateCircleParameter($circle)]
212
			);
213
		} else {
214
			$event->setRichSubject(
215
				$this->l10n->t(
216
					'{author} deleted circle {circle}'
217
				), [
218
					'author' => $this->generateUserParameter(
219
						$circle->getOwner()
220
							   ->getUserId()
221
					),
222
					'circle' => $this->generateCircleParameter($circle)
223
				]
224
			);
225
		}
226
227
		return $event;
228
	}
229
230
231
	/**
232
	 * @param string $lang
233
	 * @param Circle $circle
234
	 * @param Member $member
235
	 * @param IEvent $event
236
	 *
237
	 * @return IEvent
238
	 */
239
	private function parseMemberInvited($lang, Circle $circle, Member $member, IEvent $event) {
240
241
		if ($circle->getUser()
242
				   ->getUserId() === $this->activityManager->getCurrentUserId()
243
		) {
244
			$event->setRichSubject(
245
				$this->l10n->t(
246
					'You have invited {member} into {circle}'
247
				),
248
				[
249
					'circle' => $this->generateCircleParameter($circle),
250
					'member' => $this->generateMemberParameter($member)
251
				]
252
			);
253
254
		} elseif ($member->getUserId() === $this->activityManager->getCurrentUserId()) {
255
			$event->setRichSubject(
256
				$this->l10n->t(
257
					'You have been invited into {circle} by {author}'
258
				), [
259
					'author' => $this->generateUserParameter(
260
						$circle->getUser()
261
							   ->getUserId()
262
					),
263
					'circle' => $this->generateCircleParameter($circle)
264
				]
265
			);
266
267
		} else {
268
			$event->setRichSubject(
269
				$this->l10n->t(
270
					'{member} have been invited into {circle} by {author}'
271
				), [
272
					'author' => $this->generateUserParameter(
273
						$circle->getUser()
274
							   ->getUserId()
275
					),
276
					'circle' => $this->generateCircleParameter($circle),
277
					'member' => $this->generateMemberParameter($member)
278
				]
279
			);
280
		}
281
282
		return $event;
283
	}
284
285
286
	/**
287
	 * @param $lang
288
	 * @param Circle $circle
289
	 * @param Member $member
290
	 * @param IEvent $event
291
	 *
292
	 * @return IEvent
293
	 */
294
	private function parseMemberRequestInvitation(
295
		$lang, Circle $circle, Member $member, IEvent $event
296
	) {
297
		if ($member->getUserId() === $this->activityManager->getCurrentUserId()) {
298
			$event->setRichSubject(
299
				$this->l10n->t(
300
					'You have requested an invitation to {circle}'
301
				),
302
				[
303
					'circle' => $this->generateCircleParameter($circle)
304
				]
305
			);
306
307
		} else {
308
			$event->setRichSubject(
309
				$this->l10n->t(
310
					'{author} have requested an invitation into {circle}'
311
				), [
312
					'author' => $this->generateMemberParameter($member),
313
					'circle' => $this->generateCircleParameter($circle)
314
				]
315
			);
316
		}
317
318
		return $event;
319
	}
320
321
322
	/**
323
	 * @param $lang
324
	 * @param Circle $circle
325
	 * @param Member $member
326
	 * @param IEvent $event
327
	 *
328
	 * @return IEvent
329
	 */
330
	private function parseMemberJoin($lang, Circle $circle, Member $member, IEvent $event) {
331
		return $event;
332
	}
333
334
//
0 ignored issues
show
Unused Code Comprehensibility introduced by
40% 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...
335
//
336
//	/**
337
//	 * @param string $lang
338
//	 * @param IEvent $event
339
//	 *
340
//	 * @return IEvent
341
//	 */
342
//	private function parsePopulation($lang, IEvent $event) {
343
//		if ($event->getType() !== 'circles_population') {
344
//			return $event;
345
//		}
346
//
347
//		return $event;
348
//	}
349
//
350
//
351
//	/**
352
//	 * @param string $lang
353
//	 * @param IEvent $event
354
//	 *
355
//	 * @return IEvent
356
//	 */
357
//	private function parseRights($lang, IEvent $event) {
358
//		if ($event->getType() !== 'circles_rights') {
359
//			return $event;
360
//		}
361
//
362
//		return $event;
363
//	}
364
//
365
//
366
//	/**
367
//	 * @param string $lang
368
//	 * @param IEvent $event
369
//	 *
370
//	 * @return IEvent
371
//	 */
372
//	private function parseShares($lang, IEvent $event) {
373
//		if ($event->getType() !== 'circles_shares') {
374
//			return $event;
375
//		}
376
//
377
//		return $event;
378
//	}
379
//
380
//	private function parseMood(IEvent &$event, $mood) {
381
//
382
//		if (key_exists('website', $mood)) {
383
//			$event->setRichMessage(
384
//				$mood['text'] . '{opengraph}',
385
//				['opengraph' => $this->generateOpenGraphParameter('_id_', $mood['website'])]
386
//			);
387
//		} else {
388
//			$event->setParsedMessage($mood['text']);
389
//		}
390
//
391
//	}
392
393
394
	private function parseActivityHeader(IEvent &$event, SharingFrame $frame) {
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
395
396
		$this->activityManager->getCurrentUserId();
397
398
		if ($frame->getAuthor() === $this->activityManager->getCurrentUserId()
399
			&& $frame->getCloudId() === null
400
		) {
401
402
			$event->setParsedSubject(
403
				$this->l10n->t(
404
					'You shared a mood with %1$s', ['circle1, circle2']
405
				)
406
			)
407
				  ->setRichSubject(
408
					  $this->l10n->t(
409
						  'You shared a mood with {circles}'
410
					  ),
411
					  ['circles' => $this->generateCircleParameter($frame)]
412
413
				  );
414
415
		} else {
416
417
			$author = $this->generateUserParameter($frame);
418
			$event->setParsedSubject(
419
				$this->l10n->t(
420
					'%1$s shared a mood with %2$s', [
421
													  $author['name'],
422
													  'circle1, circle2'
423
												  ]
424
				)
425
			)
426
				  ->setRichSubject(
427
					  $this->l10n->t(
428
						  '{author} shared a mood with {circles}'
429
					  ), [
430
						  'author'  => $author,
431
						  'circles' => $this->generateCircleParameter($frame)
432
					  ]
433
				  );
434
		}
435
	}
436
437
438
	private function generateCircleParameter(Circle $circle) {
439
		return [
440
			'type' => 'circle',
441
			'id'   => $circle->getId(),
442
			'name' => $circle->getName(),
443
			'link' => \OC::$server->getURLGenerator()
444
								  ->linkToRoute('circles.Navigation.navigate')
445
					  . '#' . $circle->getId()
446
		];
447
	}
448
449
450
	private function generateMemberParameter(Member $member) {
451
		return $this->generateUserParameter($member->getUserId());
452
	}
453
454
	/**
455
	 * @param $userId
456
	 *
457
	 * @return array
458
	 */
459
	private function generateUserParameter($userId) {
460
		return [
461
			'type' => 'user',
462
			'id'   => $userId,
463
			'name' => $userId
464
		];
465
	}
466
}
467