|
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
|
|
|
* @param \NotificationChannels\HipChat\HipChat |
|
20
|
|
|
*/ |
|
21
|
|
|
public function __construct(HipChat $hipChat) |
|
22
|
|
|
{ |
|
23
|
|
|
$this->hipChat = $hipChat; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Send the given notification. |
|
28
|
|
|
* |
|
29
|
|
|
* @param mixed $notifiable |
|
30
|
|
|
* @param \Illuminate\Notifications\Notification $notification |
|
31
|
|
|
* |
|
32
|
|
|
* @throws \NotificationChannels\HipChat\Exceptions\CouldNotSendNotification |
|
33
|
|
|
*/ |
|
34
|
|
|
public function send($notifiable, Notification $notification) |
|
35
|
|
|
{ |
|
36
|
|
|
$message = $notification->toHipChat($notifiable); |
|
37
|
|
|
|
|
38
|
|
|
if (is_string($message)) { |
|
39
|
|
|
$message = new HipChatMessage($message); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
if (! is_a($message, HipChatMessage::class)) { |
|
43
|
|
|
throw CouldNotSendNotification::invalidMessageObject($message); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
$to = $message->room ?: $notifiable->routeNotificationFor('hipChat'); |
|
47
|
|
|
if (! $to = $to ?: $this->hipChat->room()) { |
|
48
|
|
|
throw CouldNotSendNotification::missingTo(); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
try { |
|
52
|
|
|
$this->sendMessage($to, $message); |
|
53
|
|
|
} catch (ClientException $exception) { |
|
54
|
|
|
throw CouldNotSendNotification::hipChatRespondedWithAnError($exception); |
|
55
|
|
|
} catch (\Exception $exception) { |
|
56
|
|
|
throw CouldNotSendNotification::internalError(); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Send the HipChat notification message. |
|
62
|
|
|
* |
|
63
|
|
|
* @param $to |
|
64
|
|
|
* @param mixed $message |
|
65
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
|
66
|
|
|
*/ |
|
67
|
|
|
protected function sendMessage($to, $message) |
|
68
|
|
|
{ |
|
69
|
|
|
if ($message instanceof HipChatMessage) { |
|
70
|
|
|
return $this->hipChat->sendMessage($to, $message->toArray()); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|