WunderlistChannel::send()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 4.0032

Importance

Changes 0
Metric Value
dl 0
loc 29
ccs 16
cts 17
cp 0.9412
rs 9.456
c 0
b 0
f 0
cc 4
nc 4
nop 2
crap 4.0032
1
<?php
2
3
namespace NotificationChannels\Wunderlist;
4
5
use GuzzleHttp\Client;
6
use Illuminate\Support\Arr;
7
use NotificationChannels\Wunderlist\Exceptions\CouldNotSendNotification;
8
use Illuminate\Notifications\Notification;
9
use NotificationChannels\Wunderlist\Exceptions\InvalidConfiguration;
10
11
class WunderlistChannel
12
{
13
    const API_ENDPOINT = 'https://a.wunderlist.com/api/v1/tasks';
14
15
    /** @var Client */
16
    protected $client;
17
18
    /**
19
     * @param Client $client
20
     */
21 3
    public function __construct(Client $client)
22
    {
23 3
        $this->client = $client;
24 3
    }
25
26
    /**
27
     * Send the given notification.
28
     *
29
     * @param mixed $notifiable
30
     * @param \Illuminate\Notifications\Notification $notification
31
     *
32
     * @throws \NotificationChannels\Wunderlist\Exceptions\InvalidConfiguration
33
     * @throws \NotificationChannels\Wunderlist\Exceptions\CouldNotSendNotification
34
     */
35 3
    public function send($notifiable, Notification $notification)
36
    {
37 3
        $routing = collect($notifiable->routeNotificationFor('Wunderlist'));
38
39
        if (! Arr::has($routing, ['token', 'list_id'])) {
40
            return;
41 3
        }
42
43 3
        $key = config('services.wunderlist.key');
44 1
45
        if (is_null($key)) {
46
            throw InvalidConfiguration::configurationNotSet();
47 2
        }
48
49 2
        $wunderlistParameters = $notification->toWunderlist($notifiable)->toArray();
0 ignored issues
show
Bug introduced by
The method toWunderlist() 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...
50 2
51
        $response = $this->client->post(self::API_ENDPOINT, [
52 2
            'body' => json_encode(Arr::set($wunderlistParameters, 'list_id', (int) $routing->get('list_id'))),
53 2
            'headers' => [
54 2
                'X-Client-ID' => $key,
55 2
                'X-Access-Token' => $routing->get('token'),
56 2
                'Content-Type' => 'application/json',
57
            ],
58 2
        ]);
59 1
60
        if ($response->getStatusCode() !== 201) {
61 1
            throw CouldNotSendNotification::serviceRespondedWithAnError($response);
62
        }
63
    }
64
}
65