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
Push — master ( 330865...b1ea0f )
by Atymic
02:03
created

PusherMessage   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 330
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 86.11%

Importance

Changes 0
Metric Value
wmc 24
lcom 1
cbo 2
dl 0
loc 330
ccs 62
cts 72
cp 0.8611
rs 10
c 0
b 0
f 0

19 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 4 1
A __construct() 0 4 1
A platform() 0 10 2
A iOS() 0 6 1
A android() 0 6 1
A withAndroid() 0 6 1
A withiOS() 0 6 1
A withExtra() 0 8 2
A title() 0 6 1
A body() 0 6 1
A sound() 0 6 1
A icon() 0 6 1
A badge() 0 6 1
A setOption() 0 6 1
A toArray() 0 6 2
A getPlatform() 0 4 1
A formatMessage() 0 10 3
A toiOS() 0 19 1
A toAndroid() 0 17 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(self $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(self $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
     */
160 1
    private function withExtra(self $message)
161
    {
162 1
        if ($message->getPlatform() == $this->platform) {
163
            throw CouldNotCreateMessage::platformConflict($this->platform);
164
        }
165
166 1
        $this->extraMessage = $message;
167 1
    }
168
169
    /**
170
     * Set the message title.
171
     *
172
     * @param string $value
173
     *
174
     * @return $this
175
     */
176 1
    public function title($value)
177
    {
178 1
        $this->title = $value;
179
180 1
        return $this;
181
    }
182
183
    /**
184
     * Set the message body.
185
     *
186
     * @param string $value
187
     *
188
     * @return $this
189
     */
190 1
    public function body($value)
191
    {
192 1
        $this->body = $value;
193
194 1
        return $this;
195
    }
196
197
    /**
198
     * Set the message sound (Android).
199
     *
200
     * @param string $value
201
     *
202
     * @return $this
203
     */
204 1
    public function sound($value)
205
    {
206 1
        $this->sound = $value;
207
208 1
        return $this;
209
    }
210
211
    /**
212
     * Set the message icon (Android).
213
     *
214
     * @param string $value
215
     *
216
     * @return $this
217
     */
218 1
    public function icon($value)
219
    {
220 1
        $this->icon = $value;
221
222 1
        return $this;
223
    }
224
225
    /**
226
     * Set the message badge (iOS).
227
     *
228
     * @param int $value
229
     *
230
     * @return $this
231
     */
232 1
    public function badge($value)
233
    {
234 1
        $this->badge = (int) $value;
235
236 1
        return $this;
237
    }
238
239
    /**
240
     * @param string $key
241
     * @param mixed $value
242
     *
243
     * @return $this
244
     */
245
    public function setOption($key, $value)
246
    {
247
        $this->options[$key] = $value;
248
249
        return $this;
250
    }
251
252
    /**
253
     * Get an array representation of the message.
254
     *
255
     * @return array
256
     */
257 6
    public function toArray()
258
    {
259 6
        return $this->platform === 'iOS'
260 6
            ? $this->toiOS()
261 6
            : $this->toAndroid();
262
    }
263
264
    /**
265
     * Format the message for iOS.
266
     *
267
     * @return array
268
     */
269 12
    public function toiOS()
270
    {
271
        $message = [
272
            'apns' => [
273
                'aps' => [
274
                    'alert' => [
275 12
                        'title' => $this->title,
276 12
                        'body' => $this->body,
277
                    ],
278 12
                    'sound' => $this->sound,
279 12
                    'badge' => $this->badge,
280
                ],
281
            ],
282
        ];
283
284 12
        $this->formatMessage($message);
285
286 12
        return $message;
287
    }
288
289
    /**
290
     * Format the message for Android.
291
     *
292
     * @return array
293
     */
294 6
    public function toAndroid()
295
    {
296
        $message = [
297
            'fcm' => [
298 6
                'notification' => array_filter([
299 6
                    'title' => $this->title,
300 6
                    'body' => $this->body,
301 6
                    'sound' => $this->sound,
302 6
                    'icon' => $this->icon,
303
                ]),
304
            ],
305
        ];
306
307 6
        $this->formatMessage($message);
308
309 6
        return $message;
310
    }
311
312
    /**
313
     * Return the current platform.
314
     *
315
     * @return string
316
     */
317 1
    public function getPlatform()
318
    {
319 1
        return $this->platform;
320
    }
321
322
    /**
323
     * Format the final Payload.
324
     *
325
     * @param $message
326
     */
327 13
    private function formatMessage(&$message)
328
    {
329 13
        if ($this->extraMessage) {
330 1
            $message = array_merge($message, $this->extraMessage->toArray());
331
        }
332
333 13
        foreach ($this->options as $option => $value) {
334
            Arr::set($message, $option, $value);
335
        }
336 13
    }
337
}
338