Completed
Push — master ( e5fd9c...a0cb80 )
by Christoph
19:49 queued 10:51
created

Notification::getIcon()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
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 Morris Jobke <[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\Notification;
25
26
27
use OCP\Notification\IAction;
28
use OCP\Notification\INotification;
29
30
class Notification implements INotification {
31
	/** @var string */
32
	protected $app;
33
34
	/** @var string */
35
	protected $user;
36
37
	/** @var \DateTime */
38
	protected $dateTime;
39
40
	/** @var string */
41
	protected $objectType;
42
43
	/** @var string */
44
	protected $objectId;
45
46
	/** @var string */
47
	protected $subject;
48
49
	/** @var array */
50
	protected $subjectParameters;
51
52
	/** @var string */
53
	protected $subjectParsed;
54
55
	/** @var string */
56
	protected $message;
57
58
	/** @var array */
59
	protected $messageParameters;
60
61
	/** @var string */
62
	protected $messageParsed;
63
64
	/** @var string */
65
	protected $link;
66
67
	/** @var string */
68
	protected $icon;
69
70
	/** @var array */
71
	protected $actions;
72
73
	/** @var array */
74
	protected $actionsParsed;
75
76
	/** @var bool */
77
	protected $hasPrimaryAction;
78
79
	/** @var bool */
80
	protected $hasPrimaryParsedAction;
81
82
	/**
83
	 * Constructor
84
	 */
85
	public function __construct() {
86
		$this->app = '';
87
		$this->user = '';
88
		$this->dateTime = new \DateTime();
89
		$this->dateTime->setTimestamp(0);
90
		$this->objectType = '';
91
		$this->objectId = '';
92
		$this->subject = '';
93
		$this->subjectParameters = [];
94
		$this->subjectParsed = '';
95
		$this->message = '';
96
		$this->messageParameters = [];
97
		$this->messageParsed = '';
98
		$this->link = '';
99
		$this->icon = '';
100
		$this->actions = [];
101
		$this->actionsParsed = [];
102
	}
103
104
	/**
105
	 * @param string $app
106
	 * @return $this
107
	 * @throws \InvalidArgumentException if the app id is invalid
108
	 * @since 8.2.0
109
	 */
110 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...
111
		if (!is_string($app) || $app === '' || isset($app[32])) {
112
			throw new \InvalidArgumentException('The given app name is invalid');
113
		}
114
		$this->app = $app;
115
		return $this;
116
	}
117
118
	/**
119
	 * @return string
120
	 * @since 8.2.0
121
	 */
122
	public function getApp() {
123
		return $this->app;
124
	}
125
126
	/**
127
	 * @param string $user
128
	 * @return $this
129
	 * @throws \InvalidArgumentException if the user id is invalid
130
	 * @since 8.2.0
131
	 */
132 View Code Duplication
	public function setUser($user) {
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...
133
		if (!is_string($user) || $user === '' || isset($user[64])) {
134
			throw new \InvalidArgumentException('The given user id is invalid');
135
		}
136
		$this->user = $user;
137
		return $this;
138
	}
139
140
	/**
141
	 * @return string
142
	 * @since 8.2.0
143
	 */
144
	public function getUser() {
145
		return $this->user;
146
	}
147
148
	/**
149
	 * @param \DateTime $dateTime
150
	 * @return $this
151
	 * @throws \InvalidArgumentException if the $dateTime is invalid
152
	 * @since 9.0.0
153
	 */
154
	public function setDateTime(\DateTime $dateTime) {
155
		if ($dateTime->getTimestamp() === 0) {
156
			throw new \InvalidArgumentException('The given date time is invalid');
157
		}
158
		$this->dateTime = $dateTime;
159
		return $this;
160
	}
161
162
	/**
163
	 * @return \DateTime
164
	 * @since 9.0.0
165
	 */
166
	public function getDateTime() {
167
		return $this->dateTime;
168
	}
169
170
	/**
171
	 * @param string $type
172
	 * @param string $id
173
	 * @return $this
174
	 * @throws \InvalidArgumentException if the object type or id is invalid
175
	 * @since 8.2.0 - 9.0.0: Type of $id changed to string
176
	 */
177
	public function setObject($type, $id) {
178
		if (!is_string($type) || $type === '' || isset($type[64])) {
179
			throw new \InvalidArgumentException('The given object type is invalid');
180
		}
181
		$this->objectType = $type;
182
183
		if (!is_int($id) && (!is_string($id) || $id === '' || isset($id[64]))) {
184
			throw new \InvalidArgumentException('The given object id is invalid');
185
		}
186
		$this->objectId = (string) $id;
187
		return $this;
188
	}
189
190
	/**
191
	 * @return string
192
	 * @since 8.2.0
193
	 */
194
	public function getObjectType() {
195
		return $this->objectType;
196
	}
197
198
	/**
199
	 * @return string
200
	 * @since 8.2.0 - 9.0.0: Return type changed to string
201
	 */
202
	public function getObjectId() {
203
		return $this->objectId;
204
	}
205
206
	/**
207
	 * @param string $subject
208
	 * @param array $parameters
209
	 * @return $this
210
	 * @throws \InvalidArgumentException if the subject or parameters are invalid
211
	 * @since 8.2.0
212
	 */
213 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...
214
		if (!is_string($subject) || $subject === '' || isset($subject[64])) {
215
			throw new \InvalidArgumentException('The given subject is invalid');
216
		}
217
		$this->subject = $subject;
218
219
		if (!is_array($parameters)) {
220
			throw new \InvalidArgumentException('The given subject parameters are invalid');
221
		}
222
		$this->subjectParameters = $parameters;
223
		return $this;
224
	}
