Completed
Pull Request — master (#11)
by
unknown
05:38
created

WunderlistChannel   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 94.74%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B send() 0 29 4
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 3
        if (! Arr::has($routing, ['token', 'list_id'])) {
40
            return;
41
        }
42
43 3
        $key = config('services.wunderlist.key');
44
45 3
        if (is_null($key)) {
46 1
            throw InvalidConfiguration::configurationNotSet();
47
        }
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
51 2
        $response = $this->client->post(self::API_ENDPOINT, [
52 2
            'body' => json_encode(Arr::set($wunderlistParameters, 'list_id', (int) $routing->get('list_id'))),
53
            'headers' => [
54 2
                'X-Client-ID' => $key,
55 2
                'X-Access-Token' => $routing->get('token'),
56 2
                'Content-Type' => 'application/json',
57
            ],
58
        ]);
59
60 2
        if ($response->getStatusCode() !== 201) {
61 1
            throw CouldNotSendNotification::serviceRespondedWithAnError($response);
62
        }
63 1
    }
64
}
65