Completed
Push — master ( a7d2b9...a7d2b9 )
by Maxence
06:32 queued 02:55
created

Provider::parseInvitation()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 2
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\SharingFrame;
11
use OCA\Circles\Service\CirclesService;
12
use OCA\Circles\Service\MiscService;
13
use OCP\Activity\IEvent;
14
use OCP\Activity\IManager;
15
use OCP\Activity\IProvider;
16
use OCP\IL10N;
17
use OCP\IURLGenerator;
18
19
20
class Provider implements IProvider {
21
22
	/** @var MiscService */
23
	protected $miscService;
24
25
	/** @var IL10N */
26
	protected $l10n;
27
28
	/** @var IURLGenerator */
29
	protected $url;
30
31
	/** @var IManager */
32
	protected $activityManager;
33
34
	public function __construct(
35
		IURLGenerator $url, IManager $activityManager, IL10N $l10n, MiscService $miscService
36
	) {
37
		$this->url = $url;
38
		$this->activityManager = $activityManager;
39
		$this->l10n = $l10n;
40
		$this->miscService = $miscService;
41
	}
42
43
44
	/**
45
	 * @param string $lang
46
	 * @param IEvent $event
47
	 * @param IEvent|null $previousEvent
48
	 *
49
	 * @return IEvent
50
	 * @since 11.0.0
51
	 */
52
	public function parse($lang, IEvent $event, IEvent $previousEvent = null) {
53
54
		if ($event->getApp() !== 'circles') {
55
			throw new \InvalidArgumentException();
56
		}
57
58
		$event = $this->parseCreation($lang, $event);
59
		$event = $this->parseInvitation($lang, $event);
60
		$event = $this->parsePopulation($lang, $event);
61
		$event = $this->parseRights($lang, $event);
62
		$event = $this->parseShares($lang, $event);
63
64
		return $event;
65
	}
66
67
68
	/**
69
	 * @param string $lang
70
	 * @param IEvent $event
71
	 *
72
	 * @return IEvent
73
	 * @throws Exception
74
	 */
75
	private function parseCreation($lang, IEvent $event) {
76
		if ($event->getType() !== 'circles_creation') {
77
			return $event;
78
		}
79
80
		switch ($event->getSubject()) {
81
			case 'create':
82
				return $this->parseCreationCreate($lang, $event);
83
84
			case 'delete':
85
				return $this->parseCreationDelete($lang, $event);
86
87
			default:
88
				throw new InvalidArgumentException();
89
		}
90
	}
91
92
93
	/**
94
	 * @param string $lang
95
	 * @param IEvent $event
96
	 *
97
	 * @return IEvent
98
	 */
99
	private function parseCreationCreate($lang, IEvent $event) {
100
101
		$params = $event->getSubjectParameters();
102
		$this->miscService->log(var_export($params, true));
103
104
		$circle = Circle::fromJSON($this->l10n, $params['circle']);
105
        $event->setIcon(CirclesService::getCircleIcon($circle->getType()));
106
107
//          $this->url->getAbsoluteURL($this->url->imagePath('circles', 'black_circle.svg'))
0 ignored issues
show
Unused Code Comprehensibility introduced by
66% 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...
108
//        );
109
110
//        $frame = SharingFrame::fromJSON($params['share']);
0 ignored issues
show
Unused Code Comprehensibility introduced by
53% 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...
111
//
112
//        if ($frame === null) {
113
//          throw new \InvalidArgumentException();
114
//        }
115
//        $mood = $frame->getPayload();
116
//        $this->parseActivityHeader($event, $frame);
117
//        $this->parseMood($event, $mood);
118
//        break;
119
//
120
//      default:
121
//        throw new \InvalidArgumentException();
122
//    }
123
124
125
		return $event;
126
//		throw new InvalidArgumentException();
0 ignored issues
show
Unused Code Comprehensibility introduced by
56% 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...
127
	}
128
129
130
	/**
131
	 * @param string $lang
132
	 * @param IEvent $event
133
	 *
134
	 * @return IEvent
135
	 */
136
	private function parseCreationDelete($lang, IEvent $event) {
137
		return $event;
138
//		throw new InvalidArgumentException();
0 ignored issues
show
Unused Code Comprehensibility introduced by
56% 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...
139
	}
140
141
142
	/**
143
	 * @param string $lang
144
	 * @param IEvent $event
145
	 *
146
	 * @return IEvent
147
	 */
148
	private function parseInvitation($lang, IEvent $event) {
149
		if ($event->getType() !== 'circles_invitation') {
150
			return $event;
151
		}
152
153
		return $event;
154
	}
155
156
157
	/**
158
	 * @param string $lang
159
	 * @param IEvent $event
160
	 *
161
	 * @return IEvent
162
	 */
163
	private function parsePopulation($lang, IEvent $event) {
164
		if ($event->getType() !== 'circles_population') {
165
			return $event;
166
		}
167
168
		return $event;
169
	}
170
171
172
	/**
173
	 * @param string $lang
174
	 * @param IEvent $event
175
	 *
176
	 * @return IEvent
177
	 */
178
	private function parseRights($lang, IEvent $event) {
179
		if ($event->getType() !== 'circles_rights') {
180
			return $event;
181
		}
182
183
		return $event;
184
	}
185
186
187
	/**
188
	 * @param string $lang
189
	 * @param IEvent $event
190
	 *
191
	 * @return IEvent
192
	 */
193
	private function parseShares($lang, IEvent $event) {
194
		if ($event->getType() !== 'circles_shares') {
195
			return $event;
196
		}
197
198
		return $event;
199
	}
200
201
//	private function parseMood(IEvent &$event, $mood) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
57% 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...
202
//
203
//		if (key_exists('website', $mood)) {
204
//			$event->setRichMessage(
205
//				$mood['text'] . '{opengraph}',
206
//				['opengraph' => $this->generateOpenGraphParameter('_id_', $mood['website'])]
207
//			);
208
//		} else {
209
//			$event->setParsedMessage($mood['text']);
210
//		}
211
//
212
//	}
213
214
215
	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...
216
217
		$this->activityManager->getCurrentUserId();
218
219
		if ($frame->getAuthor() === $this->activityManager->getCurrentUserId()
220
			&& $frame->getCloudId() === null
221
		) {
222
223
			$event->setParsedSubject(
224
				$this->l10n->t(
225
					'You shared a mood with %1$s', ['circle1, circle2']
226
				)
227
			)
228
				  ->setRichSubject(
229
					  $this->l10n->t(
230
						  'You shared a mood with {circles}'
231
					  ),
232
					  ['circles' => $this->generateCircleParameter($frame)]
233
234
				  );
235
236
		} else {
237
238
			$author = $this->generateUserParameter($frame);
239
			$event->setParsedSubject(
240
				$this->l10n->t(
241
					'%1$s shared a mood with %2$s', [
242
													  $author['name'],
243
													  'circle1, circle2'
244
												  ]
245
				)
246
			)
247
				  ->setRichSubject(
248
					  $this->l10n->t(
249
						  '{author} shared a mood with {circles}'
250
					  ), [
251
						  'author'  => $author,
252
						  'circles' => $this->generateCircleParameter($frame)
253
					  ]
254
				  );
255
		}
256
	}
