|
3 | 3 | namespace h4kuna\Ares\Adis; |
4 | 4 |
|
5 | 5 | use h4kuna\Ares\Adis\Soap\Envelope; |
6 | | -use h4kuna\Ares\Exceptions\ServerResponseException; |
| 6 | +use h4kuna\Ares\Exception\ServerResponseException; |
7 | 7 | use h4kuna\Ares\Http\TransportProvider; |
8 | | -use h4kuna\Ares\Tools\Xml; |
| 8 | +use h4kuna\Ares\Tool\Arrays; |
| 9 | +use h4kuna\Ares\Tool\Integer; |
| 10 | +use h4kuna\Ares\Tool\Strings; |
| 11 | +use h4kuna\Ares\Tool\Xml; |
| 12 | +use SimpleXMLElement; |
9 | 13 | use stdClass; |
10 | 14 |
|
11 | 15 | final class Client |
12 | 16 | { |
13 | 17 | public static string $url = 'https://adisrws.mfcr.cz/adistc/axis2/services/rozhraniCRPDPH.rozhraniCRPDPHSOAP'; |
14 | 18 |
|
15 | | - |
16 | | - public function __construct(private TransportProvider $transportProvider,) |
17 | | - { |
| 19 | + public function __construct( |
| 20 | + private TransportProvider $transportProvider, |
| 21 | + ) { |
18 | 22 | } |
19 | 23 |
|
20 | 24 |
|
21 | 25 | /** |
22 | 26 | * @param array<string, string> $chunk |
23 | | - * @return array<stdClass> |
| 27 | + * @return list<stdClass> |
| 28 | + * |
| 29 | + * @throws ServerResponseException |
24 | 30 | */ |
25 | 31 | public function statusBusinessSubjects(array $chunk): array |
26 | 32 | { |
27 | 33 | $xml = Envelope::StatusNespolehlivySubjektRozsireny(...$chunk); |
28 | 34 | $data = $this->request($xml, 'StatusNespolehlivySubjektRozsirenyResponse'); |
29 | 35 | $attributes = '@attributes'; |
30 | | - assert($data->status instanceof stdClass); |
31 | | - if (isset($data->status->$attributes) === false) { |
32 | | - throw new ServerResponseException('Broken response xml.'); |
| 36 | + |
| 37 | + if ( |
| 38 | + $data->status instanceof stdClass === false |
| 39 | + || isset($data->status->$attributes) === false |
| 40 | + || $data->status->$attributes instanceof stdClass === false |
| 41 | + ) { |
| 42 | + throw ServerResponseException::brokenXml(); |
33 | 43 | } |
34 | 44 | $element = $data->status->$attributes; |
35 | | - assert($element instanceof stdClass); |
36 | | - |
37 | 45 | if ($element->statusCode !== '0') { |
38 | | - throw new ServerResponseException($element->statusText, (int) $element->statusCode); |
| 46 | + throw ServerResponseException::badResponse(Strings::fromMixedStrict($element->statusText), (int) Integer::fromMixed($element->statusCode)); |
39 | 47 | } |
40 | 48 |
|
41 | | - return is_array($data->statusSubjektu) ? $data->statusSubjektu : [$data->statusSubjektu]; |
| 49 | + return Arrays::fromStdClass($data->statusSubjektu); |
42 | 50 | } |
43 | 51 |
|
44 | | - |
| 52 | + /** |
| 53 | + * @throws ServerResponseException |
| 54 | + */ |
45 | 55 | private function request(string $xml, string $name): stdClass |
46 | 56 | { |
47 | 57 | $request = $this->transportProvider->createXmlRequest(self::$url, $xml); |
48 | 58 | $response = $this->transportProvider->response($request); |
49 | 59 | $xml = @simplexml_load_string($response->getBody()->getContents(), namespace_or_prefix: 'soapenv', is_prefix: true); |
50 | 60 |
|
51 | | - if ($xml === false || isset($xml->Body->children()->$name) === false) { |
52 | | - throw new ServerResponseException(sprintf('Missing tag "%s" in response.', $name)); |
| 61 | + if ($xml === false || ($xml->Body->children()->$name instanceof SimpleXMLElement) === false) { |
| 62 | + throw ServerResponseException::badResponse(sprintf('Missing tag "%s" in response.', $name)); |
53 | 63 | } |
54 | 64 |
|
55 | 65 | return Xml::toJson($xml->Body->children()->$name); |
|
0 commit comments