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
|
|
|
|