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

PusherMessage::meta()   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
     * Meta data that will be passed along with the message.
61
     *
62
     * @var array
63
     */
64
    protected $meta = [];
65
66
    /**
67
     * An extra message to the other platform.
68
     *
69
     * @var
70
     */
71
    protected $extraMessage;
72
73
    /**
74
     * @param string $body
75
     *
76
     * @return static
77
     */
78 1
    public static function create($body = '')
79
    {
80 1
        return new static($body);
81
    }
82
83
    /**
84
     * @param string $body
85
     */
86 16
    public function __construct($body = '')
87
    {
88 16
        $this->body = $body;
89 16
    }
90
91
    /**
92
     * Set the platform [iOS/Android].
93
     *
94
     * @param string $platform
95
     *
96
     * @return $this
97
     *
98
     * @throws \NotificationChannels\PusherPushNotifications\Exceptions\CouldNotCreateMessage
99
     */
100 1
    public function platform($platform)
101
    {
102 1
        if (! in_array($platform, ['iOS', 'Android'])) {
103 1
            throw CouldNotCreateMessage::invalidPlatformGiven($platform);
104
        }
105
106
        $this->platform = $platform;
107
108
        return $this;
109
    }
110
111
    /**
112
     * Set the platform to iOS.
113
     *
114
     * @return $this
115
     */
116 2
    public function iOS()
117
    {
118 2
        $this->platform = 'iOS';
119
120 2
        return $this;
121
    }
122
123
    /**
124
     * Set the platform to Android.
125
     *
126
     * @return $this
127
     */
128 3
    public function android()
129
    {
130 3
        $this->platform = 'Android';
131
132 3
        return $this;
133
    }
134
135
    /**
136
     * Set an extra message to be sent to Android.
137
     *
138
     * @param \NotificationChannels\PusherPushNotifications\PusherMessage $message
139
     * @return $this
140
     */
141 1
    public function withAndroid(self $message)
142
    {
143 1
        $this->withExtra($message->android());
144
145 1
        return $this;
146
    }
147
148
    /**
149
     * Set an extra message to be sent to iOS.
150
     *
151
     * @param \NotificationChannels\PusherPushNotifications\PusherMessage $message
152
     * @return $this
153
     */
154
    public function withiOS(self $message)
155
    {
156
        $this->withExtra($message->iOS());
157
158
        return $this;
159
    }
160
161
    /**
162
     * Set an extra message to be sent to another platform.
163
     *
164
     * @param \NotificationChannels\PusherPushNotifications\PusherMessage $message
165
     * @return void
166
     */
167 1
    private function withExtra(self $message)
168
    {
169 1
        if ($message->getPlatform() == $this->platform) {
170
            throw CouldNotCreateMessage::platformConflict($this->platform);
171
        }
172
173 1
        $this->extraMessage = $message;
174 1
    }
175
176
    /**
177
     * Set the message title.
178
     *
179
     * @param string $value
180
     *
181
     * @return $this
182
     */
183 1
    public function title($value)
184
    {
185 1
        $this->title = $value;
186
187 1
        return $this;
188
    }
189
190
    /**
191
     * Set the message body.
192
     *
193
     * @param string $value
194
     *
195
     * @return $this
196
     */
197 1
    public function body($value)
198
    {
199 1
        $this->body = $value;
200
201 1
        return $this;
202
    }
203
204
    /**
205
     * Set the message sound (Android).
206
     *
207
     * @param string $value
208
     *
209
     * @return $this
210
     */
211 1
    public function sound($value)
212
    {
213 1
        $this->sound = $value;
214
215 1
        return $this;
216
    }
217
218
    /**
219
     * Set the message icon (Android).
220
     *
221
     * @param string $value
222
     *
223
     * @return $this
224
     */
225 1
    public function icon($value)
226
    {
227 1
        $this->icon = $value;
228
229 1
        return $this;
230
    }
231
232
    /**
233
     * Set the message badge (iOS).
234
     *
235
     * @param int $value
236
     *
237
     * @return $this
238
     */
239 1
    public function badge($value)
240
    {
241 1
        $this->badge = (int) $value;
242
243 1
        return $this;
244
    }
245
246
    /**
247
     * Set the metadata.
248
     *
249
     * @param array $meta
250
     *
251
     * @return $this
252
     */
253 1
    public function meta(array $meta)
254
    {
255 1
        $this->meta = $meta;
256
257 1
        return $this;
258
    }
259
260
    /**
261
     * @param string $key
262
     * @param mixed $value
263
     *
264
     * @return $this
265
     */
266
    public function setOption($key, $value)
267
    {
268
        $this->options[$key] = $value;
269
270
        return $this;
271
    }
272
273
    /**
274
     * Get an array representation of the message.
275
     *
276
     * @return array
277
     */
278 8
    public function toArray()
279
    {
280 8
        return $this->platform === 'iOS'
281 8
            ? $this->toiOS()
282 8
            : $this->toAndroid();
283
    }
284
285
    /**
286
     * Format the message for iOS.
287
     *
288
     * @return array
289
     */
290 14 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...
291
    {
292
        $message = [
293
            'apns' => [
294
                'aps' => [
295
                    'alert' => [
296 14
                        'title' => $this->title,
297 14
                        'body' => $this->body,
298
                    ],
299 14
                    'sound' => $this->sound,
300 14
                    'badge' => $this->badge,
301
                ],
302
            ],
303
        ];
304
305 14
        if (!empty($this->meta)) {
306 1
            $message['apns']['data'] = $this->meta;
307
        }
308
309 14
        $this->formatMessage($message);
310
311 14
        return $message;
312
    }
313
314
    /**
315
     * Format the message for Android.
316
     *
317
     * @return array
318
     */
319 7 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...
320
    {
321
        $message = [
322
            'fcm' => [
323 7
                'notification' => array_filter([
324 7
                    'title' => $this->title,
325 7
                    'body' => $this->body,
326 7
                    'sound' => $this->sound,
327 7
                    'icon' => $this->icon,
328
                ]),
329
            ],
330
        ];
331
332 7
        if (!empty($this->meta)) {
333 1
            $message['fcm']['data'] = $this->meta;
334
        }
335
336 7
        $this->formatMessage($message);
337
338 7
        return $message;
339
    }
340
341
    /**
342
     * Return the current platform.
343
     *
344
     * @return string
345
     */
346 1
    public function getPlatform()
347
    {
348 1
        return $this->platform;
349
    }
350
351
    /**
352
     * Format the final Payload.
353
     *
354
     * @param $message
355
     */
356 15
    private function formatMessage(&$message)
357
    {
358 15
        if ($this->extraMessage) {
359 1
            $message = array_merge($message, $this->extraMessage->toArray());
360
        }
361
362 15
        foreach ($this->options as $option => $value) {
363
            Arr::set($message, $option, $value);
364
        }
365 15
    }
366
}
367