FullReportResponseDecoder::decode()   A
last analyzed

Complexity

Conditions 5
Paths 16

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 5
eloc 13
c 3
b 0
f 0
nc 16
nop 1
dl 0
loc 24
rs 9.5222
1
<?php
2
3
declare(strict_types=1);
4
5
namespace GusApi\Util;
6
7
use GusApi\Exception\InvalidServerResponseException;
8
use GusApi\Type\Response\GetFullReportResponse;
9
use GusApi\Type\Response\GetFullReportResponseRaw;
10
use SimpleXMLElement;
11
12
class FullReportResponseDecoder
13
{
14
    /**
15
     * @throws InvalidServerResponseException
16
     */
17
    public static function decode(GetFullReportResponseRaw $fullReportResponseRaw): GetFullReportResponse
18
    {
19
        /** @var array<int, array<string, string>> $elements */
20
        $elements = [];
21
22
        if ('' === $fullReportResponseRaw->getDanePobierzPelnyRaportResult()) {
23
            return new GetFullReportResponse($elements);
24
        }
25
26
        try {
27
            $xmlElementsResponse = new SimpleXMLElement($fullReportResponseRaw->getDanePobierzPelnyRaportResult());
28
29
            foreach ($xmlElementsResponse->dane as $resultData) {
30
                /** @var array<string, string> $element */
31
                $element = [];
32
                foreach ($resultData as $key => $item) {
33
                    $element[$key] = (string) $item;
34
                }
35
                $elements[] = $element;
36
            }
37
38
            return new GetFullReportResponse($elements);
39
        } catch (\Exception $e) {
40
            throw new InvalidServerResponseException('Invalid server response', 0, $e);
41
        }
42
    }
43
}
44