Passed
Push — master ( 06646a...1e9aa0 )
by Darío
04:01 queued 01:55
created

AbstractClient   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Test Coverage

Coverage 68.42%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 50
ccs 13
cts 19
cp 0.6842
rs 10
wmc 7

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setAdapter() 0 3 1
A setRequest() 0 3 1
A getRequest() 0 3 1
A prepareRequest() 0 5 1
A withHandler() 0 5 1
A execute() 0 3 1
A call() 0 4 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 2
    protected function setRequest(HttpClientRequest $request): void
24
    {
25 2
        $this->request = $request;
26 2
    }
27
28
    protected function setAdapter(HttpClientAdapter $adapter): void
29
    {
30
        $this->adapter = $adapter;
31
    }
32
33 1
    public function call(string $method, string $uri): HttpClientResponse
34
    {
35 1
        $request = $this->buildRequest($method, $uri);
36 1
        return $this->getAdapter()->request($request);
37
    }
38
39 2
    public function prepareRequest(string $method, string $uri): self
40
    {
41 2
        $this->setRequest($this->buildRequest($method, $uri));
42
43 2
        return $this;
44
    }
45
46
    public function withHandler(callable $handler): self
47
    {
48
        $this->handler = $handler;
49
50
        return $this;
51
    }
52
53 1
    public function execute(): HttpClientResponse
54
    {
55 1
        return $this->getAdapter()->request($this->request);
56
    }
57
58
    abstract protected function buildRequest(string $method, string $uri): HttpClientRequest;
59
    abstract protected function getAdapter(): HttpClientAdapter;
60
}
61