|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace NotificationChannels\Gitter; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use GuzzleHttp\Client as HttpClient; |
|
7
|
|
|
use GuzzleHttp\Exception\ClientException; |
|
8
|
|
|
use Illuminate\Notifications\Notification; |
|
9
|
|
|
use NotificationChannels\Gitter\Exceptions\CouldNotSendNotification; |
|
10
|
|
|
|
|
11
|
|
|
class GitterChannel |
|
12
|
|
|
{ |
|
13
|
|
|
protected $baseUrl = 'https://api.gitter.im/v1/rooms'; |
|
14
|
|
|
|
|
15
|
|
|
/** @var HttpClient */ |
|
16
|
|
|
protected $httpClient; |
|
17
|
|
|
|
|
18
|
3 |
|
public function __construct(HttpClient $client) |
|
19
|
|
|
{ |
|
20
|
3 |
|
$this->httpClient = $client; |
|
21
|
3 |
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Send the given notification. |
|
25
|
|
|
* |
|
26
|
|
|
* @param mixed $notifiable |
|
27
|
|
|
* @param Notification $notification |
|
28
|
|
|
* |
|
29
|
|
|
* @return void |
|
30
|
|
|
*/ |
|
31
|
3 |
|
public function send($notifiable, Notification $notification): void |
|
32
|
|
|
{ |
|
33
|
|
|
/** @var GitterMessage $message */ |
|
34
|
3 |
|
$message = $notification->{'toGitter'}($notifiable); |
|
35
|
|
|
|
|
36
|
3 |
|
if (empty($message->room)) { |
|
37
|
1 |
|
$message->room($notifiable->routeNotificationFor('gitter')); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
3 |
|
$this->sendMessage($message); |
|
41
|
1 |
|
} |
|
42
|
|
|
|
|
43
|
3 |
|
protected function sendMessage(GitterMessage $message): void |
|
44
|
|
|
{ |
|
45
|
3 |
|
if (empty($message->room)) { |
|
46
|
1 |
|
throw CouldNotSendNotification::missingRoom(); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
2 |
|
if (empty($message->from)) { |
|
50
|
1 |
|
throw CouldNotSendNotification::missingFrom(); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
$options = [ |
|
54
|
1 |
|
'json' => ['text' => $message->content], |
|
55
|
|
|
'headers' => [ |
|
56
|
1 |
|
'Authorization' => "Bearer {$message->from}", |
|
57
|
|
|
], |
|
58
|
|
|
]; |
|
59
|
|
|
|
|
60
|
|
|
try { |
|
61
|
1 |
|
$this->httpClient->post("{$this->baseUrl}/{$message->room}/chatMessages", $options); |
|
62
|
|
|
} catch (ClientException $exception) { |
|
63
|
|
|
throw CouldNotSendNotification::gitterRespondedWithAnError($exception); |
|
64
|
|
|
} catch (Exception $exception) { |
|
65
|
|
|
throw CouldNotSendNotification::couldNotCommunicateWithGitter($exception); |
|
66
|
|
|
} |
|
67
|
1 |
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|