Completed
Pull Request — master (#3)
by
unknown
02:03
created

RocketChatWebhookChannel::send()   B

Complexity

Conditions 8
Paths 10

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 8.2327

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 11
cts 13
cp 0.8462
rs 8.4444
c 0
b 0
f 0
cc 8
nc 10
nop 2
crap 8.2327
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
use Psr\Http\Message\ResponseInterface;
12
13
final class RocketChatWebhookChannel
14
{
15
    /**
16
     * The HTTP client instance.
17
     *
18
     * @var \NotificationChannels\RocketChat\RocketChat
19
     */
20
    private $rocketChat;
21
22
    /**
23
     * Create a new RocketChat channel instance.
24
     *
25
     * @param  \NotificationChannels\RocketChat\RocketChat $rocketChat
26
     * @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...
27
     */
28 3
    public function __construct(RocketChat $rocketChat)
29
    {
30 3
        $this->rocketChat = $rocketChat;
31 3
    }
32
33
    /**
34
     * Send the given notification.
35
     *
36
     * @param  mixed  $notifiable
37
     * @param  \Illuminate\Notifications\Notification $notification
38
     * @return void
39
     *
40
     * @throws \NotificationChannels\RocketChat\Exceptions\CouldNotSendNotification
41
     */
42 3
    public function send($notifiable, Notification $notification): void
43
    {
44
        /** @var \NotificationChannels\RocketChat\RocketChatMessage $message */
45 3
        $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...
46
47 3
        $to = $message->channel ?: $notifiable->routeNotificationFor('RocketChat');
48 3
        if (! $to = $to ?: $this->rocketChat->channel()) {
49 1
            throw CouldNotSendNotification::missingTo();
50
        }
51
52 2
        if (! $from = $message->from ?: $this->rocketChat->token()) {
53 1
            throw CouldNotSendNotification::missingFrom();
54
        }
55
56
        try {
57 1
            $this->sendMessage($to, $message);
58 1
        } catch (ClientException $exception) {
59
            throw CouldNotSendNotification::rocketChatRespondedWithAnError($exception);
60 1
        } catch (Exception $exception) {
61 1
            throw CouldNotSendNotification::couldNotCommunicateWithRocketChat($exception);
62
        }
63
    }
64
65
    /**
66
     * @param  string  $to
67
     * @param  \NotificationChannels\RocketChat\RocketChatMessage  $message
68
     * @return \Psr\Http\Message\ResponseInterface
69
     */
70 1
    private function sendMessage($to, RocketChatMessage $message): ResponseInterface
71
    {
72 1
        return $this->rocketChat->sendMessage($to, $message->toArray());
73
    }
74
}
75