Completed
Pull Request — master (#23)
by
unknown
11:18
created

WebhookChannel   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 92.31%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 0
loc 64
ccs 12
cts 13
cp 0.9231
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A send() 0 6 2
A sendWebhookNotification() 0 15 2
A getUrlsForNotifiable() 0 4 1
1
<?php
2
3
namespace NotificationChannels\Webhook;
4
5
use GuzzleHttp\Exception\RequestException;
6
use GuzzleHttp\ClientInterface;
7
8
use Illuminate\Support\Arr;
9
use Illuminate\Notifications\Notification;
10
11
use NotificationChannels\Webhook\Exceptions\CouldNotSendNotification;
12
13
class WebhookChannel
14
{
15
    /**
16
     * @var GuzzleHttp\ClientInterface
17
     */
18 3
    protected $client;
19
20 3
    /**
21 3
     * @param GuzzleHttp\ClientInterface
22
     */
23
    public function __construct(ClientInterface $client)
24
    {
25
        $this->client = $client;
0 ignored issues
show
Documentation Bug introduced by
It seems like $client of type object<GuzzleHttp\ClientInterface> is incompatible with the declared type object<NotificationChann...leHttp\ClientInterface> of property $client.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
26
    }
27
28
    /**
29
     * Send the given notification.
30
     *
31 3
     * @param mixed $notifiable
32
     * @param \Illuminate\Notifications\Notification $notification
33 3
     * @return void
34
     *
35
     * @throws \NotificationChannels\Webhook\Exceptions\CouldNotSendNotification
36
     */
37 3
    public function send($notifiable, Notification $notification)
38
    {
39 3
        foreach ($this->getUrlsForNotifiable($notifiable) as $url) {
40 3
            $this->sendWebhookNotification($notifiable, $notification, $url);
41
        }
42 3
    }
43
44
    /**
45 3
     * Send the notification to a single webhook
46 1
     *
47
     * @param mixed $notifiable
48 2
     * @param \Illuminate\Notifications\Notification $notification
49
     * @return void
50
     *
51
     * @throws \NotificationChannels\Webhook\Exceptions\CouldNotSendNotification
52
     */
53
    protected function sendWebhookNotification($notifiable, Notification $notification, string $url)
54
    {
55
        $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...
56
57
        [ 'data' => $data, 'headers' => $headers ] = $webhookData;
0 ignored issues
show
Bug introduced by
The variable $data does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $headers does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
58
59
        try {
60
            $response = $this->client->post($url, [
0 ignored issues
show
Unused Code introduced by
$response is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
61
                'body'      => json_encode($data),
62
                'headers'   => $headers 
63
            ]);
64
        } catch (RequestException $exception) {
65
            throw CouldNotSendNotification::serviceRespondedWithAnError($exception->getResponse());
0 ignored issues
show
Bug introduced by
It seems like $exception->getResponse() can be null; however, serviceRespondedWithAnError() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
66
        }
67
    }
68
69
    /**
70
     * @param mixed $notifiable
71
     */
72
    protected function getUrlsForNotifiable($notifiable)
73
    {
74
        return Arr::wrap($notifiable->routeNotificationFor('Webhook'));
75
    }
76
}
77