Completed
Push — master ( 13aa7e...d7c5f7 )
by Mark
10s
created

IonicPushChannel::send()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 33
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 4.0016

Importance

Changes 5
Bugs 1 Features 1
Metric Value
c 5
b 1
f 1
dl 0
loc 33
ccs 20
cts 21
cp 0.9524
rs 8.5806
cc 4
eloc 18
nc 4
nop 2
crap 4.0016
1
<?php
2
3
namespace NotificationChannels\IonicPushNotifications;
4
5
use GuzzleHttp\Client;
6
use NotificationChannels\IonicPushNotifications\Exceptions\CouldNotSendNotification;
7
use Illuminate\Notifications\Notification;
8
use NotificationChannels\IonicPushNotifications\Exceptions\InvalidConfiguration;
9
10
class IonicPushChannel
11
{
12
    const API_ENDPOINT = 'https://api.ionic.io/push/notifications';
13
14
    /** @var Client */
15
    protected $client;
16
17
    /**
18
     * @param Client $client
19
     */
20 4
    public function __construct(Client $client)
21
    {
22 4
        $this->client = $client;
23 4
    }
24
25
    /**
26
     * Send the given notification.
27
     *
28
     * @param mixed $notifiable
29
     * @param \Illuminate\Notifications\Notification $notification
30
     *
31
     * @throws \NotificationChannels\IonicPushNotifications\Exceptions\InvalidConfiguration
32
     * @throws \NotificationChannels\IonicPushNotifications\Exceptions\CouldNotSendNotification
33
     */
34 4
    public function send($notifiable, Notification $notification)
35
    {
36 4
        $routing = collect($notifiable->routeNotificationFor('IonicPush'));
37
38 4
        if (! $routing->first()) {
39
            return;
40
        }
41
42 4
        $authorizationKey = config('services.ionicpush.key');
43
44 4
        if (is_null($authorizationKey)) {
45 1
            throw InvalidConfiguration::configurationNotSet();
46
        }
47
48 3
        $message = $notification->toIonicPush($notifiable);
0 ignored issues
show
Bug introduced by
The method toIonicPush() does not seem to exist on object<Illuminate\Notifications\Notification>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
49
50 3
        $ionicPushData = array_merge(
51 3
            [$message->getSendToType() => $routing->first()],
52 3
            $message->toArray()
53 3
        );
54
55 3
        $response = $this->client->post(self::API_ENDPOINT, [
56 3
            'body' => json_encode($ionicPushData),
57
            'headers' => [
58 3
                'Authorization' => "Bearer {$authorizationKey}",
59 3
                'Content-Type' => 'application/json',
60 3
            ],
61 3
        ]);
62
63 3
        if ($response->getStatusCode() !== 201) {
64 1
            throw CouldNotSendNotification::serviceRespondedWithAnError($response);
65
        }
66 2
    }
67
}
68