Passed
Push — master ( 37b4ec...6e8e43 )
by Darío
02:14 queued 10s
created

AbstractClient::flushAdapter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
ccs 2
cts 2
cp 1
crap 1
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 4
    public function call(string $method, string $uri): HttpClientResponse
24
    {
25 4
        $request = $this->buildRequest($method, $uri);
26 4
        return $this->getAdapter()->request($request);
27
    }
28
29 2
    public function prepareRequest(string $method, string $uri): self
30
    {
31 2
        $this->request = $this->buildRequest($method, $uri);
32
33 2
        return $this;
34
    }
35
36 2
    public function withHandler(callable $handler): self
37
    {
38 2
        $this->flushAdapter();
39 2
        $this->handler = $handler;
40
41 2
        return $this;
42
    }
43
44 1
    public function execute(): HttpClientResponse
45
    {
46 1
        return $this->getAdapter()->request($this->request);
47
    }
48
49 5
    protected function getAdapter(): HttpClientAdapter
50
    {
51 5
        if ($this->hasAdapter()) {
52 1
            return $this->adapter;
53
        }
54
55 5
        $this->adapter = $this->buildAdapter();
56
57 5
        return $this->adapter;
58
    }
59
60 5
    protected function hasAdapter(): bool
61
    {
62 5
        return (bool) ($this->adapter ?? null);
63
    }
64
65 5
    protected function hasHandler(): bool
66
    {
67 5
        return (bool) ($this->handler ?? null);
68
    }
69
70 2
    protected function flushAdapter(): void
71
    {
72 2
        unset($this->adapter);
73 2
    }
74
75
    abstract protected function buildRequest(string $method, string $uri): HttpClientRequest;
76
    abstract protected function buildAdapter(): HttpClientAdapter;
77
}
78