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

RocketChatWebhookChannel::sendMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 2
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
    public function __construct(RocketChat $rocketChat)
29
    {
30
        $this->rocketChat = $rocketChat;
31
    }
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
    public function send($notifiable, Notification $notification): void
43
    {
44
        /** @var \NotificationChannels\RocketChat\RocketChatMessage $message */
45
        $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
        $to = $message->channel ?: $notifiable->routeNotificationFor('RocketChat');
0 ignored issues
show
Bug introduced by
The property channel does not seem to exist in NotificationChannels\RocketChat\RocketChatMessage.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
48
        if (! $to = $to ?: $this->rocketChat->channel()) {
49
            throw CouldNotSendNotification::missingTo();
50
        }
51
52
        if (! $from = $message->from ?: $this->rocketChat->token()) {
53
            throw CouldNotSendNotification::missingFrom();
54
        }
55
56
        try {
57
            $this->sendMessage($to, $message);
58
        } catch (ClientException $exception) {
59
            throw CouldNotSendNotification::rocketChatRespondedWithAnError($exception);
60
        } catch (Exception $exception) {
61
            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
    private function sendMessage($to, RocketChatMessage $message): ResponseInterface
71
    {
72
        return $this->rocketChat->sendMessage($to, $message->toArray());
73
    }
74
}
75