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

WebhookChannel::sendWebhookNotification()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 9
cts 9
cp 1
rs 9.7666
c 0
b 0
f 0
cc 2
nc 2
nop 3
crap 2
1
<?php
2
3
namespace NotificationChannels\Webhook;
4
5
use Illuminate\Support\Arr;
6
use GuzzleHttp\ClientInterface;
7
use GuzzleHttp\Exception\RequestException;
8
use Illuminate\Notifications\Notification;
9
use NotificationChannels\Webhook\Exceptions\CouldNotSendNotification;
10
11
class WebhookChannel
12
{
13
    /**
14
     * @var GuzzleHttp\ClientInterface
15
     */
16
    protected $client;
17
18
    /**
19
     * @param GuzzleHttp\ClientInterface
20
     */
21 3
    public function __construct(ClientInterface $client)
22
    {
23 3
        $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...
24 3
    }
25
26
    /**
27
     * Send the given notification.
28
     *
29
     * @param mixed $notifiable
30
     * @param \Illuminate\Notifications\Notification $notification
31
     * @return void
32
     *
33
     * @throws \NotificationChannels\Webhook\Exceptions\CouldNotSendNotification
34
     */
35 3
    public function send($notifiable, Notification $notification)
36
    {
37 3
        foreach ($this->getUrlsForNotifiable($notifiable) as $url) {
38 3
            $this->sendWebhookNotification($notifiable, $notification, $url);
39
        }
40 2
    }
41
42
    /**
43
     * Send the notification to a single webhook.
44
     *
45
     * @param mixed $notifiable
46
     * @param \Illuminate\Notifications\Notification $notification
47
     * @return void
48
     *
49
     * @throws \NotificationChannels\Webhook\Exceptions\CouldNotSendNotification
50
     */
51 3
    protected function sendWebhookNotification($notifiable, Notification $notification, string $url)
52
    {
53 3
        $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...
54
55 3
        [ '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...
56
57
        try {
58 3
            $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...
59 3
                'body'      => json_encode($data),
60 3
                'headers'   => $headers,
61
            ]);
62 1
        } catch (RequestException $exception) {
63 1
            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...
64
        }
65 2
    }
66
67
    /**
68
     * @param mixed $notifiable
69
     */
70 3
    protected function getUrlsForNotifiable($notifiable)
71
    {
72 3
        return Arr::wrap($notifiable->routeNotificationFor('Webhook'));
73
    }
74
}
75