Completed
Pull Request — master (#7)
by Anton
01:26
created

RocketChatWebhookChannel   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 94.74%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 3
dl 0
loc 58
ccs 18
cts 19
cp 0.9474
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B send() 0 22 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
        if (! $to = $to ?: $this->rocketChat->getDefaultChannel()) {
44 1
            throw CouldNotSendNotification::missingTo();
45
        }
46
47 3
        if (! $from = $message->getFrom() ?: $this->rocketChat->getToken()) {
48 1
            throw CouldNotSendNotification::missingFrom();
49
        }
50
51
        try {
52 2
            $this->sendMessage($to, $message);
53 1
        } catch (ClientException $exception) {
54
            throw CouldNotSendNotification::rocketChatRespondedWithAnError($exception);
55 1
        } catch (Exception $exception) {
56 1
            throw CouldNotSendNotification::couldNotCommunicateWithRocketChat($exception);
57
        }
58 1
    }
59
60
    /**
61
     * @param  string  $to
62
     * @param  \NotificationChannels\RocketChat\RocketChatMessage  $message
63
     * @return void
64
     */
65 2
    private function sendMessage(string $to, RocketChatMessage $message): void
66
    {
67 2
        $this->rocketChat->sendMessage($to, $message->toArray());
68 1
    }
69
}
70