WebhookChannel::send()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 8
rs 10
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
The assignment to $response is dead and can be removed.
Loading history...
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