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
19:50 queued 18:51
created

PusherMessage::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
dl 0
loc 6
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
     * 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', 'web'])) {
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 the platform to web.
130
     *
131
     * @return $this
132
     */
133
    public function web()
134
    {
135
        $this->platform = 'web';
136
137
        return $this;
138
    }
139
140
    /**
141
     * Set an extra message to be sent to Android.
142
     *
143
     * @param \NotificationChannels\PusherPushNotifications\PusherMessage $message
144
     * @return $this
145
     */
146 1
    public function withAndroid(self $message)
147
    {
148 1
        $this->withExtra($message->android());
149
150 1
        return $this;
151
    }
152
153
    /**
154
     * Set an extra message to be sent to iOS.
155
     *
156
     * @param \NotificationChannels\PusherPushNotifications\PusherMessage $message
157
     * @return $this
158
     */
159
    public function withiOS(self $message)
160
    {
161
        $this->withExtra($message->iOS());
162
163
        return $this;
164
    }
165
166
    /**
167
     * Set an extra message to be sent to web.
168
     *
169
     * @param \NotificationChannels\PusherPushNotifications\PusherMessage $message
170
     * @return $this
171
     */
172
    public function withWeb(self $message)
173
    {
174
        $this->withExtra($message->web());
175
176
        return $this;
177
    }
178
179
    /**
180
     * Set an extra message to be sent to another platform.
181
     *
182
     * @param \NotificationChannels\PusherPushNotifications\PusherMessage $message
183
     * @return void
184
     */
185 1
    private function withExtra(self $message)
186
    {
187 1
        if ($message->getPlatform() == $this->platform) {
188
            throw CouldNotCreateMessage::platformConflict($this->platform);
189
        }
190
191 1
        $this->extraMessage = $message;
192 1
    }
193
194
    /**
195
     * Set the message title.
196
     *
197
     * @param string $value
198
     *
199
     * @return $this
200
     */
201 1
    public function title($value)
202
    {
203 1
        $this->title = $value;
204
205 1
        return $this;
206
    }
207
208
    /**
209
     * Set the message body.
210
     *
211
     * @param string $value
212
     *
213
     * @return $this
214
     */
215 1
    public function body($value)
216
    {
217 1
        $this->body = $value;
218
219 1
        return $this;
220
    }
221
222
    /**
223
     * Set the message sound (Android).
224
     *
225
     * @param string $value
226
     *
227
     * @return $this
228
     */
229 1
    public function sound($value)
230
    {
231 1
        $this->sound = $value;
232
233 1
        return $this;
234
    }
235
236
    /**
237
     * Set the message icon (Android).
238
     *
239
     * @param string $value
240
     *
241
     * @return $this
242
     */
243 1
    public function icon($value)
244
    {
245 1
        $this->icon = $value;
246
247 1
        return $this;
248
    }
249
250
    /**
251
     * Set the message badge (iOS).
252
     *
253
     * @param int $value
254
     *
255
     * @return $this
256
     */
257 1
    public function badge($value)
258
    {
259 1
        $this->badge = (int) $value;
260
261 1
        return $this;
262
    }
263
264
    /**
265
     * @param string $key
266
     * @param mixed $value
267
     *
268
     * @return $this
269
     */
270
    public function setOption($key, $value)
271
    {
272
        $this->options[$key] = $value;
273
274
        return $this;
275
    }
276
277
    /**
278
     * Get an array representation of the message.
279
     *
280
     * @return array
281
     */
282 6
    public function toArray()
283
    {
284 6
        switch ($this->platform) {
285 6
            case 'Android':
286 2
                return $this->toAndroid();
287 6
            case 'web':
288
                return $this->toWeb();
289
            default:
290 6
                return $this->toiOS();
291
        }
292
    }
293
294
    /**
295
     * Format the message for iOS.
296
     *
297
     * @return array
298
     */
299 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...
300
    {
301
        $message = [
302
            'apns' => [
303
                'aps' => [
304
                    'alert' => [
305 12
                        'title' => $this->title,
306 12
                        'body' => $this->body,
307
                    ],
308 12
                    'sound' => $this->sound,
309 12
                    'badge' => $this->badge,
310
                ],
311
            ],
312
        ];
313
314 12
        $this->formatMessage($message);
315
316 12
        return $message;
317
    }
318
319
    /**
320
     * Format the message for Android.
321
     *
322
     * @return array
323
     */
324 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...
325
    {
326
        $message = [
327
            'fcm' => [
328 6
                'notification' => array_filter([
329 6
                    'title' => $this->title,
330 6
                    'body' => $this->body,
331 6
                    'sound' => $this->sound,
332 6
                    'icon' => $this->icon,
333
                ]),
334
            ],
335
        ];
336
337 6
        $this->formatMessage($message);
338
339 6
        return $message;
340
    }
341
342
    /**
343
     * Format the message for web.
344
     *
345
     * @return array
346
     */
347 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...
348
    {
349
        $message = [
350
            'web' => [
351
                'notification' => array_filter([
352
                    'title' => $this->title,
353
                    'body' => $this->body,
354
                    'sound' => $this->sound,
355
                    'icon' => $this->icon,
356
                ]),
357
            ],
358
        ];
359
360
        $this->formatMessage($message);
361
362
        return $message;
363
    }
364
365
    /**
366
     * Return the current platform.
367
     *
368
     * @return string
369
     */
370 1
    public function getPlatform()
371
    {
372 1
        return $this->platform;
373
    }
374
375
    /**
376
     * Format the final Payload.
377
     *
378
     * @param $message
379
     */
380 13
    private function formatMessage(&$message)
381
    {
382 13
        if ($this->extraMessage) {
383 1
            $message = array_merge($message, $this->extraMessage->toArray());
384
        }
385
386 13
        foreach ($this->options as $option => $value) {
387
            Arr::set($message, $option, $value);
388
        }
389 13
    }
390
}
391