DataSearchDecoder::decodeSingleResult()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 6
nc 3
nop 1
dl 0
loc 13
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace GusApi\Util;
6
7
use GusApi\Exception\InvalidServerResponseException;
8
use GusApi\Exception\NotFoundException;
9
use GusApi\Type\Response\SearchDataResponse;
10
use GusApi\Type\Response\SearchResponseCompanyData;
11
use GusApi\Type\Response\SearchResponseRaw;
12
use SimpleXMLElement;
13
14
class DataSearchDecoder
15
{
16
    /**
17
     * @throws InvalidServerResponseException
18
     * @throws NotFoundException
19
     */
20
    public static function decode(SearchResponseRaw $searchResponseRaw): SearchDataResponse
21
    {
22
        if ('' === $searchResponseRaw->getDaneSzukajPodmiotyResult()) {
23
            return new SearchDataResponse();
24
        }
25
26
        try {
27
            $xmlElementsResponse = new SimpleXMLElement($searchResponseRaw->getDaneSzukajPodmiotyResult());
28
        } catch (\Exception $e) {
29
            throw new InvalidServerResponseException('Invalid server response', 0, $e);
30
        }
31
32
        $elements = [];
33
34
        foreach ($xmlElementsResponse->dane as $resultData) {
35
            $elements[] = self::decodeSingleResult($resultData);
0 ignored issues
show
Bug introduced by
It seems like $resultData can also be of type null; however, parameter $element of GusApi\Util\DataSearchDe...r::decodeSingleResult() does only seem to accept SimpleXMLElement, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

35
            $elements[] = self::decodeSingleResult(/** @scrutinizer ignore-type */ $resultData);
Loading history...
36
        }
37
38
        return new SearchDataResponse($elements);
39
    }
40
41
    /**
42
     * @throws NotFoundException
43
     */
44
    private static function decodeSingleResult(SimpleXMLElement $element): SearchResponseCompanyData
45
    {
46
        $result = new SearchResponseCompanyData();
47
48
        foreach ($element as $key => $item) {
49
            if ('ErrorCode' === $key && 4 === (int) $item) {
50
                throw new NotFoundException('No data found');
51
            }
52
53
            $result->$key = (string) $item;
54
        }
55
56
        return $result;
57
    }
58
}
59