Completed
Push — master ( 51d638...9f66fa )
by Thomas
12:17
created

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