Passed
Push — feature/eco-3047/eco-3049-invo... ( 77480f...6dcad0 )
by Aleksey
06:29
created

NotificationXmlConverter::simpleXmlToArray()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 11
rs 10
cc 3
nc 3
nop 1
1
<?php
2
3
/**
4
 * MIT License
5
 * For full license information, please view the LICENSE file that was distributed with this source code.
6
 */
7
8
namespace SprykerEco\Zed\Heidelpay\Business\Processor\Notification\Converter;
9
10
use SimpleXMLElement;
11
use SprykerEco\Zed\Heidelpay\Business\Exception\TransactionElementMissingException;
12
13
class NotificationXmlConverter implements NotificationXmlConverterInterface
14
{
15
    protected const TRANSACTION_ELEMENT = 'Transaction';
16
17
    /**
18
     * @param string $xml
19
     *
20
     * @return array
21
     */
22
    public function convert(string $xml): array
23
    {
24
        $xmlElement = $this->loadXml($xml);
25
26
        return $this->simpleXmlToArray($xmlElement);
27
    }
28
29
    /**
30
     * @param string $xml
31
     *
32
     * @throws \SprykerEco\Zed\Heidelpay\Business\Exception\TransactionElementMissingException
33
     *
34
     * @return \SimpleXMLElement
35
     */
36
    protected function loadXml(string $xml): SimpleXMLElement
37
    {
38
        $xmlProcess = new SimpleXMLElement($xml);
39
        if (!property_exists($xmlProcess, static::TRANSACTION_ELEMENT)) {
40
            throw new TransactionElementMissingException();
41
        }
42
43
        return $xmlProcess->Transaction;
44
    }
45
46
    /**
47
     * @param \SimpleXMLElement $xmlElement
48
     *
49
     * @return string[]
50
     */
51
    protected function simpleXmlToArray(SimpleXMLElement $xmlElement): array
52
    {
53
        $result = (array)$xmlElement;
54
55
        foreach ($result as $name => $value) {
56
            if ($value instanceof SimpleXMLElement) {
57
                $result[$name] = $this->simpleXmlToArray($value);
58
            }
59
        }
60
61
        return $result;
62
    }
63
}
64