257
258
259
	private function generateOpenGraphParameter($id, $website) {
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
260
		return [
261
			'type'        => 'open-graph',
262
			'id'          => $id,
263
			'name'        => $website['title'],
264
			'description' => $website['description'],
265
			'website'     => $website['website'],
266
			'thumb'       => \OC::$server->getURLGenerator()
267
										 ->linkToRoute('mood.Tools.binFromExternalImage') . '?url='
268
							 . rawurlencode($website['thumb']),
269
			'link'        => $website['url']
270
		];
271
	}
272
273
274
	private function generateCircleParameter(SharingFrame $frame) {
275
		return [
276
			'type' => 'circle',
277
			'id'   => $frame->getCircleId(),
278
			'name' => $frame->getCircleName(),
279
			'link' => \OC::$server->getURLGenerator()
280
								  ->linkToRoute('circles.Navigation.navigate')
281
					  . '#' . $frame->getCircleId()
282
		];
283
	}
284
285
286
	/**
287
	 * @param SharingFrame $frame
288
	 *
289
	 * @return array
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...
290
	 */
291
	private function generateUserParameter(SharingFrame $frame) {
292
		$host = '';
293
		if ($frame->getCloudId() !== null) {
294
			$host = '@' . $frame->getCloudId();
295
		}
296
297
		return [
298
			'type' => 'user',
299
			'id'   => $frame->getAuthor(),
300
			'name' => $frame->getAuthor() . $host
301
		];
302
	}
303
}
304