Completed
Push — master ( 3c00ff...81e983 )
by Joas
10:38 queued 10:11
created

Event::isValidCommon()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 9
nc 4
nop 0
dl 0
loc 18
rs 9.2
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Joas Schilling <[email protected]>
6
 * @author Phil Davis <[email protected]>
7
 *
8
 * @license AGPL-3.0
9
 *
10
 * This code is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License, version 3,
12
 * as published by the Free Software Foundation.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License, version 3,
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
21
 *
22
 */
23
24
namespace OC\Activity;
25
26
use OCP\Activity\IEvent;
27
use OCP\RichObjectStrings\InvalidObjectExeption;
28
use OCP\RichObjectStrings\IValidator;
29
30
class Event implements IEvent {
31
32
	/** @var string */
33
	protected $app = '';
34
	/** @var string */
35
	protected $type = '';
36
	/** @var string */
37
	protected $affectedUser = '';
38
	/** @var string */
39
	protected $author = '';
40
	/** @var int */
41
	protected $timestamp = 0;
42
	/** @var string */
43
	protected $subject = '';
44
	/** @var array */
45
	protected $subjectParameters = [];
46
	/** @var string */
47
	protected $subjectParsed;
48
	/** @var string */
49
	protected $subjectRich;
50
	/** @var array */
51
	protected $subjectRichParameters;
52
	/** @var string */
53
	protected $message = '';
54
	/** @var array */
55
	protected $messageParameters = [];
56
	/** @var string */
57
	protected $messageParsed;
58
	/** @var string */
59
	protected $messageRich;
60
	/** @var array */
61
	protected $messageRichParameters;
62
	/** @var string */
63
	protected $objectType = '';
64
	/** @var int */
65
	protected $objectId = 0;
66
	/** @var string */
67
	protected $objectName = '';
68
	/** @var string */
69
	protected $link = '';
70
	/** @var string */
71
	protected $icon = '';
72
73
	/** @var IEvent */
74
	protected $child = null;
75
	/** @var IValidator */
76
	protected $richValidator;
77
78
	/**
79
	 * @param IValidator $richValidator
80
	 */
81
	public function __construct(IValidator $richValidator) {
82
		$this->richValidator = $richValidator;
83
	}
84
85
	/**
86
	 * Set the app of the activity
87
	 *
88
	 * @param string $app
89
	 * @return IEvent
90
	 * @throws \InvalidArgumentException if the app id is invalid
91
	 * @since 8.2.0
92
	 */
93 View Code Duplication
	public function setApp($app) {
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...
94
		if (!is_string($app) || $app === '' || isset($app[32])) {
95
			throw new \InvalidArgumentException('The given app is invalid');
96
		}
97
		$this->app = (string) $app;
98
		return $this;
99
	}
100
101
	/**
102
	 * @return string
103
	 */
104
	public function getApp() {
105
		return $this->app;
106
	}
107
108
	/**
109
	 * Set the type of the activity
110
	 *
111
	 * @param string $type
112
	 * @return IEvent
113
	 * @throws \InvalidArgumentException if the type is invalid
114
	 * @since 8.2.0
115
	 */
116 View Code Duplication
	public function setType($type) {
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...
117
		if (!is_string($type) || $type === '' || isset($type[255])) {
118
			throw new \InvalidArgumentException('The given type is invalid');
119
		}
120
		$this->type = (string) $type;
121
		return $this;
122
	}
123
124
	/**
125
	 * @return string
126
	 */
127
	public function getType() {
128
		return $this->type;
129
	}
130
131
	/**
132
	 * Set the affected user of the activity
133
	 *
134
	 * @param string $affectedUser
135
	 * @return IEvent
136
	 * @throws \InvalidArgumentException if the affected user is invalid
137
	 * @since 8.2.0
138
	 */
139 View Code Duplication
	public function setAffectedUser($affectedUser) {
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...
140
		if (!is_string($affectedUser) || $affectedUser === '' || isset($affectedUser[64])) {
141
			throw new \InvalidArgumentException('The given affected user is invalid');
142
		}
143
		$this->affectedUser = (string) $affectedUser;
144
		return $this;
145
	}
146
147
	/**
148
	 * @return string
149
	 */
150
	public function getAffectedUser() {
151
		return $this->affectedUser;
152
	}
153
154
	/**
155
	 * Set the author of the activity
156
	 *
157
	 * @param string $author
158
	 * @return IEvent
159
	 * @throws \InvalidArgumentException if the author is invalid
160
	 * @since 8.2.0
161
	 */
162 View Code Duplication
	public function setAuthor($author) {
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...
163
		if (!is_string($author) || isset($author[64])) {
164
			throw new \InvalidArgumentException('The given author user is invalid'. serialize($author));
165
		}
166
		$this->author = (string) $author;
167
		return $this;
168
	}
169
170
	/**
171
	 * @return string
172
	 */
173
	public function getAuthor() {
174
		return $this->author;
175
	}
176
177
	/**
178
	 * Set the timestamp of the activity
179
	 *
180
	 * @param int $timestamp
181
	 * @return IEvent
182
	 * @throws \InvalidArgumentException if the timestamp is invalid
183
	 * @since 8.2.0
184
	 */
185
	public function setTimestamp($timestamp) {
186
		if (!is_int($timestamp)) {
187
			throw new \InvalidArgumentException('The given timestamp is invalid');
188
		}
189
		$this->timestamp = (int) $timestamp;
190
		return $this;
191
	}
192
193
	/**
194
	 * @return int
195
	 */
196
	public function getTimestamp() {
197
		return $this->timestamp;
198
	}
199
200
	/**
201
	 * Set the subject of the activity
202
	 *
203
	 * @param string $subject
204
	 * @param array $parameters
205
	 * @return IEvent
206
	 * @throws \InvalidArgumentException if the subject or parameters are invalid
207
	 * @since 8.2.0
208
	 */
209 View Code Duplication
	public function setSubject($subject, array $parameters = []) {
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...
210
		if (!is_string($subject) || isset($subject[255])) {
211
			throw new \InvalidArgumentException('The given subject is invalid');
212
		}
213
		$this->subject = (string) $subject;
214
		$this->subjectParameters = $parameters;
215
		return $this;
216
	}
217
218
	/**
219
	 * @return string
220
	 */
221
	public function getSubject() {
222
		return $this->subject;
223
	}
224
225
	/**
226
	 * @return array
227
	 */
228
	public function getSubjectParameters() {
229
		return $this->subjectParameters;
230
	}
231
232
	/**
233
	 * @param string $subject
234
	 * @return $this
235
	 * @throws \InvalidArgumentException if the subject is invalid
236
	 * @since 11.0.0
237
	 */
238 View Code Duplication
	public function setParsedSubject($subject) {
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...
239
		if (!is_string($subject) || $subject === '') {
240
			throw new \InvalidArgumentException('The given parsed subject is invalid');
241
		}
242
		$this->subjectParsed = $subject;
243
		return $this;
244
	}
245
246
	/**
247
	 * @return string
248
	 * @since 11.0.0
249
	 */
250
	public function getParsedSubject() {
251
		return $this->subjectParsed;
252
	}
253
254
	/**
255
	 * @param string $subject
256
	 * @param array $parameters
257
	 * @return $this
258
	 * @throws \InvalidArgumentException if the subject or parameters are invalid
259
	 * @since 11.0.0
260
	 */
261 View Code Duplication
	public function setRichSubject($subject, array $parameters = []) {
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...
262
		if (!is_string($subject) || $subject === '') {
263
			throw new \InvalidArgumentException('The given parsed subject is invalid');
264
		}
265
		$this->subjectRich = $subject;
266
267
		if (!is_array($parameters)) {
268
			throw new \InvalidArgumentException('The given subject parameters are invalid');
269
		}
270
		$this->subjectRichParameters = $parameters;
271
272
		return $this;
273
	}
274
275
	/**
276
	 * @return string
277
	 * @since 11.0.0
278
	 */
279
	public function getRichSubject() {
280
		return $this->subjectRich;
281
	}
282
283
	/**
284
	 * @return array[]
285
	 * @since 11.0.0
286
	 */
287
	public function getRichSubjectParameters() {
288
		return $this->subjectRichParameters;
289
	}
290
291
	/**
292
	 * Set the message of the activity
293
	 *
294
	 * @param string $message
295
	 * @param array $parameters
296
	 * @return IEvent
297
	 * @throws \InvalidArgumentException if the message or parameters are invalid
298
	 * @since 8.2.0
299
	 */
300 View Code Duplication
	public function setMessage($message, array $parameters = []) {
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...
301
		if (!is_string($message) || isset($message[255])) {
302
			throw new \InvalidArgumentException('The given message is invalid');
303
		}
304
		$this->message = (string) $message;
305
		$this->messageParameters = $parameters;
306
		return $this;
307
	}
308
309
	/**
310
	 * @return string
311
	 */
312
	public function getMessage() {
313
		return $this->message;
314
	}
315
316
	/**
317
	 * @return array
318
	 */
319
	public function getMessageParameters() {
320
		return $this->messageParameters;
321
	}
322
323
	/**
324
	 * @param string $message
325
	 * @return $this
326
	 * @throws \InvalidArgumentException if the message is invalid
327
	 * @since 11.0.0
328
	 */
329
	public function setParsedMessage($message) {
330
		if (!is_string($message)) {
331
			throw new \InvalidArgumentException('The given parsed message is invalid');
332
		}
333
		$this->messageParsed = $message;
334
		return $this;
335
	}
336
337
	/**
338
	 * @return string
339
	 * @since 11.0.0
340
	 */
341
	public function getParsedMessage() {
342
		return $this->messageParsed;
343
	}
344
345
	/**
346
	 * @param string $message
347
	 * @param array $parameters
348
	 * @return $this
349
	 * @throws \InvalidArgumentException if the subject or parameters are invalid
350
	 * @since 11.0.0
351
	 */
352 View Code Duplication
	public function setRichMessage($message, array $parameters = []) {
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...
353
		if (!is_string($message)) {
354
			throw new \InvalidArgumentException('The given parsed message is invalid');
355
		}
356
		$this->messageRich = $message;
357
358
		if (!is_array($parameters)) {
359
			throw new \InvalidArgumentException('The given message parameters are invalid');
360
		}
361
		$this->messageRichParameters = $parameters;
362
363
		return $this;
364
	}
365
366
	/**
367
	 * @return string
368
	 * @since 11.0.0
369
	 */
370
	public function getRichMessage() {
371
		return $this->messageRich;
372
	}
373
374
	/**
375
	 * @return array[]
376
	 * @since 11.0.0
377
	 */
378
	public function getRichMessageParameters() {
379
		return $this->messageRichParameters;
380
	}
381
382
	/**
383
	 * Set the object of the activity
384
	 *
385
	 * @param string $objectType
386
	 * @param int $objectId
387
	 * @param string $objectName
388
	 * @return IEvent
389
	 * @throws \InvalidArgumentException if the object is invalid
390
	 * @since 8.2.0
391
	 */
392
	public function setObject($objectType, $objectId, $objectName = '') {
393
		if (!is_string($objectType) || isset($objectType[255])) {
394
			throw new \InvalidArgumentException('The given object type is invalid');
395
		}
396
		if (!is_int($objectId)) {
397
			throw new \InvalidArgumentException('The given object id is invalid');
398
		}
399
		if (!is_string($objectName) || isset($objectName[4000])) {
400
			throw new \InvalidArgumentException('The given object name is invalid');
401
		}
402
		$this->objectType = (string) $objectType;
403
		$this->objectId = (int) $objectId;
404
		$this->objectName = (string) $objectName;
405
		return $this;
406
	}
407
408
	/**
409
	 * @return string
410
	 */
411
	public function getObjectType() {
412
		return $this->objectType;
413
	}
414
415
	/**
416
	 * @return string
417
	 */
418
	public function getObjectId() {
419
		return $this->objectId;
420
	}
421
422
	/**
423
	 * @return string
424
	 */
425
	public function getObjectName() {
426
		return $this->objectName;
427
	}
428
429
	/**
430
	 * Set the link of the activity
431
	 *
432
	 * @param string $link
433
	 * @return IEvent
434
	 * @throws \InvalidArgumentException if the link is invalid
435
	 * @since 8.2.0
436
	 */
437 View Code Duplication
	public function setLink($link) {
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...
438
		if (!is_string($link) || isset($link[4000])) {
439
			throw new \InvalidArgumentException('The given link is invalid');
440
		}
441
		$this->link = (string) $link;
442
		return $this;
443
	}
444
445
	/**
446
	 * @return string
447
	 */
448
	public function getLink() {
449
		return $this->link;
450
	}
451
452
	/**
453
	 * @param string $icon
454
	 * @return $this
455
	 * @throws \InvalidArgumentException if the icon is invalid
456
	 * @since 11.0.0
457
	 */
458 View Code Duplication
	public function setIcon($icon) {
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...
459
		if (!is_string($icon) || isset($icon[4000])) {
460
			throw new \InvalidArgumentException('The given icon is invalid');
461
		}
462
		$this->icon = $icon;
463
		return $this;
464
	}
465
466
	/**
467
	 * @return string
468
	 * @since 11.0.0
469
	 */
470
	public function getIcon() {
471
		return $this->icon;
472
	}
473
474
	/**
475
	 * @param IEvent $child
476
	 * @since 11.0.0
477
	 */
478
	public function setChildEvent(IEvent $child) {
479
		$this->child = $child;
480
	}
481
482
	/**
483
	 * @return IEvent|null
484
	 * @since 11.0.0
485
	 */
486
	public function getChildEvent() {
487
		return $this->child;
488
	}
489
490
	/**
491
	 * @return bool
492
	 * @since 8.2.0
493
	 */
494
	public function isValid() {
495
		return
496
			$this->isValidCommon()
497
			&&
498
			$this->getSubject() !== ''
499
		;
500
	}
501
502
	/**
503
	 * @return bool
504
	 * @since 8.2.0
505
	 */
506 View Code Duplication
	public function isValidParsed() {
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...
507
		if ($this->getRichSubject() !== '' || !empty($this->getRichSubjectParameters())) {
508
			try {
509
				$this->richValidator->validate($this->getRichSubject(), $this->getRichSubjectParameters());
510
			} catch (InvalidObjectExeption $e) {
511
				return false;
512
			}
513
		}
514
515
		if ($this->getRichMessage() !== '' || !empty($this->getRichMessageParameters())) {
516
			try {
517
				$this->richValidator->validate($this->getRichMessage(), $this->getRichMessageParameters());
518
			} catch (InvalidObjectExeption $e) {
519
				return false;
520
			}
521
		}
522
523
		return
524
			$this->isValidCommon()
525
			&&
526
			$this->getParsedSubject() !== ''
527
		;
528
	}
529
530
	/**
531
	 * @return bool
532
	 */
533
	protected function isValidCommon() {
534
		return
535
			$this->getApp() !== ''
536
			&&
537
			$this->getType() !== ''
538
			&&
539
			$this->getAffectedUser() !== ''
540
			&&
541
			$this->getTimestamp() !== 0
542
			/**
543
			 * Disabled for BC with old activities
544
			&&
545
			$this->getObjectType() !== ''
546
			&&
547
			$this->getObjectId() !== 0
548
			 */
549
		;
550
	}
551
}
552