Ping   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 17
c 1
b 0
f 0
dl 0
loc 77
ccs 14
cts 14
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A dispatch() 0 14 2
A getPsr7RequestHeader() 0 3 1
A setPsr7RequestHeader() 0 3 1
1
<?php
2
3
namespace Joesama\Webhook\Web;
4
5
use GuzzleHttp\Client;
6
use GuzzleHttp\Exception\TransferException;
7
use GuzzleHttp\Psr7\Request;
8
use Psr\Http\Message\RequestInterface;
9
use Psr\Http\Message\ResponseInterface;
10
11
abstract class Ping
12
{
13
    /**
14
     * Client configuration.
15
     *
16
     * @var array
17
     */
18
    protected array $configs = [];
19
20
    /**
21
     * Request method..
22
     *
23
     * @var string
24
     */
25
    protected string $method;
26
27
    /**
28
     * Endpoint base URI.
29
     *
30
     * @var string
31
     */
32
    protected string $pathUri;
33
34
    /**
35
     * Request payload parameters.
36
     *
37
     * @var array
38
     */
39
    protected array $options;
40
41
    /**
42
     * Request parameter.
43
     *
44
     * @var RequestInterface
45
     */
46
    protected RequestInterface $request;
47
48
    /**
49
     * Dispatch HTTP request.
50
     */
51
    final protected function dispatch(): ResponseInterface
52 1
    {
53
        $client = new Client($this->configs);
54 1
55
        $this->setPsr7RequestHeader();
56 1
57
        try {
58
            return $client->request(
59 1
                $this->method,
60 1
                $this->pathUri,
61 1
                $this->options
62 1
            );
63
        } catch (TransferException $exception) {
64 1
            return $this->exceptionHandlers($exception);
65 1
        }
66
    }
67
68
    /**
69
     * Make the client parameter acessible.
70
     */
71
    final protected function getPsr7RequestHeader(): RequestInterface
72 1
    {
73
        return $this->request;
74 1
    }
75
76
    /**
77
     * Set request parameter.
78
     */
79
    private function setPsr7RequestHeader(): void
80 1
    {
81
        $this->request = new Request($this->method, $this->pathUri, $this->configs);
82 1
    }
83 1
84
    /**
85
     * Optional handler for exception.
86
     */
87
    abstract protected function exceptionHandlers(TransferException $exception);
88
}
89