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 (#26)
by Hanlin
03:05
created

PusherMessage::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
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
    public static function create($body = '')
72
    {
73
        return new static($body);
74
    }
75
76
    /**
77
     * @param string $body
78
     */
79
    public function __construct($body = '')
80
    {
81
        $this->body = $body;
82
    }
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
    public function platform($platform)
94
    {
95
        if (! in_array($platform, ['iOS', 'Android'])) {
96
            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
    public function iOS()
110
    {
111
        $this->platform = 'iOS';
112
113
        return $this;
114
    }
115
116
    /**
117
     * Set the platform to Android.
118
     *
119
     * @return $this
120
     */
121
    public function android()
122
    {
123
        $this->platform = 'Android';
124
125
        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
    public function withAndroid(PusherMessage $message)
135
    {
136
        $this->withExtra($message->android());
137
138
        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
     */
160
    private function withExtra(PusherMessage $message)
161
    {
162
        if ($message->getPlatform() == $this->platform) {
163
            throw CouldNotCreateMessage::platformConflict($this->platform);
164
        }
165
166
        $this->extraMessage = $message;
167
    }
168
169
    /**
170
     * Set the message title.
171
     *
172
     * @param string $value
173
     *
174
     * @return $this
175
     */
176
    public function title($value)
177
    {
178
        $this->title = $value;
179
180
        return $this;
181
    }
182
183
    /**
184
     * Set the message body.
185
     *
186
     * @param string $value
187
     *
188
     * @return $this
189
     */
190
    public function body($value)
191
    {
192
        $this->body = $value;
193
194
        return $this;
195
    }
196
197
    /**
198
     * Set the message sound (Android).
199
     *
200
     * @param string $value
201
     *
202
     * @return $this
203
     */
204
    public function sound($value)
205
    {
206
        $this->sound = $value;
207
208
        return $this;
209
    }
210
211
    /**
212
     * Set the message icon (Android).
213
     *
214
     * @param string $value
215
     *
216
     * @return $this
217
     */
218
    public function icon($value)
219
    {
220
        $this->icon = $value;
221
222
        return $this;
223
    }
224
225
    /**
226
     * Set the message badge (iOS).
227
     *
228
     * @param int $value
229
     *
230
     * @return $this
231
     */
232
    public function badge($value)
233
    {
234
        $this->badge = (int) $value;
235
236
        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
    public function toArray()
258
    {
259
        return $this->platform === 'iOS'
260
            ? $this->toiOS()
261
            : $this->toAndroid();
262
    }
263
264
    /**
265
     * Format the message for iOS.
266
     *
267
     * @return array
268
     */
269 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...
270
    {
271
        $message = [
272
            'apns' => [
273
                'aps' => [
274
                    'alert' => [
275
                        'title' => $this->title,
276
                        'body' => $this->body,
277
                    ],
278
                    'sound' => $this->sound,
279
                    'badge' => $this->badge,
280
                ],
281
            ],
282
        ];
283
284
        $this->formatMessage($message);
285
286
        return $message;
287
    }
288
289
    /**
290
     * Format the message for Android.
291
     *
292
     * @return array
293
     */
294 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...
295
    {
296
        $message = [
297
            'gcm' => [
298
                'notification' => [
299
                    'title' => $this->title,
300
                    'body' => $this->body,
301
                    'sound' => $this->sound,
302
                    'icon' => $this->icon,
303
                ],
304
            ],
305
        ];
306
307
        $this->formatMessage($message);
308
309
        return $message;
310
    }
311
312
    /**
313
     * Return the current platform.
314
     *
315
     * @return string
316
     */
317
    public function getPlatform()
318
    {
319
        return $this->platform;
320
    }
321
322
    /**
323
     * Format the final Payload.
324
     *
325
     * @param $message
326
     */
327
    private function formatMessage(&$message)
328
    {
329
        if ($this->extraMessage) {
330
            $message = array_merge($message, $this->extraMessage->toArray());
331
        }
332
333
        foreach ($this->options as $option => $value) {
334
            Arr::set($message, $option, $value);
335
        }
336
    }
337
}
338