225
226
	/**
227
	 * @return string
228
	 * @since 8.2.0
229
	 */
230
	public function getSubject() {
231
		return $this->subject;
232
	}
233
234
	/**
235
	 * @return string[]
236
	 * @since 8.2.0
237
	 */
238
	public function getSubjectParameters() {
239
		return $this->subjectParameters;
240
	}
241
242
	/**
243
	 * @param string $subject
244
	 * @return $this
245
	 * @throws \InvalidArgumentException if the subject is invalid
246
	 * @since 8.2.0
247
	 */
248
	public function setParsedSubject($subject) {
249
		if (!is_string($subject) || $subject === '') {
250
			throw new \InvalidArgumentException('The given parsed subject is invalid');
251
		}
252
		$this->subjectParsed = $subject;
253
		return $this;
254
	}
255
256
	/**
257
	 * @return string
258
	 * @since 8.2.0
259
	 */
260
	public function getParsedSubject() {
261
		return $this->subjectParsed;
262
	}
263
264
	/**
265
	 * @param string $message
266
	 * @param array $parameters
267
	 * @return $this
268
	 * @throws \InvalidArgumentException if the message or parameters are invalid
269
	 * @since 8.2.0
270
	 */
271 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...
272
		if (!is_string($message) || $message === '' || isset($message[64])) {
273
			throw new \InvalidArgumentException('The given message is invalid');
274
		}
275
		$this->message = $message;
276
277
		if (!is_array($parameters)) {
278
			throw new \InvalidArgumentException('The given message parameters are invalid');
279
		}
280
		$this->messageParameters = $parameters;
281
		return $this;
282
	}
283
284
	/**
285
	 * @return string
286
	 * @since 8.2.0
287
	 */
288
	public function getMessage() {
289
		return $this->message;
290
	}
291
292
	/**
293
	 * @return string[]
294
	 * @since 8.2.0
295
	 */
296
	public function getMessageParameters() {
297
		return $this->messageParameters;
298
	}
299
300
	/**
301
	 * @param string $message
302
	 * @return $this
303
	 * @throws \InvalidArgumentException if the message is invalid
304
	 * @since 8.2.0
305
	 */
306
	public function setParsedMessage($message) {
307
		if (!is_string($message) || $message === '') {
308
			throw new \InvalidArgumentException('The given parsed message is invalid');
309
		}
310
		$this->messageParsed = $message;
311
		return $this;
312
	}
313
314
	/**
315
	 * @return string
316
	 * @since 8.2.0
317
	 */
318
	public function getParsedMessage() {
319
		return $this->messageParsed;
320
	}
