RocketChatWebhookChannel   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 90.48%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 3
dl 0
loc 60
ccs 19
cts 21
cp 0.9048
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B send() 0 24 8
A sendMessage() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace NotificationChannels\RocketChat;
6
7
use Exception;
8
use GuzzleHttp\Exception\ClientException;
9
use Illuminate\Notifications\Notification;
10
use NotificationChannels\RocketChat\Exceptions\CouldNotSendNotification;
11
12
final class RocketChatWebhookChannel
13
{
14
    /** @var \NotificationChannels\RocketChat\RocketChat The HTTP client instance. */
15
    private $rocketChat;
16
17
    /**
18
     * Create a new RocketChat channel instance.
19
     *
20
     * @param  \NotificationChannels\RocketChat\RocketChat $rocketChat
21
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
22
     */
23 4
    public function __construct(RocketChat $rocketChat)
24
    {
25 4
        $this->rocketChat = $rocketChat;
26 4
    }
27
28
    /**
29
     * Send the given notification.
30
     *
31
     * @param  mixed  $notifiable
32
     * @param  \Illuminate\Notifications\Notification $notification
33
     * @return void
34
     *
35
     * @throws \NotificationChannels\RocketChat\Exceptions\CouldNotSendNotification
36
     */
37 4
    public function send($notifiable, Notification $notification): void
38
    {
39
        /** @var \NotificationChannels\RocketChat\RocketChatMessage $message */
40 4
        $message = $notification->toRocketChat($notifiable);
0 ignored issues
show
Bug introduced by
The method toRocketChat() 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...
41
42 4
        $to = $message->getChannel() ?: $notifiable->routeNotificationFor('RocketChat');
43 4
        $to = $to ?: $this->rocketChat->getDefaultChannel();
44 4
        if ($to === null) {
45
            throw CouldNotSendNotification::missingTo();
46
        }
47
48 4
        $from = $message->getFrom() ?: $this->rocketChat->getToken();
49 4
        if (! $from) {
50 1
            throw CouldNotSendNotification::missingFrom();
51
        }
52
53
        try {
54 3
            $this->sendMessage($to, $message);
55 2
        } catch (ClientException $exception) {
56
            throw CouldNotSendNotification::rocketChatRespondedWithAnError($exception);
57 2
        } catch (Exception $exception) {
58 2
            throw CouldNotSendNotification::couldNotCommunicateWithRocketChat($exception);
59
        }
60 1
    }
61
62
    /**
63
     * @param  string  $to
64
     * @param  \NotificationChannels\RocketChat\RocketChatMessage  $message
65
     * @return void
66
     */
67 3
    private function sendMessage(string $to, RocketChatMessage $message): void
68
    {
69 3
        $this->rocketChat->sendMessage($to, $message->toArray());
70 1
    }
71
}
72