1 | <?php |
||
28 | class GuzzleAdapter implements GuzzleAdapterInterface |
||
29 | { |
||
30 | /** @var ClientInterface */ |
||
31 | private $guzzleClient; |
||
32 | private $extended = false; |
||
33 | 89 | ||
34 | /** @var string |
||
35 | 89 | * since client->getConfig() is deprecated, we keep here a copy of the endpoint passed on "create()" |
|
36 | 89 | */ |
|
37 | private $baseUri = ''; |
||
38 | |||
39 | /** |
||
40 | * @var string |
||
41 | * since client->setUserAgent() is removed, we keep it here and pass it on exery "call()" |
||
42 | */ |
||
43 | 2 | private $userAgent = ''; |
|
44 | |||
45 | 2 | public function __construct(ClientInterface $guzzleClient) |
|
49 | |||
50 | /** |
||
51 | * {@inheritdoc} |
||
52 | * |
||
53 | 2 | * @return ClientInterface |
|
54 | */ |
||
55 | 2 | public function getGuzzle(): ClientInterface |
|
59 | |||
60 | /** |
||
61 | * Sets the baseUrl |
||
62 | * |
||
63 | 1 | * @param string $baseUrl |
|
64 | * @return GuzzleAdapter |
||
65 | 1 | */ |
|
66 | 1 | public function setBaseUrl(string $baseUrl): GuzzleAdapterInterface |
|
72 | |||
73 | /** |
||
74 | * Returns the client base URL |
||
75 | * |
||
76 | 6 | * @return string |
|
77 | 1 | */ |
|
78 | public function getBaseUrl(): string |
||
83 | |||
84 | /** |
||
85 | * Sets the user agent |
||
86 | * |
||
87 | * @param string $userAgent |
||
88 | * @return GuzzleAdapter |
||
89 | */ |
||
90 | public function setUserAgent(string $userAgent): GuzzleAdapterInterface |
||
97 | |||
98 | /** |
||
99 | * @return string |
||
100 | */ |
||
101 | public function getUserAgent(): string |
||
105 | |||
106 | /** |
||
107 | * Sets extended mode |
||
108 | * |
||
109 | * Extended mode fetch more data (status, meta, subdefs) in one request |
||
110 | * for a record |
||
111 | * |
||
112 | * @param bool $extended |
||
113 | * @return GuzzleAdapter |
||
114 | 82 | */ |
|
115 | 82 | public function setExtended(bool $extended): GuzzleAdapterInterface |
|
121 | 2 | ||
122 | 17 | /** |
|
123 | 15 | * @return boolean |
|
124 | 2 | */ |
|
125 | 1 | public function isExtended(): bool |
|
129 | |||
130 | /** |
||
131 | * Performs an HTTP request, returns the body response |
||
132 | * |
||
133 | * @param string $method The method |
||
134 | * @param string $path The path to query |
||
135 | * @param array $query An array of query parameters |
||
136 | * @param array $postFields An array of post fields |
||
137 | * @param array $files An array of post files |
||
138 | * @param array $headers An array of request headers |
||
139 | 6 | * |
|
140 | * @return string The response body |
||
141 | * |
||
142 | * @throws InvalidArgumentException |
||
143 | 6 | * @throws BadResponseException |
|
144 | * @throws RuntimeException |
||
145 | */ |
||
146 | public function call(string $method, string $path, array $query = [], array $postFields = [], array $files = [], array $headers = []): string |
||
200 | |||
201 | /** |
||
202 | * Creates a new instance of GuzzleAdapter |
||
203 | * |
||
204 | * @param string $endpoint |
||
205 | * @return static |
||
206 | */ |
||
207 | public static function create(string $endpoint): GuzzleAdapterInterface |
||
246 | |||
247 | /* |
||
248 | private function addRequestParameters(RequestInterface $request, $query, $postFields, $files) |
||
249 | { |
||
250 | foreach ($query as $name => $value) { |
||
251 | $request->getQuery()->add($name, $value); |
||
252 | } |
||
253 | |||
254 | // todo : EntityEnclosingRequestInterface ??? |
||
255 | if ($request instanceof EntityEnclosingRequestInterface) { |
||
256 | if ($request->getHeader('Content-Type') == 'application/json') { |
||
257 | $request->getHeaders()->offsetUnset('Content-Type'); |
||
258 | $request->setBody(json_encode($postFields)); |
||
259 | |||
260 | return; |
||
261 | } |
||
262 | |||
263 | foreach ($postFields as $name => $value) { |
||
264 | $request->getPostFields()->add($name, $value); |
||
265 | } |
||
266 | foreach ($files as $name => $filename) { |
||
267 | $request->addPostFile($name, $filename); |
||
268 | } |
||
269 | } elseif (0 < count($postFields)) { |
||
270 | throw new InvalidArgumentException('Can not add post fields to GET request'); |
||
271 | } |
||
272 | } |
||
273 | */ |
||
274 | } |
||
275 |