321
322
	/**
323
	 * @param string $link
324
	 * @return $this
325
	 * @throws \InvalidArgumentException if the link is invalid
326
	 * @since 8.2.0
327
	 */
328 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...
329
		if (!is_string($link) || $link === '' || isset($link[4000])) {
330
			throw new \InvalidArgumentException('The given link is invalid');
331
		}
332
		$this->link = $link;
333
		return $this;
334
	}
335
336
	/**
337
	 * @return string
338
	 * @since 8.2.0
339
	 */
340
	public function getLink() {
341
		return $this->link;
342
	}
343
344
	/**
345
	 * @param string $icon
346
	 * @return $this
347
	 * @throws \InvalidArgumentException if the icon is invalid
348
	 * @since 9.2.0
349
	 */
350 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...
351
		if (!is_string($icon) || $icon === '' || isset($icon[4000])) {
352
			throw new \InvalidArgumentException('The given icon is invalid');
353
		}
354
		$this->icon = $icon;
355
		return $this;
356
	}
357
358
	/**
359
	 * @return string
360
	 * @since 9.2.0
361
	 */
362
	public function getIcon() {
363
		return $this->icon;
364
	}
365
366
	/**
367
	 * @return IAction
368
	 * @since 8.2.0
369
	 */
370
	public function createAction() {
371
		return new Action();
372
	}
373
374
	/**
375
	 * @param IAction $action
376
	 * @return $this
377
	 * @throws \InvalidArgumentException if the action is invalid
378
	 * @since 8.2.0
379
	 */
380 View Code Duplication
	public function addAction(IAction $action) {
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...
381
		if (!$action->isValid()) {
382
			throw new \InvalidArgumentException('The given action is invalid');
383
		}
384
385
		if ($action->isPrimary()) {
386
			if ($this->hasPrimaryAction) {
387
				throw new \InvalidArgumentException('The notification already has a primary action');
388
			}
389
390
			$this->hasPrimaryAction = true;
391
		}
392
393
		$this->actions[] = $action;
394
		return $this;
395
	}
396
397
	/**
398
	 * @return IAction[]
399
	 * @since 8.2.0
400
	 */
401
	public function getActions() {
402
		return $this->actions;
403
	}
404
405
	/**
406
	 * @param IAction $action
407
	 * @return $this
408
	 * @throws \InvalidArgumentException if the action is invalid
409
	 * @since 8.2.0
410
	 */
411 View Code Duplication
	public function addParsedAction(IAction $action) {
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...
412
		if (!$action->isValidParsed()) {
413
			throw new \InvalidArgumentException('The given parsed action is invalid');
414
		}
415
416
		if ($action->isPrimary()) {
417
			if ($this->hasPrimaryParsedAction) {
418
				throw new \InvalidArgumentException('The notification already has a primary action');
419
			}
420
421
			$this->hasPrimaryParsedAction = true;
422
423
			// Make sure the primary action is always the first one
424
			array_unshift($this->actionsParsed, $action);
425
		} else {
426
			$this->actionsParsed[] = $action;
427
		}
428
429
		return $this;
430
	}
431
432
	/**
433
	 * @return IAction[]
434
	 * @since 8.2.0
435
	 */
436
	public function getParsedActions() {
437
		return $this->actionsParsed;
438
	}
439
440
	/**
441
	 * @return bool
442
	 * @since 8.2.0
443
	 */
444
	public function isValid() {
445
		return
446
			$this->isValidCommon()
447
			&&
448
			$this->getSubject() !== ''
449
		;
450
	}
451
452
	/**
453
	 * @return bool
454
	 * @since 8.2.0
455
	 */
456
	public function isValidParsed() {
457
		return
458
			$this->isValidCommon()
459
			&&
460
			$this->getParsedSubject() !== ''
461
		;
462
	}
463
464
	/**
465
	 * @return bool
466
	 */
467
	protected function isValidCommon() {
468
		return
469
			$this->getApp() !== ''
470
			&&
471
			$this->getUser() !== ''
472
			&&
473
			$this->getDateTime()->getTimestamp() !== 0
474
			&&
475
			$this->getObjectType() !== ''
476
			&&
477
			$this->getObjectId() !== ''
478
		;
479
	}
480
}
481