Completed
Push — master ( 3f8806...162df9 )
by Peter
02:17
created

HipChatChannel   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 12
c 2
b 0
f 0
lcom 1
cbo 4
dl 0
loc 71
ccs 0
cts 34
cp 0
rs 10

3 Methods

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