Passed
Push — master ( cc2f59...504a7e )
by Aly
49s
created

ExpoMessage   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 168
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 94.29%

Importance

Changes 0
Metric Value
wmc 12
c 0
b 0
f 0
lcom 1
cbo 1
dl 0
loc 168
ccs 33
cts 35
cp 0.9429
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 4 1
A __construct() 0 4 1
A body() 0 6 1
A enableSound() 0 6 1
A disableSound() 0 6 1
A badge() 0 6 1
A setTtl() 0 6 1
A setJsonData() 0 16 4
A toArray() 0 10 1
1
<?php
2
3
namespace NotificationChannels\ExpoPushNotifications;
4
5
use NotificationChannels\ExpoPushNotifications\Exceptions\CouldNotCreateMessage;
6
7
class ExpoMessage
8
{
9
    /**
10
     * The message body.
11
     *
12
     * @var string
13
     */
14
    protected $body;
15
16
    /**
17
     * The sound to play when the recipient receives this notification.
18
     *
19
     * @var string|null
20
     */
21
    protected $sound = 'default';
22
23
    /**
24
     * The number to display next to the push notification (iOS).
25
     * Specify zero to clear the badge.
26
     *
27
     * @var int
28
     */
29
    protected $badge = 0;
30
31
    /**
32
     * The number of seconds for which the message may be kept around for redelivery if it has not been delivered yet.
33
     *
34
     * @var int
35
     */
36
    protected $ttl = 0;
37
38
    /**
39
     * The json data attached to the message.
40
     *
41
     * @var string
42
     */
43
    protected $jsonData = '';
44
45
    /**
46
     * Create a message with given body.
47
     *
48
     * @param string $body
49
     *
50
     * @return static
51
     */
52 1
    public static function create($body = '')
53
    {
54 1
        return new static($body);
55
    }
56
57
    /**
58
     * ExpoMessage constructor.
59
     *
60
     * @param string $body
61
     */
62 10
    public function __construct(string $body = '')
63
    {
64 10
        $this->body = $body;
65 10
    }
66
67
    /**
68
     * Set the message body.
69
     *
70
     * @param string $value
71
     *
72
     * @return $this
73
     */
74 1
    public function body(string $value)
75
    {
76 1
        $this->body = $value;
77
78 1
        return $this;
79
    }
80
81
    /**
82
     * Enable the message sound.
83
     *
84
     * @return $this
85
     */
86 1
    public function enableSound()
87
    {
88 1
        $this->sound = 'default';
89
90 1
        return $this;
91
    }
92
93
    /**
94
     * Disable the message sound.
95
     *
96
     * @return $this
97
     */
98 2
    public function disableSound()
99
    {
100 2
        $this->sound = null;
101
102 2
        return $this;
103
    }
104
105
    /**
106
     * Set the message badge (iOS).
107
     *
108
     * @param int $value
109
     *
110
     * @return $this
111
     */
112 1
    public function badge(int $value)
113
    {
114 1
        $this->badge = $value;
115
116 1
        return $this;
117
    }
118
119
    /**
120
     * Set the time to live of the notification.
121
     *
122
     * @param int $ttl
123
     *
124
     * @return $this
125
     */
126 1
    public function setTtl(int $ttl)
127
    {
128 1
        $this->ttl = $ttl;
129
130 1
        return $this;
131
    }
132
133
    /**
134
     * Set the json Data attached to the message.
135
     *
136
     * @param array|string $data
137
     *
138
     * @return $this
139
     *
140
     * @throws CouldNotCreateMessage
141
     */
142 1
    public function setJsonData($data)
143
    {
144 1
        if (is_array($data)) {
145
            $data = json_encode($data);
146 1
        } elseif (is_string($data)) {
147 1
            @json_decode($data);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
148
149 1
            if (json_last_error() !== JSON_ERROR_NONE) {
150
                throw new CouldNotCreateMessage('Invalid json format passed to the setJsonData().');
151
            }
152
        }
153
154 1
        $this->jsonData = $data;
155
156 1
        return $this;
157
    }
158
159
    /**
160
     * Get an array representation of the message.
161
     *
162
     * @return array
163
     */
164 10
    public function toArray()
165
    {
166
        return [
167 10
            'body'      =>  $this->body,
168 10
            'sound'     =>  $this->sound,
169 10
            'badge'     =>  $this->badge,
170 10
            'ttl'       =>  $this->ttl,
171 10
            'data'  => $this->jsonData,
172
        ];
173
    }
174
}
175