Completed
Push — master ( 4c54bb...1c8578 )
by Marcel
07:02
created

WunderlistChannel::send()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 27
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 4.0032

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 0
loc 27
ccs 16
cts 17
cp 0.9412
rs 8.5806
cc 4
eloc 15
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
        if (! $routing = collect($notifiable->routeNotificationFor('Wunderlist'))) {
38
            return;
39
        }
40
41 3
        $key = config('services.wunderlist.key');
42
43 3
        if (is_null($key)) {
44 1
            throw InvalidConfiguration::configurationNotSet();
45
        }
46
47 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...
48
49 2
        $response = $this->client->post(self::API_ENDPOINT, [
50 2
            'body' => json_encode(Arr::set($wunderlistParameters, 'list_id', (int) $routing->get('list_id'))),
51
            'headers' => [
52 2
                'X-Client-ID' => $key,
53 2
                'X-Access-Token' => $routing->get('token'),
54 2
                'Content-Type' => 'application/json',
55 2
            ],
56 2
        ]);
57
58 2
        if ($response->getStatusCode() !== 201) {
59 1
            throw CouldNotSendNotification::serviceRespondedWithAnError($response);
60
        }
61 1
    }
62
}
63