Test Failed
Pull Request — master (#17)
by
unknown
12:54
created

ExpoMessage::body()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

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 6
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 = 'Default';
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
    public static function create($body = '')
67
    {
68
        return new static($body);
69
    }
70
71
    /**
72
     * ExpoMessage constructor.
73
     *
74
     * @param string $body
75
     */
76
    public function __construct(string $body = '')
77
    {
78
        $this->body = $body;
79
    }
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
    public function body(string $value)
103
    {
104
        $this->body = $value;
105
106
        return $this;
107
    }
108
109
    /**
110
     * Enable the message sound.
111
     *
112
     * @return $this
113
     */
114
    public function enableSound()
115
    {
116
        $this->sound = 'default';
117
118
        return $this;
119
    }
120
121
    /**
122
     * Disable the message sound.
123
     *
124
     * @return $this
125
     */
126
    public function disableSound()
127
    {
128
        $this->sound = null;
129
130
        return $this;
131
    }
132
133
    /**
134
     * Set the message badge (iOS).
135
     *
136
     * @param int $value
137
     *
138
     * @return $this
139
     */
140
    public function badge(int $value)
141
    {
142
        $this->badge = $value;
143
144
        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
    public function setTtl(int $ttl)
155
    {
156
        $this->ttl = $ttl;
157
158
        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
    public function setChannelId(string $channelId)
169
    {
170
        $this->channelId = $channelId;
171
172
        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
    public function setJsonData($data)
185
    {
186
        if (is_array($data)) {
187
            $data = json_encode($data);
188
        } elseif (is_string($data)) {
189
            @json_decode($data);
190
191
            if (json_last_error() !== JSON_ERROR_NONE) {
192
                throw new CouldNotCreateMessage('Invalid json format passed to the setJsonData().');
193
            }
194
        }
195
196
        $this->jsonData = $data;
197
198
        return $this;
199
    }
200
201
    /**
202
     * Get an array representation of the message.
203
     *
204
     * @return array
205
     */
206
    public function toArray()
207
    {
208
        return [
209
            'title'     =>  $this-
210
            'body'      =>  $this->body,
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_DOUBLE_ARROW, expecting ']'
Loading history...
211
            'sound'     =>  $this->sound,
212
            'badge'     =>  $this->badge,
213
            'ttl'       =>  $this->ttl,
214
            'channelId' =>  $this->channelId,
215
            'data'      => $this->jsonData,
216
        ];
217
    }
218
}
219