HipChatChannel   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 75%

Importance

Changes 0
Metric Value
dl 0
loc 71
ccs 18
cts 24
cp 0.75
rs 10
c 0
b 0
f 0
wmc 12
lcom 1
cbo 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B send() 0 25 8
A sendMessage() 0 10 3
1
<?php
2
3
namespace NotificationChannels\HipChat;
4
5
use Exception;
6
use GuzzleHttp\Exception\ClientException;
7
use Illuminate\Notifications\Notification;
8
use NotificationChannels\HipChat\Exceptions\CouldNotSendNotification;
9
10
class HipChatChannel
11
{
12
    /**
13
     * The HipChat client instance.
14
     *
15
     * @var \NotificationChannels\HipChat\HipChat
16
     */
17
    protected $hipChat;
18
19
    /**
20
     * Create a new instance of HipChatChannel.
21
     *
22
     * @param \NotificationChannels\HipChat\HipChat
23
     */
24 5
    public function __construct(HipChat $hipChat)
25
    {
26 5
        $this->hipChat = $hipChat;
27 5
    }
28
29
    /**
30
     * Send the given notification.
31
     *
32
     * @param mixed $notifiable
33
     * @param \Illuminate\Notifications\Notification $notification
34
     *
35
     * @throws \NotificationChannels\HipChat\Exceptions\CouldNotSendNotification
36
     */
37 4
    public function send($notifiable, Notification $notification)
38
    {
39 4
        $message = $notification->toHipChat($notifiable);
0 ignored issues
show
Bug introduced by
The method toHipChat() 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...
40
41 4
        if (is_string($message)) {
42
            $message = new HipChatMessage($message);
43
        }
44
45 4
        if (! in_array(get_class($message), [HipChatMessage::class, HipChatFile::class])) {
46 1
            throw CouldNotSendNotification::invalidMessageObject($message);
47
        }
48
49 3
        $to = $message->room ?: $notifiable->routeNotificationFor('HipChat');
50 3
        if (! $to = $to ?: $this->hipChat->room()) {
51 1
            throw CouldNotSendNotification::missingTo();
52
        }
53
54
        try {
55 2
            $this->sendMessage($to, $message);
56
        } catch (ClientException $exception) {
57
            throw CouldNotSendNotification::hipChatRespondedWithAnError($exception);
58
        } catch (Exception $exception) {
59
            throw CouldNotSendNotification::internalError($exception);
60
        }
61 2
    }
62
63
    /**
64
     * Send the HipChat notification message.
65
     *
66
     * @param $to
67
     * @param mixed $message
68
     * @return \Psr\Http\Message\ResponseInterface
69
     */
70 2
    protected function sendMessage($to, $message)
71
    {
72 2
        if ($message instanceof HipChatMessage) {
73 1
            return $this->hipChat->sendMessage($to, $message->toArray());
74
        }
75
76 1
        if ($message instanceof HipChatFile) {
77 1
            return $this->hipChat->shareFile($to, $message->toArray());
78
        }
79
    }
80
}
81