1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace Larium\Pay\Client; |
||
6 | |||
7 | use Http\Discovery\Psr17FactoryDiscovery; |
||
8 | use Http\Message\Authentication\BasicAuth; |
||
9 | use Psr\Http\Message\RequestInterface; |
||
10 | use Psr\Http\Message\ResponseInterface; |
||
11 | |||
12 | class RestClient extends AbstractClient |
||
13 | { |
||
14 | private string $username = ''; |
||
15 | |||
16 | private string $pass = ''; |
||
17 | |||
18 | private array $headerAuthentication = []; |
||
19 | |||
20 | 8 | public function __construct( |
|
21 | private readonly string $baseUri, |
||
22 | private readonly string $resource, |
||
23 | private array $headers = [], |
||
24 | array $options = [] |
||
25 | ) { |
||
26 | 8 | $this->options = $options; |
|
27 | } |
||
28 | |||
29 | 1 | public function addHeader(string $name, string $value): void |
|
30 | { |
||
31 | 1 | $this->headers[$name] = $value; |
|
32 | } |
||
33 | |||
34 | 1 | public function get(string $id = null, string|array $payload = ''): array |
|
35 | { |
||
36 | 1 | $uri = $this->getUri($id); |
|
37 | 1 | if ($query = $this->normalizePayload($payload)) { |
|
38 | 1 | $uri = $uri . '?' . ltrim($query, '?'); |
|
39 | } |
||
40 | |||
41 | 1 | return $this->request($uri, 'GET'); |
|
42 | } |
||
43 | |||
44 | 3 | public function post(string|array $payload): array |
|
45 | { |
||
46 | 3 | return $this->request($this->getUri(), 'POST', $payload); |
|
47 | } |
||
48 | |||
49 | 1 | public function put(string $id, string|array $payload = ''): array |
|
50 | { |
||
51 | 1 | $uri = $this->getUri($id); |
|
52 | |||
53 | 1 | return $this->request($uri, 'PUT', $payload); |
|
54 | } |
||
55 | |||
56 | 6 | private function request( |
|
57 | string $uri, |
||
58 | string $method, |
||
59 | string|array $payload = '' |
||
60 | ): array { |
||
61 | 6 | $factory = Psr17FactoryDiscovery::findRequestFactory(); |
|
62 | 6 | $request = $factory->createRequest( |
|
63 | 6 | $method, |
|
64 | 6 | $uri |
|
65 | 6 | ); |
|
66 | |||
67 | 6 | foreach ($this->headers as $name => $value) { |
|
68 | 1 | $request = $request->withHeader($name, $value); |
|
69 | } |
||
70 | |||
71 | 6 | if (is_array($payload)) { |
|
72 | 4 | $payload = $this->normalizePayload($payload); |
|
73 | } |
||
74 | |||
75 | 6 | if (!empty($payload)) { |
|
76 | 2 | $stream = Psr17FactoryDiscovery::findStreamFactory()->createStream($payload); |
|
77 | 2 | $request = $request->withBody($stream); |
|
78 | } |
||
79 | |||
80 | 6 | $request = $this->authenticate($request); |
|
81 | |||
82 | 6 | return $this->resolveResponse($this->sendRequest($request)); |
|
83 | } |
||
84 | |||
85 | 1 | public function delete(string $id): array |
|
86 | { |
||
87 | 1 | $uri = $this->getUri($id); |
|
88 | |||
89 | 1 | return $this->request($uri, 'DELETE'); |
|
90 | } |
||
91 | |||
92 | 8 | public function getUri(string $id = null): string |
|
93 | { |
||
94 | 8 | $uri = $this->resource |
|
95 | 7 | ? sprintf('%s/%s', $this->baseUri, $this->resource) |
|
96 | 1 | : $this->baseUri; |
|
97 | |||
98 | 8 | if ($id) { |
|
99 | 4 | $uri = sprintf($uri, $id); |
|
100 | } |
||
101 | |||
102 | 8 | return $uri; |
|
103 | } |
||
104 | |||
105 | 1 | public function setBasicAuthentication( |
|
106 | string $username, |
||
107 | string $password |
||
108 | ): void { |
||
109 | 1 | $this->username = $username; |
|
110 | 1 | $this->pass = $password; |
|
111 | } |
||
112 | |||
113 | 1 | public function setHeaderAuthentication(string $name, string $value): void |
|
114 | { |
||
115 | 1 | $this->headerAuthentication = ['name' => $name, 'value' => $value]; |
|
116 | } |
||
117 | |||
118 | 6 | protected function authenticate(RequestInterface $request): RequestInterface |
|
119 | { |
||
120 | 6 | if (!empty($this->username) || !empty($this->pass)) { |
|
121 | 1 | $authentication = new BasicAuth($this->username, $this->pass); |
|
122 | |||
123 | 1 | return $authentication->authenticate($request); |
|
124 | } |
||
125 | |||
126 | 5 | if (!empty($this->headerAuthentication)) { |
|
127 | 1 | $request = $request->withHeader( |
|
128 | 1 | $this->headerAuthentication['name'], |
|
129 | 1 | $this->headerAuthentication['value'] |
|
130 | 1 | ); |
|
131 | |||
132 | 1 | return $request; |
|
133 | } |
||
134 | |||
135 | 4 | return $request; |
|
136 | } |
||
137 | |||
138 | 5 | private function normalizePayload(string|array $payload): string |
|
139 | { |
||
140 | 5 | if (is_string($payload)) { |
|
0 ignored issues
–
show
introduced
by
![]() |
|||
141 | 1 | return $payload; |
|
142 | } |
||
143 | |||
144 | 4 | return http_build_query($payload); |
|
145 | } |
||
146 | |||
147 | /** |
||
148 | * Resolve the response from client. |
||
149 | * |
||
150 | * @param ResponseInterface $response |
||
151 | * @return array An array with following values: |
||
152 | * 'status': The Http status of response |
||
153 | * 'headers': An array of response headers |
||
154 | * 'body': The json decoded body response. (Since we are in |
||
155 | * RestClient) |
||
156 | * 'raw_response': The raw body response for logging purposes. |
||
157 | * 'raw_request': The raw body request for logging purposes. |
||
158 | */ |
||
159 | 6 | protected function resolveResponse(ResponseInterface $response): array |
|
160 | { |
||
161 | 6 | $body = $response->getBody()->__toString(); |
|
162 | 6 | $responseBody = json_decode($body, true) ?: []; |
|
163 | |||
164 | 6 | return [ |
|
165 | 6 | 'status' => $response->getStatusCode(), |
|
166 | 6 | 'headers' => $response->getHeaders(), |
|
167 | 6 | 'body' => $responseBody, |
|
168 | 6 | 'raw_response' => $body, |
|
169 | 6 | 'raw_request' => $this->rawRequest, |
|
170 | 6 | ]; |
|
171 | } |
||
172 | } |
||
173 |