Completed
Push — master ( 041186...231b92 )
by Mark
03:39
created

IonicPushChannel   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 95%

Importance

Changes 4
Bugs 0 Features 1
Metric Value
wmc 5
c 4
b 0
f 1
lcom 1
cbo 5
dl 0
loc 56
ccs 19
cts 20
cp 0.95
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B send() 0 31 4
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 3
    public function __construct(Client $client)
21
    {
22 3
        $this->client = $client;
23 3
    }
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 3
    public function send($notifiable, Notification $notification)
35
    {
36 3
        if (! $routing = collect($notifiable->routeNotificationFor('IonicPush'))) {
37
            return;
38
        }
39
40 3
        $authorizationKey = config('services.ionicpush.key');
41
42 3
        if (is_null($authorizationKey)) {
43 1
            throw InvalidConfiguration::configurationNotSet();
44
        }
45
46 2
        $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...
47
48 2
        $ionicPushData = array_merge(
49 2
            [$message->getSendToType() => $routing->first()],
50 2
            $message->toArray()
51
        );
52
53 2
        $response = $this->client->post(self::API_ENDPOINT, [
54 2
            'body' => json_encode($ionicPushData),
55
            'headers' => [
56 2
                'Authorization' => "Bearer {$authorizationKey}",
57 2
                'Content-Type' => 'application/json',
58
            ],
59
        ]);
60
61 2
        if ($response->getStatusCode() !== 201) {
62 1
            throw CouldNotSendNotification::serviceRespondedWithAnError($response);
63
        }
64 1
    }
65
}
66