1 | <?php |
||
2 | |||
3 | |||
4 | namespace SirSova\Webhooks; |
||
5 | |||
6 | use GuzzleHttp\ClientInterface; |
||
7 | use GuzzleHttp\Exception\GuzzleException; |
||
8 | use GuzzleHttp\Psr7\Request; |
||
9 | use SirSova\Webhooks\Contracts\Channel; |
||
10 | use SirSova\Webhooks\Exceptions\WebhookSendingException; |
||
11 | |||
12 | class WebhookChannel implements Channel |
||
13 | { |
||
14 | /** |
||
15 | * @var ClientInterface |
||
16 | */ |
||
17 | private $client; |
||
18 | |||
19 | public function __construct(ClientInterface $client) |
||
20 | { |
||
21 | $this->client = $client; |
||
22 | } |
||
23 | |||
24 | /** |
||
25 | * @param Webhook $webhook |
||
26 | */ |
||
27 | public function send(Webhook $webhook): void |
||
28 | { |
||
29 | $request = new Request('POST', $webhook->url(), $this->defaultHeaders(), json_encode($webhook->context())); |
||
30 | |||
31 | try { |
||
32 | $response = $this->client->send($request); |
||
0 ignored issues
–
show
Unused Code
introduced
by
![]() |
|||
33 | } catch (GuzzleException $exception) { |
||
34 | throw new WebhookSendingException($exception); |
||
35 | } |
||
36 | } |
||
37 | |||
38 | /** |
||
39 | * @return array |
||
40 | */ |
||
41 | protected function defaultHeaders(): array |
||
42 | { |
||
43 | return ['Content-type' => 'application/json']; |
||
44 | } |
||
45 | } |
||
46 |