Completed
Push — master ( 1a47ad...67b7fb )
by Davide
03:23
created

EngageChannel   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 94.74%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 5
dl 0
loc 55
ccs 18
cts 19
cp 0.9474
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A send() 0 30 4
1
<?php
2
3
namespace NotificationChannels\Engage;
4
5
use GuzzleHttp\Client;
6
use Illuminate\Notifications\Notification;
7
use NotificationChannels\Engage\Exceptions\CouldNotSendNotification;
8
use NotificationChannels\Engage\Exceptions\InvalidConfiguration;
9
10
class EngageChannel
11
{
12
    const API_ENDPOINT = 'https://pushcrew.com/api/v1/send/list';
13
14
    /** @var \GuzzleHttp\Client */
15
    protected $client;
16
17
    /**
18
     * @param \GuzzleHttp\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\Engage\Exceptions\InvalidConfiguration
32
     * @throws \NotificationChannels\Engage\Exceptions\CouldNotSendNotification
33
     */
34 4
    public function send($notifiable, Notification $notification)
35
    {
36 4
        $subscribers = collect($notifiable->routeNotificationFor('Engage'));
37
38 4
        if (! $subscribers->count()) {
39 1
            return;
40
        }
41
42 3
        if (! $token = config('services.vwo-engage.token')) {
43 1
            throw InvalidConfiguration::configurationNotSet();
44
        }
45
46 2
        $message = $notification->toEngage($notifiable);
0 ignored issues
show
Bug introduced by
The method toEngage() 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
        $data = $message->toArray() + [
49
            'subscriber_list' => json_encode([
50 2
                'subscriber_list' => $subscribers,
51
            ]),
52
        ];
53
54 2
        $response = $this->client->post(self::API_ENDPOINT, [
55 2
            'headers' => ['Authorization' => $token],
56
            'http_errors' => false,
57 2
            'form_params' => $data,
58
        ]);
59
60 2
        if ($response->getStatusCode() !== 200) {
61 1
            throw CouldNotSendNotification::serviceRespondedWithAnError($response);
62
        }
63 1
    }
64
}
65