GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#27)
by Hanlin
03:13
created

PusherMessage::create()   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 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace NotificationChannels\PusherPushNotifications;
4
5
use Illuminate\Support\Arr;
6
use NotificationChannels\PusherPushNotifications\Exceptions\CouldNotCreateMessage;
7
8
class PusherMessage
9
{
10
    /**
11
     * The device platform (iOS/Android).
12
     *
13
     * @var string
14
     */
15
    protected $platform = 'iOS';
16
17
    /**
18
     * The message title.
19
     *
20
     * @var string
21
     */
22
    protected $title;
23
24
    /**
25
     * The message body.
26
     *
27
     * @var string
28
     */
29
    protected $body;
30
31
    /**
32
     * The phone number the message should be sent from.
33
     *
34
     * @var string
35
     */
36
    protected $sound = 'default';
37
38
    /**
39
     * The message icon (Android).
40
     *
41
     * @var string
42
     */
43
    protected $icon;
44
45
    /**
46
     * The number to display next to the push notification (iOS).
47
     *
48
     * @var int
49
     */
50
    protected $badge;
51
52
    /**
53
     * Extra options that will get added to the message.
54
     *
55
     * @var array
56
     */
57
    protected $options = [];
58
59
    /**
60
     * An extra message to the other platform.
61
     *
62
     * @var
63
     */
64
    protected $extraMessage;
65
66
    /**
67
     * @param string $body
68
     *
69
     * @return static
70
     */
71 1
    public static function create($body = '')
72
    {
73 1
        return new static($body);
74
    }
75
76
    /**
77
     * @param string $body
78
     */
79 14
    public function __construct($body = '')
80
    {
81 14
        $this->body = $body;
82 14
    }
83
84
    /**
85
     * Set the platform [iOS/Android].
86
     *
87
     * @param string $platform
88
     *
89
     * @return $this
90
     *
91
     * @throws \NotificationChannels\PusherPushNotifications\Exceptions\CouldNotCreateMessage
92
     */
93 1
    public function platform($platform)
94
    {
95 1
        if (! in_array($platform, ['iOS', 'Android'])) {
96 1
            throw CouldNotCreateMessage::invalidPlatformGiven($platform);
97
        }
98
99
        $this->platform = $platform;
100
101
        return $this;
102
    }
103
104
    /**
105
     * Set the platform to iOS.
106
     *
107
     * @return $this
108
     */
109 2
    public function iOS()
110
    {
111 2
        $this->platform = 'iOS';
112
113 2
        return $this;
114
    }
115
116
    /**
117
     * Set the platform to Android.
118
     *
119
     * @return $this
120
     */
121 2
    public function android()
122
    {
123 2
        $this->platform = 'Android';
124
125 2
        return $this;
126
    }
127
128
    /**
129
     * Set an extra message to be sent to Android.
130
     *
131
     * @param \NotificationChannels\PusherPushNotifications\PusherMessage $message
132
     * @return $this
133
     */
134 1
    public function withAndroid(PusherMessage $message)
135
    {
136 1
        $this->withExtra($message->android());
137
138 1
        return $this;
139
    }
140
141
    /**
142
     * Set an extra message to be sent to iOS.
143
     *
144
     * @param \NotificationChannels\PusherPushNotifications\PusherMessage $message
145
     * @return $this
146
     */
147
    public function withiOS(PusherMessage $message)
148
    {
149
        $this->withExtra($message->iOS());
150
151
        return $this;
152
    }
153
154
    /**
155
     * Set an extra message to be sent to another platform.
156
     *
157
     * @param \NotificationChannels\PusherPushNotifications\PusherMessage $message
158
     * @return void
159
     * @throws CouldNotCreateMessage
160
     */
161 1
    private function withExtra(PusherMessage $message)
162
    {
163 1
        if ($message->getPlatform() == $this->platform) {
164
            throw CouldNotCreateMessage::platformConflict($this->platform);
165
        }
166
167 1
        $this->extraMessage = $message;
168 1
    }
169
170
    /**
171
     * Set the message title.
172
     *
173
     * @param string $value
174
     *
175
     * @return $this
176
     */
177 1
    public function title($value)
178
    {
179 1
        $this->title = $value;
180
181 1
        return $this;
182
    }
183
184
    /**
185
     * Set the message body.
186
     *
187
     * @param string $value
188
     *
189
     * @return $this
190
     */
191 1
    public function body($value)
192
    {
193 1
        $this->body = $value;
194
195 1
        return $this;
196
    }
197
198
    /**
199
     * Set the message sound (Android).
200
     *
201
     * @param string $value
202
     *
203
     * @return $this
204
     */
205 1
    public function sound($value)
206
    {
207 1
        $this->sound = $value;
208
209 1
        return $this;
210
    }
211
212
    /**
213
     * Set the message icon (Android).
214
     *
215
     * @param string $value
216
     *
217
     * @return $this
218
     */
219 1
    public function icon($value)
220
    {
221 1
        $this->icon = $value;
222
223 1
        return $this;
224
    }
225
226
    /**
227
     * Set the message badge (iOS).
228
     *
229
     * @param int $value
230
     *
231
     * @return $this
232
     */
233 1
    public function badge($value)
234
    {
235 1
        $this->badge = (int) $value;
236
237 1
        return $this;
238
    }
239
240
    /**
241
     * @param string $key
242
     * @param mixed $value
243
     *
244
     * @return $this
245
     */
246
    public function setOption($key, $value)
247
    {
248
        $this->options[$key] = $value;
249
250
        return $this;
251
    }
252
253
    /**
254
     * Get an array representation of the message.
255
     *
256
     * @return array
257
     */
258 6
    public function toArray()
259
    {
260 6
        return $this->platform === 'iOS'
261 6
            ? $this->toiOS()
262 6
            : $this->toAndroid();
263
    }
264
265
    /**
266
     * Format the message for iOS.
267
     *
268
     * @return array
269
     */
270 12
    public function toiOS()
271
    {
272
        $message = [
273
            'apns' => [
274
                'aps' => [
275
                    'alert' => [
276 12
                        'title' => $this->title,
277 12
                        'body' => $this->body,
278
                    ],
279 12
                    'sound' => $this->sound,
280 12
                    'badge' => $this->badge,
281
                ],
282
            ],
283
        ];
284
285 12
        $this->formatMessage($message);
286
287 12
        return $message;
288
    }
289
290
    /**
291
     * Format the message for Android.
292
     *
293
     * @return array
294
     */
295 6
    public function toAndroid()
296
    {
297
        $message = [
298
            'fcm' => [
299
                'notification' => [
300 6
                    'title' => $this->title,
301 6
                    'body' => $this->body,
302 6
                    'sound' => $this->sound,
303 6
                    'icon' => $this->icon ?: 'icon', // without icon pusher api will return invalid json format error
304
                ],
305
            ],
306
        ];
307
308 6
        $this->formatMessage($message);
309
310 6
        return $message;
311
    }
312
313
    /**
314
     * Return the current platform.
315
     *
316
     * @return string
317
     */
318 1
    public function getPlatform()
319
    {
320 1
        return $this->platform;
321
    }
322
323
    /**
324
     * Format the final Payload.
325
     *
326
     * @param $message
327
     */
328 13
    private function formatMessage(&$message)
329
    {
330 13
        if ($this->extraMessage) {
331 1
            $message = array_merge($message, $this->extraMessage->toArray());
332
        }
333
334 13
        foreach ($this->options as $option => $value) {
335
            Arr::set($message, $option, $value);
336
        }
337 13
    }
338
}
339