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 (#52)
by
unknown
03:02
created

PusherMessage::__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
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
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
     * URL to follow on notification click
54
     */
55
    protected $link;
56
57
    /**
58
     * Extra options that will get added to the message.
59
     *
60
     * @var array
61
     */
62
    protected $options = [];
63
64
    /**
65
     * An extra message to the other platform.
66
     *
67
     * @var
68
     */
69
    protected $extraMessage;
70
71
    /**
72
     * @param string $body
73
     *
74
     * @return static
75
     */
76 1
    public static function create($body = '')
77
    {
78 1
        return new static($body);
79
    }
80
81
    /**
82
     * @param string $body
83
     */
84 14
    public function __construct($body = '')
85
    {
86 14
        $this->body = $body;
87 14
    }
88
89
    /**
90
     * Set the platform [iOS/Android].
91
     *
92
     * @param string $platform
93
     *
94
     * @return $this
95
     *
96
     * @throws \NotificationChannels\PusherPushNotifications\Exceptions\CouldNotCreateMessage
97
     */
98 1
    public function platform($platform)
99
    {
100 1
        if (! in_array($platform, ['iOS', 'Android', 'web'])) {
101 1
            throw CouldNotCreateMessage::invalidPlatformGiven($platform);
102
        }
103
104
        $this->platform = $platform;
105
106
        return $this;
107
    }
108
109
    /**
110
     * Set the platform to iOS.
111
     *
112
     * @return $this
113
     */
114 2
    public function iOS()
115
    {
116 2
        $this->platform = 'iOS';
117
118 2
        return $this;
119
    }
120
121
    /**
122
     * Set the platform to Android.
123
     *
124
     * @return $this
125
     */
126 2
    public function android()
127
    {
128 2
        $this->platform = 'Android';
129
130 2
        return $this;
131
    }
132
133
    /**
134
     * Set the platform to web.
135
     *
136
     * @return $this
137
     */
138
    public function web()
139
    {
140
        $this->platform = 'web';
141
142
        return $this;
143
    }
144
145
    /**
146
     * Set an extra message to be sent to Android.
147
     *
148
     * @param \NotificationChannels\PusherPushNotifications\PusherMessage $message
149
     * @return $this
150
     */
151 1
    public function withAndroid(self $message)
152
    {
153 1
        $this->withExtra($message->android());
154
155 1
        return $this;
156
    }
157
158
    /**
159
     * Set an extra message to be sent to iOS.
160
     *
161
     * @param \NotificationChannels\PusherPushNotifications\PusherMessage $message
162
     * @return $this
163
     */
164
    public function withiOS(self $message)
165
    {
166
        $this->withExtra($message->iOS());
167
168
        return $this;
169
    }
170
171
    /**
172
     * Set an extra message to be sent to web.
173
     *
174
     * @param \NotificationChannels\PusherPushNotifications\PusherMessage $message
175
     * @return $this
176
     */
177
    public function withWeb(self $message)
178
    {
179
        $this->withExtra($message->web());
180
181
        return $this;
182
    }
183
184
    /**
185
     * Set an extra message to be sent to another platform.
186
     *
187
     * @param \NotificationChannels\PusherPushNotifications\PusherMessage $message
188
     * @return void
189
     */
190 1
    private function withExtra(self $message)
191
    {
192 1
        if ($message->getPlatform() == $this->platform) {
193
            throw CouldNotCreateMessage::platformConflict($this->platform);
194
        }
195
196 1
        $this->extraMessage = $message;
197 1
    }
198
199
    /**
200
     * Set the message title.
201
     *
202
     * @param string $value
203
     *
204
     * @return $this
205
     */
206 1
    public function title($value)
207
    {
208 1
        $this->title = $value;
209
210 1
        return $this;
211
    }
212
213
    /**
214
     * Set the message body.
215
     *
216
     * @param string $value
217
     *
218
     * @return $this
219
     */
220 1
    public function body($value)
221
    {
222 1
        $this->body = $value;
223
224 1
        return $this;
225
    }
226
227
    /**
228
     * Set the message sound (Android).
229
     *
230
     * @param string $value
231
     *
232
     * @return $this
233
     */
234 1
    public function sound($value)
235
    {
236 1
        $this->sound = $value;
237
238 1
        return $this;
239
    }
240
241
    /**
242
     * Set the message icon (Android).
243
     *
244
     * @param string $value
245
     *
246
     * @return $this
247
     */
248 1
    public function icon($value)
249
    {
250 1
        $this->icon = $value;
251
252 1
        return $this;
253
    }
254
255
    /**
256
     * Set the message badge (iOS).
257
     *
258
     * @param int $value
259
     *
260
     * @return $this
261
     */
262 1
    public function badge($value)
263
    {
264 1
        $this->badge = (int) $value;
265
266 1
        return $this;
267
    }
268
269
    /**
270
     * Set the message link.
271
     *
272
     * @param string $value
273
     *
274
     * @return $this
275
     */
276
    public function link($value)
277
    {
278
        $this->link = $value;
279
280
        return $this;
281
    }
282
283
    /**
284
     * @param string $key
285
     * @param mixed $value
286
     *
287
     * @return $this
288
     */
289
    public function setOption($key, $value)
290
    {
291
        $this->options[$key] = $value;
292
293
        return $this;
294
    }
295
296
    /**
297
     * Get an array representation of the message.
298
     *
299
     * @return array
300
     */
301 6
    public function toArray()
302
    {
303 6
        switch ($this->platform) {
304 6
            case 'Android':
305 2
                return $this->toAndroid();
306 6
            case 'web':
307
                return $this->toWeb();
308
            default:
309 6
                return $this->toiOS();
310
        }
311
    }
312
313
    /**
314
     * Format the message for iOS.
315
     *
316
     * @return array
317
     */
318 12 View Code Duplication
    public function toiOS()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
319
    {
320
        $message = [
321
            'apns' => [
322
                'aps' => [
323
                    'alert' => [
324 12
                        'title' => $this->title,
325 12
                        'body' => $this->body,
326
                    ],
327 12
                    'sound' => $this->sound,
328 12
                    'badge' => $this->badge,
329
                ],
330
            ],
331
        ];
332
333 12
        $this->formatMessage($message);
334
335 12
        return $message;
336
    }
337
338
    /**
339
     * Format the message for Android.
340
     *
341
     * @return array
342
     */
343 6 View Code Duplication
    public function toAndroid()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
344
    {
345
        $message = [
346
            'fcm' => [
347 6
                'notification' => array_filter([
348 6
                    'title' => $this->title,
349 6
                    'body' => $this->body,
350 6
                    'sound' => $this->sound,
351 6
                    'icon' => $this->icon,
352
                ]),
353
            ],
354
        ];
355
356 6
        $this->formatMessage($message);
357
358 6
        return $message;
359
    }
360
361
    /**
362
     * Format the message for web.
363
     *
364
     * @return array
365
     */
366 View Code Duplication
    public function toWeb()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
367
    {
368
        $message = [
369
            'web' => [
370
                'notification' => array_filter([
371
                    'title' => $this->title,
372
                    'body' => $this->body,
373
                    'sound' => $this->sound,
374
                    'icon' => $this->icon,
375
                    'deep_link' => $this->link,
376
                ]),
377
            ],
378
        ];
379
380
        $this->formatMessage($message);
381
382
        return $message;
383
    }
384
385
    /**
386
     * Return the current platform.
387
     *
388
     * @return string
389
     */
390 1
    public function getPlatform()
391
    {
392 1
        return $this->platform;
393
    }
394
395
    /**
396
     * Format the final Payload.
397
     *
398
     * @param $message
399
     */
400 13
    private function formatMessage(&$message)
401
    {
402 13
        if ($this->extraMessage) {
403 1
            $message = array_merge($message, $this->extraMessage->toArray());
404
        }
405
406 13
        foreach ($this->options as $option => $value) {
407
            Arr::set($message, $option, $value);
408
        }
409 13
    }
410
}
411