WebhookChannel   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 9
c 2
b 1
f 0
dl 0
loc 32
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A send() 0 8 2
A defaultHeaders() 0 3 1
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