|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace EasyHttp\SymfonyLayer; |
|
4
|
|
|
|
|
5
|
|
|
use EasyHttp\LayerContracts\Contracts\HttpClientAdapter; |
|
6
|
|
|
use EasyHttp\LayerContracts\Contracts\HttpClientRequest; |
|
7
|
|
|
use EasyHttp\LayerContracts\Contracts\HttpClientResponse; |
|
8
|
|
|
use EasyHttp\LayerContracts\Exceptions\HttpClientException; |
|
9
|
|
|
use Symfony\Component\HttpClient\Exception\ClientException; |
|
10
|
|
|
use Symfony\Component\HttpClient\Exception\RedirectionException; |
|
11
|
|
|
use Symfony\Component\HttpClient\Exception\ServerException; |
|
12
|
|
|
use Symfony\Component\HttpClient\Exception\TransportException; |
|
13
|
|
|
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface; |
|
14
|
|
|
use Symfony\Contracts\HttpClient\HttpClientInterface; |
|
15
|
|
|
|
|
16
|
|
|
class SymfonyAdapter implements HttpClientAdapter |
|
17
|
|
|
{ |
|
18
|
|
|
protected HttpClientInterface $client; |
|
19
|
|
|
|
|
20
|
14 |
|
public function __construct(HttpClientInterface $client) |
|
21
|
|
|
{ |
|
22
|
14 |
|
$this->client = $client; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
14 |
|
public function request(HttpClientRequest $request): HttpClientResponse |
|
26
|
|
|
{ |
|
27
|
|
|
try { |
|
28
|
14 |
|
$response = $this->client->request( |
|
29
|
14 |
|
$request->getMethod(), |
|
30
|
14 |
|
$request->getUri(), |
|
31
|
14 |
|
$this->buildOptions($request) |
|
32
|
|
|
); |
|
33
|
|
|
|
|
34
|
12 |
|
return new SymfonyResponse($response, $response->getContent()); |
|
35
|
3 |
|
} catch (ClientException | RedirectionException $exception) { |
|
36
|
1 |
|
$response = $exception->getResponse(); |
|
37
|
2 |
|
} catch (ServerException | TransportException | TransportExceptionInterface $exception) { |
|
38
|
2 |
|
throw new HttpClientException($exception->getMessage(), (int) $exception->getCode(), $exception); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
1 |
|
return new SymfonyResponse($response); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
14 |
|
private function buildOptions(HttpClientRequest $request): array |
|
45
|
|
|
{ |
|
46
|
|
|
$options = [ |
|
47
|
14 |
|
'timeout' => $request->getTimeout(), |
|
48
|
14 |
|
'verify_peer' => $request->isSSL() |
|
49
|
|
|
]; |
|
50
|
|
|
|
|
51
|
|
|
$this |
|
52
|
14 |
|
->setHeaders($request, $options) |
|
53
|
14 |
|
->setJson($request, $options) |
|
54
|
14 |
|
->setQuery($request, $options) |
|
55
|
14 |
|
->setSecurityContext($request, $options) |
|
56
|
14 |
|
->setBasicAuth($request, $options); |
|
57
|
|
|
|
|
58
|
14 |
|
return $options; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
14 |
|
private function setHeaders(HttpClientRequest $request, &$options): self |
|
62
|
|
|
{ |
|
63
|
14 |
|
if ($request->hasHeaders()) { |
|
64
|
2 |
|
$options['headers'] = $request->getHeaders(); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
14 |
|
return $this; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
14 |
|
private function setJson(HttpClientRequest $request, &$options): self |
|
71
|
|
|
{ |
|
72
|
14 |
|
if ($request->hasJson()) { |
|
73
|
1 |
|
$options['json'] = $request->getJson(); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
14 |
|
return $this; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
14 |
|
private function setQuery(HttpClientRequest $request, &$options): self |
|
80
|
|
|
{ |
|
81
|
14 |
|
if ($request->hasQuery()) { |
|
82
|
3 |
|
$options['query'] = $request->getQuery(); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
14 |
|
return $this; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
14 |
|
private function setSecurityContext(HttpClientRequest $request, &$options): self |
|
89
|
|
|
{ |
|
90
|
14 |
|
if ($request->hasSecurityContext() && $request->getSecurityContext()->hasCertificate()) { |
|
91
|
|
|
$options['local_cert'] = $request->getSecurityContext()->getCertificate(); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
14 |
|
if ($request->hasSecurityContext() && $request->getSecurityContext()->hasPrivateKey()) { |
|
95
|
|
|
$options['local_pk'] = $request->getSecurityContext()->getPrivateKey(); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
14 |
|
return $this; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
14 |
|
private function setBasicAuth(HttpClientRequest $request, &$options): self |
|
102
|
|
|
{ |
|
103
|
14 |
|
if (count($request->getBasicAuth())) { |
|
104
|
2 |
|
$options['auth_basic'] = $request->getBasicAuth(); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
14 |
|
return $this; |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|