NotificationXmlConverter   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 8
eloc 16
c 3
b 0
f 0
dl 0
loc 54
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A convert() 0 5 1
A loadXml() 0 8 2
A simpleXmlToArray() 0 15 5
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\TransactionNodeMissingException;
12
13
class NotificationXmlConverter implements NotificationXmlConverterInterface
14
{
15
    protected const TRANSACTION_ELEMENT = 'Transaction';
16
    protected const EXCEPTION_MESSAGE_TRANSACTION_NODE_MISSING = 'Notification body has invalid body. Transaction node is missing.';
17
18
    /**
19
     * @param string $xml
20
     *
21
     * @return string[][]
22
     */
23
    public function convert(string $xml): array
24
    {
25
        $xmlElement = $this->loadXml($xml);
26
27
        return $this->simpleXmlToArray($xmlElement);
28
    }
29
30
    /**
31
     * @param string $xml
32
     *
33
     * @throws \SprykerEco\Zed\Heidelpay\Business\Exception\TransactionNodeMissingException
34
     *
35
     * @return \SimpleXMLElement
36
     */
37
    protected function loadXml(string $xml): SimpleXMLElement
38
    {
39
        $xmlProcess = new SimpleXMLElement($xml);
40
        if (!property_exists($xmlProcess, static::TRANSACTION_ELEMENT)) {
41
            throw new TransactionNodeMissingException(static::EXCEPTION_MESSAGE_TRANSACTION_NODE_MISSING);
42
        }
43
44
        return $xmlProcess->Transaction;
45
    }
46
47
    /**
48
     * @param \SimpleXMLElement $xmlElement
49
     *
50
     * @return string[][]
51
     */
52
    protected function simpleXmlToArray(SimpleXMLElement $xmlElement): array
53
    {
54
        $result = [];
55
56
        $attributes = $xmlElement->attributes();
57
        if ($xmlElement->count() > 0 && $attributes->count() > 0) {
58
            $result['@attributes'] = ((array)$attributes)['@attributes'];
59
        }
60
61
        foreach ($xmlElement->children() as $node) {
62
            /** @var \SimpleXMLElement $node */
63
            $result[$node->getName()] = $node->count() > 0 ? $this->simpleXmlToArray($node) : (string)$node;
64
        }
65
66
        return $result;
67
    }
68
}
69