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); |
|
|
|
|
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
|
|
|
|
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.