|
4 | 4 |
|
5 | 5 | namespace Yoti\DocScan\Exception;
|
6 | 6 |
|
7 |
| -use Psr\Http\Message\ResponseInterface; |
8 |
| -use Yoti\Util\Json; |
| 7 | +use Yoti\Exception\base\YotiException; |
9 | 8 |
|
10 |
| -class DocScanException extends \Exception |
| 9 | +class DocScanException extends YotiException |
11 | 10 | {
|
12 |
| - /** |
13 |
| - * @var ResponseInterface|null |
14 |
| - */ |
15 |
| - private $response; |
16 |
| - |
17 |
| - /** |
18 |
| - * DocScanException constructor. |
19 |
| - * @param string $message |
20 |
| - * @param ResponseInterface|null $response |
21 |
| - * @param \Throwable|null $previous |
22 |
| - */ |
23 |
| - public function __construct($message = "", ?ResponseInterface $response = null, \Throwable $previous = null) |
24 |
| - { |
25 |
| - parent::__construct($this->formatMessage($message, $response), 0, $previous); |
26 |
| - |
27 |
| - $this->response = $response; |
28 |
| - } |
29 |
| - |
30 |
| - /** |
31 |
| - * Returns the HTTP response object returned |
32 |
| - * from the Doc Scan API. |
33 |
| - * |
34 |
| - * @return ResponseInterface|null |
35 |
| - */ |
36 |
| - public function getResponse(): ?ResponseInterface |
37 |
| - { |
38 |
| - return $this->response; |
39 |
| - } |
40 |
| - |
41 |
| - /** |
42 |
| - * @param string $message |
43 |
| - * @param ResponseInterface|null $response |
44 |
| - * |
45 |
| - * @return string |
46 |
| - */ |
47 |
| - private function formatMessage(string $message, ?ResponseInterface $response): string |
48 |
| - { |
49 |
| - if ( |
50 |
| - $response === null || |
51 |
| - !$response->hasHeader('Content-Type') || |
52 |
| - $response->getHeader('Content-Type')[0] !== 'application/json' |
53 |
| - ) { |
54 |
| - return $message; |
55 |
| - } |
56 |
| - |
57 |
| - $jsonData = Json::decode((string) $response->getBody(), false); |
58 |
| - $formattedResponse = $this->formatResponse($jsonData); |
59 |
| - if ($formattedResponse !== null) { |
60 |
| - return sprintf('%s - %s', $message, $formattedResponse); |
61 |
| - } |
62 |
| - |
63 |
| - return $message; |
64 |
| - } |
65 |
| - |
66 |
| - /** |
67 |
| - * @param \stdClass $jsonData |
68 |
| - * |
69 |
| - * @return string|null |
70 |
| - */ |
71 |
| - private function formatResponse(\stdClass $jsonData): ?string |
72 |
| - { |
73 |
| - if (!isset($jsonData->message) || !isset($jsonData->code)) { |
74 |
| - return null; |
75 |
| - } |
76 |
| - |
77 |
| - $responseMessage = sprintf('%s - %s', $jsonData->code, $jsonData->message); |
78 |
| - |
79 |
| - $propertyErrors = $this->formatPropertyErrors($jsonData); |
80 |
| - if (count($propertyErrors) > 0) { |
81 |
| - return sprintf('%s: %s', $responseMessage, implode(', ', $propertyErrors)); |
82 |
| - } |
83 |
| - |
84 |
| - return $responseMessage; |
85 |
| - } |
86 |
| - |
87 |
| - /** |
88 |
| - * @param \stdClass $jsonData |
89 |
| - * |
90 |
| - * @return string[] |
91 |
| - */ |
92 |
| - private function formatPropertyErrors(\stdClass $jsonData): array |
93 |
| - { |
94 |
| - if (!isset($jsonData->errors) || !is_array($jsonData->errors)) { |
95 |
| - return []; |
96 |
| - } |
97 |
| - |
98 |
| - return array_reduce( |
99 |
| - $jsonData->errors, |
100 |
| - function ($carry, $error): array { |
101 |
| - if (isset($error->property) && isset($error->message)) { |
102 |
| - $carry[] = sprintf('%s "%s"', $error->property, $error->message); |
103 |
| - } |
104 |
| - return $carry; |
105 |
| - }, |
106 |
| - [] |
107 |
| - ); |
108 |
| - } |
109 | 11 | }
|
0 commit comments