Completed
Pull Request — master (#28)
by Adam
07:23 queued 06:20
created

WebhookChannel   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 86.67%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 4
dl 0
loc 45
ccs 13
cts 15
cp 0.8667
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A send() 0 21 4
1
<?php
2
3
namespace NotificationChannels\Webhook;
4
5
use GuzzleHttp\Client;
6
use Illuminate\Support\Arr;
7
use Illuminate\Notifications\Notification;
8
use NotificationChannels\Webhook\Exceptions\CouldNotSendNotification;
9
10
class WebhookChannel
11
{
12
    /** @var Client */
13
    protected $client;
14
15
    /**
16
     * @param Client $client
17
     */
18 4
    public function __construct(Client $client)
19
    {
20 4
        $this->client = $client;
21 4
    }
22
23
    /**
24
     * Send the given notification.
25
     *
26
     * @param mixed $notifiable
27
     * @param \Illuminate\Notifications\Notification $notification
28
     *
29
     * @return \GuzzleHttp\Psr7\Response
30
     *
31
     * @throws \NotificationChannels\Webhook\Exceptions\CouldNotSendNotification
32
     */
33 4
    public function send($notifiable, Notification $notification)
34
    {
35 4
        if (! $url = $notifiable->routeNotificationFor('Webhook')) {
36
            return;
37
        }
38
39 4
        $webhookData = $notification->toWebhook($notifiable)->toArray();
0 ignored issues
show
Bug introduced by
The method toWebhook() 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...
40
41 4
        $response = $this->client->post($url, [
42 4
            'query' => Arr::get($webhookData, 'query'),
43 4
            'body' => json_encode(Arr::get($webhookData, 'data')),
44 4
            'verify' => Arr::get($webhookData, 'verify'),
45 4
            'headers' => Arr::get($webhookData, 'headers'),
46
        ]);
47
48 3
        if ($response->getStatusCode() >= 300 || $response->getStatusCode() < 200) {
49
            throw CouldNotSendNotification::serviceRespondedWithAnError($response);
0 ignored issues
show
Compatibility introduced by
$response of type object<Psr\Http\Message\ResponseInterface> is not a sub-type of object<GuzzleHttp\Psr7\Response>. It seems like you assume a concrete implementation of the interface Psr\Http\Message\ResponseInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
50
        }
51
52 3
        return $response;
53
    }
54
}
55