Completed
Pull Request — master (#17)
by
unknown
04:01
created

HipChatChannel::send()   D

Complexity

Conditions 9
Paths 30

Size

Total Lines 29
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 17.6098

Importance

Changes 0
Metric Value
dl 0
loc 29
ccs 10
cts 19
cp 0.5263
rs 4.909
c 0
b 0
f 0
cc 9
eloc 17
nc 30
nop 2
crap 17.6098
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 3
    public function __construct(HipChat $hipChat)
24
    {
25 3
        $this->hipChat = $hipChat;
26 3
    }
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 2
    public function send($notifiable, Notification $notification)
37
    {
38 2
        $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 2
        if (is_string($message)) {
41
            $message = new HipChatMessage($message);
42
        }
43
44 2
        if (! in_array(get_class($message), [HipChatMessage::class, HipChatFile::class])) {
45
            throw CouldNotSendNotification::invalidMessageObject($message);
46
        }
47
48 2
        $to = $message->room ?: $notifiable->routeNotificationFor('HipChat');
49 2
        if (! $to = $to ?: $this->hipChat->room()) {
50
            throw CouldNotSendNotification::missingTo();
51
        }
52
        
53 2
        if (! is_null($message->token)) {
54
            $this->hipChat->token($message->token);
55
        }
56
57
        try {
58 1
            $this->sendMessage($to, $message);
59 1
        } catch (ClientException $exception) {
60
            throw CouldNotSendNotification::hipChatRespondedWithAnError($exception);
61
        } catch (\Exception $exception) {
62
            throw CouldNotSendNotification::internalError();
63
        }
64 1
    }
65
66
    /**
67
     * Send the HipChat notification message.
68
     *
69
     * @param $to
70
     * @param mixed $message
71
     * @return \Psr\Http\Message\ResponseInterface
72
     */
73 1
    protected function sendMessage($to, $message)
74
    {
75 1
        if ($message instanceof HipChatMessage) {
76 1
            return $this->hipChat->sendMessage($to, $message->toArray());
77
        }
78
79
        if ($message instanceof HipChatFile) {
80 1
            return $this->hipChat->shareFile($to, $message->toArray());
81
        }
82
    }
83
}
84