|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace NotificationChannels\Webhook; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Arr; |
|
6
|
|
|
use GuzzleHttp\ClientInterface; |
|
7
|
|
|
use GuzzleHttp\Exception\RequestException; |
|
8
|
|
|
use Illuminate\Notifications\Notification; |
|
9
|
|
|
use NotificationChannels\Webhook\Exceptions\CouldNotSendNotification; |
|
10
|
|
|
|
|
11
|
|
|
class WebhookChannel |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var GuzzleHttp\ClientInterface |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $client; |
|
17
|
|
|
|
|
18
|
3 |
|
/** |
|
19
|
|
|
* @param GuzzleHttp\ClientInterface |
|
20
|
3 |
|
*/ |
|
21
|
3 |
|
public function __construct(ClientInterface $client) |
|
22
|
|
|
{ |
|
23
|
|
|
$this->client = $client; |
|
|
|
|
|
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Send the given notification. |
|
28
|
|
|
* |
|
29
|
|
|
* @param mixed $notifiable |
|
30
|
|
|
* @param \Illuminate\Notifications\Notification $notification |
|
31
|
3 |
|
* @return void |
|
32
|
|
|
* |
|
33
|
3 |
|
* @throws \NotificationChannels\Webhook\Exceptions\CouldNotSendNotification |
|
34
|
|
|
*/ |
|
35
|
|
|
public function send($notifiable, Notification $notification) |
|
36
|
|
|
{ |
|
37
|
3 |
|
foreach ($this->getUrlsForNotifiable($notifiable) as $url) { |
|
38
|
|
|
$this->sendWebhookNotification($notifiable, $notification, $url); |
|
39
|
3 |
|
} |
|
40
|
3 |
|
} |
|
41
|
|
|
|
|
42
|
3 |
|
/** |
|
43
|
|
|
* Send the notification to a single webhook. |
|
44
|
|
|
* |
|
45
|
3 |
|
* @param mixed $notifiable |
|
46
|
1 |
|
* @param \Illuminate\Notifications\Notification $notification |
|
47
|
|
|
* @return void |
|
48
|
2 |
|
* |
|
49
|
|
|
* @throws \NotificationChannels\Webhook\Exceptions\CouldNotSendNotification |
|
50
|
|
|
*/ |
|
51
|
|
|
protected function sendWebhookNotification($notifiable, Notification $notification, string $url) |
|
52
|
|
|
{ |
|
53
|
|
|
$webhookData = $notification->toWebhook($notifiable)->toArray(); |
|
|
|
|
|
|
54
|
|
|
|
|
55
|
|
|
[ 'data' => $data, 'headers' => $headers ] = $webhookData; |
|
|
|
|
|
|
56
|
|
|
|
|
57
|
|
|
try { |
|
58
|
|
|
$response = $this->client->post($url, [ |
|
|
|
|
|
|
59
|
|
|
'body' => json_encode($data), |
|
60
|
|
|
'headers' => $headers, |
|
61
|
|
|
]); |
|
62
|
|
|
} catch (RequestException $exception) { |
|
63
|
|
|
throw CouldNotSendNotification::serviceRespondedWithAnError($exception->getResponse()); |
|
|
|
|
|
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param mixed $notifiable |
|
69
|
|
|
*/ |
|
70
|
|
|
protected function getUrlsForNotifiable($notifiable) |
|
71
|
|
|
{ |
|
72
|
|
|
return Arr::wrap($notifiable->routeNotificationFor('Webhook')); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..