|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of PHP CS Fixer. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Fabien Potencier <[email protected]> |
|
7
|
|
|
* Dariusz Rumiński <[email protected]> |
|
8
|
|
|
* |
|
9
|
|
|
* This source file is subject to the MIT license that is bundled |
|
10
|
|
|
* with this source code in the file LICENSE. |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace Etrias\EwarehousingConnector\Client; |
|
14
|
|
|
|
|
15
|
|
|
use Etrias\EwarehousingConnector\Exceptions\BadRequestException; |
|
16
|
|
|
use GuzzleHttp\Client; |
|
17
|
|
|
use GuzzleHttp\Exception\ClientException; |
|
18
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
19
|
|
|
use Throwable; |
|
20
|
|
|
|
|
21
|
|
|
class EwarehousingClient extends Client implements EwarehousingClientInterface |
|
22
|
|
|
{ |
|
23
|
|
|
public function __call($method, $args) |
|
24
|
|
|
{ |
|
25
|
|
|
try { |
|
26
|
|
|
$response = parent::__call($method, $args); |
|
27
|
|
|
$response = $this->parseError($response); |
|
|
|
|
|
|
28
|
|
|
} catch (ClientException $e) { |
|
29
|
|
|
$response = $this->parseError($e->getResponse(), $e); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
return $response; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param ResponseInterface $body |
|
37
|
|
|
* |
|
38
|
|
|
* @throws BadRequestException |
|
39
|
|
|
* |
|
40
|
|
|
* @return ResponseInterface |
|
41
|
|
|
*/ |
|
42
|
|
|
protected function parseError(ResponseInterface $response, Throwable $previous = null) |
|
43
|
|
|
{ |
|
44
|
|
|
$body = json_decode($response->getBody(), true); |
|
45
|
|
|
|
|
46
|
|
|
if ($body === null && $previous !== null) { |
|
47
|
|
|
throw $previous; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
if (is_array($body)) { |
|
51
|
|
|
$errors = $body['errors'] ?? $body['error'] ?? null; |
|
52
|
|
|
if (is_array($errors)) { |
|
53
|
|
|
$exception = new BadRequestException('multiple exceptions see childs', null, $previous); |
|
54
|
|
|
foreach ($errors as $error) { |
|
55
|
|
|
$exception->addChild(new BadRequestException(json_encode($error), null, $previous)); |
|
56
|
|
|
} |
|
57
|
|
|
throw $exception; |
|
58
|
|
|
} elseif ($errors !== null) { |
|
59
|
|
|
throw new BadRequestException($body['error'] ?? 'Bad eWarehousing request', null, $previous); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
return $response; |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|