Payload   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 0
loc 66
ccs 17
cts 17
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A toArray() 0 14 1
A jsonSerialize() 0 4 1
A limitString() 0 8 2
1
<?php
2
3
namespace Notimatica\Driver\Apns;
4
5
use Notimatica\Driver\Contracts\Notification;
6
7
class Payload implements \JsonSerializable
8
{
9
    const TITLE_LENGTH = 40;
10
    const BODY_LENGTH = 90;
11
12
    /**
13
     * @var Notification
14
     */
15
    private $notification;
16
17
    /**
18
     * Construct.
19
     *
20
     * @param Notification $notification
21
     */
22 1
    public function __construct(Notification $notification)
23
    {
24 1
        $this->notification = $notification;
25 1
    }
26
27
    /**
28
     * Get the instance as an array.
29
     *
30
     * @return array
31
     */
32 1
    public function toArray()
33
    {
34
        return [
35
            'aps' => [
36
                'alert' => [
37 1
                    'title' => $this->limitString($this->notification->getTitle(), static::TITLE_LENGTH - 3),
38 1
                    'body' => $this->limitString($this->notification->getBody(), static::BODY_LENGTH - 3),
39 1
                ],
40
                'url-args' => [
41 1
                    $this->notification->getId(),
42 1
                ],
43 1
            ],
44 1
        ];
45
    }
46
47
    /**
48
     * Specify data which should be serialized to JSON.
49
     */
50 1
    public function jsonSerialize()
51
    {
52 1
        return $this->toArray();
53
    }
54
55
    /**
56
     * Limit string size.
57
     * Realization: Laravel.
58
     *
59
     * @param  string $value
60
     * @param  int $limit
61
     * @param  string $end
62
     * @return string
63
     */
64 1
    protected function limitString($value, $limit = 10, $end = '...')
65
    {
66 1
        if (mb_strwidth($value, 'UTF-8') <= $limit) {
67 1
            return $value;
68
        }
69
70 1
        return rtrim(mb_strimwidth($value, 0, $limit, '', 'UTF-8')) . $end;
71
    }
72
}
73