Passed
Push — master ( 2579fe...c39097 )
by Aly
01:15 queued 13s
created

ExpoMessage::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace NotificationChannels\ExpoPushNotifications;
4
5
use NotificationChannels\ExpoPushNotifications\Exceptions\CouldNotCreateMessage;
6
7
class ExpoMessage
8
{
9
    /**
10
     * The message title.
11
     *
12
     * @var string
13
     */
14
    protected $title;
15
16
    /**
17
     * The message body.
18
     *
19
     * @var string
20
     */
21
    protected $body;
22
23
    /**
24
     * The sound to play when the recipient receives this notification.
25
     *
26
     * @var string|null
27
     */
28
    protected $sound = 'default';
29
30
    /**
31
     * The number to display next to the push notification (iOS).
32
     * Specify zero to clear the badge.
33
     *
34
     * @var int
35
     */
36
    protected $badge = 0;
37
38
    /**
39
     * The number of seconds for which the message may be kept around for redelivery if it has not been delivered yet.
40
     *
41
     * @var int
42
     */
43
    protected $ttl = 0;
44
45
    /**
46
     * ID of the Notification Channel through which to display this notification on Android devices.
47
     *
48
     * @var string
49
     */
50
    protected $channelId = '';
51
52
    /**
53
     * The json data attached to the message.
54
     *
55
     * @var string
56
     */
57
    protected $jsonData = '{}';
58
59
    /**
60
     * Create a message with given body.
61
     *
62
     * @param string $body
63
     *
64
     * @return static
65
     */
66 1
    public static function create($body = '')
67
    {
68 1
        return new static($body);
69
    }
70
71
    /**
72
     * ExpoMessage constructor.
73
     *
74
     * @param string $body
75
     */
76 13
    public function __construct(string $body = '')
77
    {
78 13
        $this->body = $body;
79 13
    }
80
81
    /**
82
     * Set the message title.
83
     *
84
     * @param string $value
85
     *
86
     * @return $this
87
     */
88
    public function title(string $value)
89
    {
90
        $this->title = $value;
91
92
        return $this;
93
    }
94
95
    /**
96
     * Set the message body.
97
     *
98
     * @param string $value
99
     *
100
     * @return $this
101
     */
102 1
    public function body(string $value)
103
    {
104 1
        $this->body = $value;
105
106 1
        return $this;
107
    }
108
109
    /**
110
     * Enable the message sound.
111
     *
112
     * @return $this
113
     */
114 1
    public function enableSound()
115
    {
116 1
        $this->sound = 'default';
117
118 1
        return $this;
119
    }
120
121
    /**
122
     * Disable the message sound.
123
     *
124
     * @return $this
125
     */
126 2
    public function disableSound()
127
    {
128 2
        $this->sound = null;
129
130 2
        return $this;
131
    }
132
133
    /**
134
     * Set the message badge (iOS).
135
     *
136
     * @param int $value
137
     *
138
     * @return $this
139
     */
140 1
    public function badge(int $value)
141
    {
142 1
        $this->badge = $value;
143
144 1
        return $this;
145
    }
146
147
    /**
148
     * Set the time to live of the notification.
149
     *
150
     * @param int $ttl
151
     *
152
     * @return $this
153
     */
154 1
    public function setTtl(int $ttl)
155
    {
156 1
        $this->ttl = $ttl;
157
158 1
        return $this;
159
    }
160
161
    /**
162
     * Set the channelId of the notification for Android devices.
163
     *
164
     * @param string $channelId
165
     *
166
     * @return $this
167
     */
168 1
    public function setChannelId(string $channelId)
169
    {
170 1
        $this->channelId = $channelId;
171
172 1
        return $this;
173
    }
174
175
    /**
176
     * Set the json Data attached to the message.
177
     *
178
     * @param array|string $data
179
     *
180
     * @return $this
181
     *
182
     * @throws CouldNotCreateMessage
183
     */
184 1
    public function setJsonData($data)
185
    {
186 1
        if (is_array($data)) {
187
            $data = json_encode($data);
188 1
        } elseif (is_string($data)) {
189 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...
190
191 1
            if (json_last_error() !== JSON_ERROR_NONE) {
192
                throw new CouldNotCreateMessage('Invalid json format passed to the setJsonData().');
193
            }
194
        }
195
196 1
        $this->jsonData = $data;
197
198 1
        return $this;
199
    }
200
201
    /**
202
     * Get an array representation of the message.
203
     *
204
     * @return array
205
     */
206 13
    public function toArray()
207
    {
208
        $message = [
209 13
            'title'     =>  $this->title,
210 13
            'body'      =>  $this->body,
211 13
            'sound'     =>  $this->sound,
212 13
            'badge'     =>  $this->badge,
213 13
            'ttl'       =>  $this->ttl,
214 13
            'data'      =>  $this->jsonData,
215
        ];
216 13
        if (! empty($this->channelId)) {
217 1
            $message['channelId'] = $this->channelId;
218
        }
219
220 13
        return $message;
221
    }
222
}
223