Completed
Push — master ( 9dd896...c84001 )
by Oliver
7s
created

ApnsMessage::getSound()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
/*
4
 * This file is part of the MobileNotif package.
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace LinkValue\MobileNotif\Model;
11
12
/**
13
 * Apple Push Notification Service Message implementation.
14
 *
15
 * Refer to APNS documentation for more details.
16
 *
17
 * @see https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/TheNotificationPayload.html
18
 *
19
 * @author  Jamal Youssefi <[email protected]>
20
 * @author  Valentin Coulon <[email protected]>
21
 */
22
class ApnsMessage extends Message
23
{
24
    // Default values
25
    const DEFAULT_SOUND = 'default';
26
27
    /**
28
     * @var string
29
     */
30
    private $simpleAlert;
31
32
    /**
33
     * @var string
34
     */
35
    private $alertTitle;
36
37
    /**
38
     * @var string
39
     */
40
    private $alertBody;
41
42
    /**
43
     * @var string
44
     */
45
    private $alertTitleLocKey;
46
47
    /**
48
     * @var array
49
     */
50
    private $alertTitleLocArgs;
51
52
    /**
53
     * @var string
54
     */
55
    private $alertActionLocKey;
56
57
    /**
58
     * @var string
59
     */
60
    private $alertLocKey;
61
62
    /**
63
     * @var array
64
     */
65
    private $alertLocArgs;
66
67
    /**
68
     * @var string
69
     */
70
    private $alertLaunchImage;
71
72
    /**
73
     * @var int
74
     */
75
    private $badge;
76
77
    /**
78
     * @var string
79
     */
80
    private $sound;
81
82
    /**
83
     * @var int
84
     */
85
    private $contentAvailable;
86
87
    /**
88
     * @var string
89
     */
90
    private $category;
91
92
    /**
93
     * @var array
94
     */
95
    private $data;
96
97
    /**
98
     * Constructor
99
     */
100 41
    public function __construct()
101
    {
102 41
        parent::__construct();
103
104 41
        $this->sound = self::DEFAULT_SOUND;
105 41
        $this->alertTitleLocArgs = array();
106 41
        $this->alertLocArgs = array();
107 41
        $this->data = array();
108 41
    }
109
110
    /**
111
     * Get full message payload.
112
     *
113
     * @return array
114
     */
115 5
    public function getPayload()
116
    {
117
        // APNS base payload structure
118
        $payload = array(
119 5
            'aps' => array(),
120 5
        );
121
122
        // Build notification
123 5
        if ($this->getPayloadAlertKeyValue()) {
124 2
            $payload['aps']['alert'] = $this->getPayloadAlertKeyValue();
125 2
        }
126
127
        // Build extra content
128 5
        if (!is_null($this->getBadge())) {
129 1
            $payload['aps']['badge'] = $this->getBadge();
130 1
        }
131 5
        if ($this->getSound() !== self::DEFAULT_SOUND) {
132 1
            $payload['aps']['sound'] = $this->getSound();
133 1
        }
134 5
        if (!is_null($this->getContentAvailable())) {
135 1
            $payload['aps']['content-available'] = $this->getContentAvailable();
136 1
        }
137 5
        if ($this->getCategory()) {
138 1
            $payload['aps']['category'] = $this->getCategory();
139 1
        }
140 5
        if ($this->getData()) {
141 1
            $payload['data'] = $this->getData();
142 1
        }
143
144
        // Return payload
145 5
        return $payload;
146
    }
147
148
    /**
149
     * Get the value of Simple Alert.
150
     *
151
     * @return string
152
     */
153 6
    public function getSimpleAlert()
154
    {
155 6
        return $this->simpleAlert;
156
    }
157
158
    /**
159
     * Set the value of Simple Alert.
160
     *
161
     * @param string $simpleAlert
162
     *
163
     * @return self
164
     */
165 3
    public function setSimpleAlert($simpleAlert)
166
    {
167 3
        if (!is_string($simpleAlert)) {
168 1
            throw new \InvalidArgumentException('Simple Alert must be set with a value of type "string".');
169
        }
170
171 2
        $this->simpleAlert = $simpleAlert;
172
173 2
        return $this;
174
    }
175
176
    /**
177
     * Get the value of Alert Title.
178
     *
179
     * @return string
180
     */
181 5
    public function getAlertTitle()
182
    {
183 5
        return $this->alertTitle;
184
    }
185
186
    /**
187
     * Set the value of Alert Title.
188
     *
189
     * @param string $alertTitle
190
     *
191
     * @return self
192
     */
193 2
    public function setAlertTitle($alertTitle)
194
    {
195 2
        $this->alertTitle = $alertTitle;
196
197 2
        return $this;
198
    }
199
200
    /**
201
     * Get the value of Alert Body.
202
     *
203
     * @return string
204
     */
205 5
    public function getAlertBody()
206
    {
207 5
        return $this->alertBody;
208
    }
209
210
    /**
211
     * Set the value of Alert Body.
212
     *
213
     * @param string $alertBody
214
     *
215
     * @return self
216
     */
217 2
    public function setAlertBody($alertBody)
218
    {
219 2
        $this->alertBody = $alertBody;
220
221 2
        return $this;
222
    }
223
224
    /**
225
     * Get the value of Alert Title Loc Key.
226
     *
227
     * @return string
228
     */
229 5
    public function getAlertTitleLocKey()
230
    {
231 5
        return $this->alertTitleLocKey;
232
    }
233
234
    /**
235
     * Set the value of Alert Title Loc Key.
236
     *
237
     * @param string $alertTitleLocKey
238
     *
239
     * @return self
240
     */
241 2
    public function setAlertTitleLocKey($alertTitleLocKey)
242
    {
243 2
        $this->alertTitleLocKey = $alertTitleLocKey;
244
245 2
        return $this;
246
    }
247
248
    /**
249
     * Get the value of Alert Title Loc Args.
250
     *
251
     * @return array
252
     */
253 5
    public function getAlertTitleLocArgs()
254
    {
255 5
        return $this->alertTitleLocArgs;
256
    }
257
258
    /**
259
     * Set the value of Alert Title Loc Args.
260
     *
261
     * @param array $alertTitleLocArgs
262
     *
263
     * @return self
264
     */
265 2
    public function setAlertTitleLocArgs(array $alertTitleLocArgs)
266
    {
267 2
        $this->alertTitleLocArgs = $alertTitleLocArgs;
268
269 2
        return $this;
270
    }
271
272
    /**
273
     * Get the value of Alert Action Loc Key.
274
     *
275
     * @return string
276
     */
277 5
    public function getAlertActionLocKey()
278
    {
279 5
        return $this->alertActionLocKey;
280
    }
281
282
    /**
283
     * Set the value of Alert Action Loc Key.
284
     *
285
     * @param string $alertActionLocKey
286
     *
287
     * @return self
288
     */
289 2
    public function setAlertActionLocKey($alertActionLocKey)
290
    {
291 2
        $this->alertActionLocKey = $alertActionLocKey;
292
293 2
        return $this;
294
    }
295
296
    /**
297
     * Get the value of Alert Loc Key.
298
     *
299
     * @return string
300
     */
301 5
    public function getAlertLocKey()
302
    {
303 5
        return $this->alertLocKey;
304
    }
305
306
    /**
307
     * Set the value of Alert Loc Key.
308
     *
309
     * @param string $alertLocKey
310
     *
311
     * @return self
312
     */
313 2
    public function setAlertLocKey($alertLocKey)
314
    {
315 2
        $this->alertLocKey = $alertLocKey;
316
317 2
        return $this;
318
    }
319
320
    /**
321
     * Get the value of Alert Loc Args.
322
     *
323
     * @return array
324
     */
325 5
    public function getAlertLocArgs()
326
    {
327 5
        return $this->alertLocArgs;
328
    }
329
330
    /**
331
     * Set the value of Alert Loc Args.
332
     *
333
     * @param array $alertLocArgs
334
     *
335
     * @return self
336
     */
337 2
    public function setAlertLocArgs(array $alertLocArgs)
338
    {
339 2
        $this->alertLocArgs = $alertLocArgs;
340
341 2
        return $this;
342
    }
343
344
    /**
345
     * Get the value of Alert Launch Image.
346
     *
347
     * @return string
348
     */
349 5
    public function getAlertLaunchImage()
350
    {
351 5
        return $this->alertLaunchImage;
352
    }
353
354
    /**
355
     * Set the value of Alert Launch Image.
356
     *
357
     * @param string $alertLaunchImage
358
     *
359
     * @return self
360
     */
361 2
    public function setAlertLaunchImage($alertLaunchImage)
362
    {
363 2
        $this->alertLaunchImage = $alertLaunchImage;
364
365 2
        return $this;
366
    }
367
368
    /**
369
     * Get the value of Badge.
370
     *
371
     * @return int
372
     */
373 6
    public function getBadge()
374
    {
375 6
        return $this->badge;
376
    }
377
378
    /**
379
     * Set the value of Badge.
380
     *
381
     * @param int $badge
382
     *
383
     * @return self
384
     */
385 2
    public function setBadge($badge)
386
    {
387 2
        $this->badge = (int) $badge;
388
389 2
        return $this;
390
    }
391
392
    /**
393
     * Get the value of Sound.
394
     *
395
     * @return string
396
     */
397 7
    public function getSound()
398
    {
399 7
        return $this->sound;
400
    }
401
402
    /**
403
     * Set the value of Sound.
404
     *
405
     * @param string $sound
406
     *
407
     * @return self
408
     */
409 2
    public function setSound($sound)
410
    {
411 2
        $this->sound = $sound;
412
413 2
        return $this;
414
    }
415
416
    /**
417
     * Get the value of Content Available.
418
     *
419
     * @return int
420
     */
421 6
    public function getContentAvailable()
422
    {
423 6
        return $this->contentAvailable;
424
    }
425
426
    /**
427
     * Set the value of Content Available.
428
     *
429
     * @param int $contentAvailable
430
     *
431
     * @return self
432
     */
433 2
    public function setContentAvailable($contentAvailable)
434
    {
435 2
        $this->contentAvailable = (int) $contentAvailable;
436
437 2
        return $this;
438
    }
439
440
    /**
441
     * Get the value of Category.
442
     *
443
     * @return string
444
     */
445 6
    public function getCategory()
446
    {
447 6
        return $this->category;
448
    }
449
450
    /**
451
     * Set the value of Category.
452
     *
453
     * @param string $category
454
     *
455
     * @return self
456
     */
457 2
    public function setCategory($category)
458
    {
459 2
        $this->category = $category;
460
461 2
        return $this;
462
    }
463
464
    /**
465
     * Get the data array.
466
     *
467
     * @return array
468
     */
469 7
    public function getData()
470
    {
471 7
        return $this->data;
472
    }
473
474
    /**
475
     * Set the data array.
476
     *
477
     * @param array $data
478
     *
479
     * @return self
480
     */
481 3
    public function setData(array $data)
482
    {
483 3
        $this->data = $data;
484
485 3
        return $this;
486
    }
487
488
    /**
489
     * Set a key/value pair in the data array.
490
     *
491
     * @param string|int $key
492
     * @param mixed $value
493
     *
494
     * @return self
495
     */
496 2 View Code Duplication
    public function addData($key, $value)
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...
497
    {
498 2
        if (!is_string($key) && !is_int($key)) {
499 1
            throw new \InvalidArgumentException('Data keys must be of type "string" or "integer".');
500
        }
501
502 1
        $data = $this->getData();
503
504 1
        $data[$key] = $value;
505
506 1
        return $this->setData($data);
507
    }
508
509
    /**
510
     * Get the value of the payload "alert" key.
511
     *
512
     * @return string|array
513
     */
514 5
    private function getPayloadAlertKeyValue()
515
    {
516
        // Alert "string" (simple alert)
517 5
        if ($this->getSimpleAlert()) {
518 1
            return $this->getSimpleAlert();
519
        }
520
521
        // Alert "array" (complex alert)
522 4
        $payloadAlert = array();
523
524 4
        if ($this->getAlertTitle()) {
525 1
            $payloadAlert['title'] = $this->getAlertTitle();
526 1
        }
527 4
        if ($this->getAlertBody()) {
528 1
            $payloadAlert['body'] = $this->getAlertBody();
529 1
        }
530 4
        if ($this->getAlertTitleLocKey()) {
531 1
            $payloadAlert['title-loc-key'] = $this->getAlertTitleLocKey();
532 1
        }
533 4
        if ($this->getAlertTitleLocArgs()) {
534 1
            $payloadAlert['title-loc-args'] = $this->getAlertTitleLocArgs();
535 1
        }
536 4
        if ($this->getAlertActionLocKey()) {
537 1
            $payloadAlert['action-loc-key'] = $this->getAlertActionLocKey();
538 1
        }
539 4
        if ($this->getAlertLocKey()) {
540 1
            $payloadAlert['loc-key'] = $this->getAlertLocKey();
541 1
        }
542 4
        if ($this->getAlertLocArgs()) {
543 1
            $payloadAlert['loc-args'] = $this->getAlertLocArgs();
544 1
        }
545 4
        if ($this->getAlertLaunchImage()) {
546 1
            $payloadAlert['launch-image'] = $this->getAlertLaunchImage();
547 1
        }
548
549 4
        return $payloadAlert;
550
    }
551
}
552