Completed
Push — master ( 3859e4...8b8edb )
by
unknown
03:04
created

WebhookChannel::send()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4.0092

Importance

Changes 0
Metric Value
cc 4
nc 3
nop 2
dl 0
loc 19
ccs 11
cts 12
cp 0.9167
crap 4.0092
rs 9.6333
c 0
b 0
f 0
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
     * @throws \NotificationChannels\Webhook\Exceptions\CouldNotSendNotification
30
     */
31 4
    public function send($notifiable, Notification $notification)
32
    {
33 4
        if (! $url = $notifiable->routeNotificationFor('Webhook')) {
34
            return;
35
        }
36
37 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...
38
39 4
        $response = $this->client->post($url, [
40 4
            'query' => Arr::get($webhookData, 'query'),
41 4
            'body' => json_encode(Arr::get($webhookData, 'data')),
42 4
            'verify' => Arr::get($webhookData, 'verify'),
43 4
            'headers' => Arr::get($webhookData, 'headers'),
44
        ]);
45
46 4
        if ($response->getStatusCode() >= 300 || $response->getStatusCode() < 200) {
47 1
            throw CouldNotSendNotification::serviceRespondedWithAnError($response);
48
        }
49 3
    }
50
}
51