Passed
Pull Request — master (#38)
by Darío
09:43 queued 07:25
created

AbstractClient::request()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace EasyHttp\LayerContracts;
4
5
use EasyHttp\LayerContracts\Contracts\EasyClientContract;
6
use EasyHttp\LayerContracts\Contracts\HttpClientAdapter;
7
use EasyHttp\LayerContracts\Contracts\HttpClientRequest;
8
use EasyHttp\LayerContracts\Contracts\HttpClientResponse;
9
10
abstract class AbstractClient implements EasyClientContract
11
{
12
    protected HttpClientAdapter $adapter;
13
14
    protected HttpClientRequest $request;
15
16
    protected $handler;
17
18 1
    public function getRequest(): HttpClientRequest
19
    {
20 1
        return $this->request;
21
    }
22
23 6
    public function call(string $method, string $uri): HttpClientResponse
24
    {
25 6
        $request = $this->buildRequest($method, $uri);
26 6
        return $this->getAdapter()->request($request);
27
    }
28
29 2
    public function request(string $method, string $uri): HttpClientRequest
30
    {
31 2
        $this->request = $this->buildRequest($method, $uri);
32
33 2
        return $this->request;
34
    }
35
36 4
    public function withHandler(callable $handler): self
37
    {
38 4
        $this->flushAdapter();
39 4
        $this->handler = $handler;
40
41 4
        return $this;
42
    }
43
44 1
    public function execute(): HttpClientResponse
45
    {
46 1
        return $this->getAdapter()->request($this->request);
47
    }
48
49 7
    protected function getAdapter(): HttpClientAdapter
50
    {
51 7
        if ($this->hasAdapter()) {
52 1
            return $this->adapter;
53
        }
54
55 7
        $this->adapter = $this->buildAdapter();
56
57 7
        return $this->adapter;
58
    }
59
60 7
    protected function hasAdapter(): bool
61
    {
62 7
        return (bool) ($this->adapter ?? null);
63
    }
64
65 7
    protected function hasHandler(): bool
66
    {
67 7
        return (bool) ($this->handler ?? null);
68
    }
69
70 4
    protected function flushAdapter(): void
71
    {
72 4
        unset($this->adapter);
73
    }
74
75
    abstract protected function buildRequest(string $method, string $uri): HttpClientRequest;
76
    abstract protected function buildAdapter(): HttpClientAdapter;
77
}
78