Completed
Push — master ( 2a4379...5cdeb5 )
by Mohamed
02:00
created

WebhookChannel   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 4
c 3
b 1
f 0
lcom 1
cbo 4
dl 0
loc 40
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A send() 0 18 3
1
<?php
2
3
namespace NotificationChannels\Webhook;
4
5
use GuzzleHttp\Client;
6
use Illuminate\Support\Arr;
7
use NotificationChannels\Webhook\Exceptions\CouldNotSendNotification;
8
use NotificationChannels\Webhook\Events\MessageWasSent;
9
use NotificationChannels\Webhook\Events\SendingMessage;
10
use Illuminate\Notifications\Notification;
11
12
class WebhookChannel
13
{
14
    /** @var Client */
15
    protected $client;
16
17
    /**
18
     * @param Client $client
19
     */
20
    public function __construct(Client $client)
21
    {
22
        $this->client = $client;
23
    }
24
25
    /**
26
     * Send the given notification.
27
     *
28
     * @param mixed $notifiable
29
     * @param \Illuminate\Notifications\Notification $notification
30
     *
31
     * @throws \NotificationChannels\Webhook\Exceptions\CouldNotSendNotification
32
     */
33
    public function send($notifiable, Notification $notification)
34
    {
35
        if (! $url = $notifiable->routeNotificationFor('Webhook')) {
36
            return;
37
        }
38
39
        $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
        $response = $this->client->post($url, [
42
            'body' => json_encode(Arr::get($webhookData, 'data')),
43
            'verify' => false,
44
            'headers' => Arr::get($webhookData, 'headers'),
45
        ]);
46
47
        if ($response->getStatusCode() !== 200) {
48
            throw CouldNotSendNotification::serviceRespondedWithAnError($response);
49
        }
50
    }
51
}
52