Passed
Push — master ( d54f6c...8277b0 )
by Giancarlos
02:59
created

DomCdrReader   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 97.37%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 1
dl 0
loc 98
ccs 37
cts 38
cp 0.9737
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getCdrResponse() 0 12 2
A getXpath() 0 9 1
A getResponseByXpath() 0 16 2
A getValueByName() 0 8 2
A getNotes() 0 16 3
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: Giansalex
5
 * Date: 22/07/2017
6
 * Time: 15:40
7
 */
8
9
namespace Greenter\Ws\Reader;
10
11
use Greenter\Model\Response\CdrResponse;
12
13
/**
14
 * Class DomCdrReader
15
 * @package Greenter\Ws\Reader
16
 */
17
class DomCdrReader implements CdrReader
18
{
19
    private $appResponseNamespace = 'urn:oasis:names:specification:ubl:schema:xsd:ApplicationResponse-2';
20
    private $cacNamespace = 'urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2';
21
22
    /**
23
     * Get Cdr using DomDocument.
24
     *
25
     * @param string $xml
26
     * @return CdrResponse
27
     * @throws \Exception
28
     */
29 8
    public function getCdrResponse($xml)
30
    {
31 8
        $xp = $this->getXpath($xml);
32
33 8
        $cdr = $this->getResponseByXpath($xp);
34 8
        if (!$cdr) {
35 2
            throw new \Exception('Not found cdr response in xml');
36
        }
37 6
        $cdr->setNotes($this->getNotes($xp));
38
39 6
        return $cdr;
40
    }
41
42
    /**
43
     * Get Xpath from xml content.
44
     *
45
     * @param string $xmlContent
46
     * @return \DOMXPath
47
     */
48 8
    private function getXpath($xmlContent)
49
    {
50 8
        $doc = new \DOMDocument();
51 8
        $doc->loadXML($xmlContent);
52 8
        $xp = new \DOMXPath($doc);
53 8
        $xp->registerNamespace('x', $this->appResponseNamespace);
54 8
        $xp->registerNamespace('cac', $this->cacNamespace);
55 8
        return $xp;
56
    }
57
58
    /**
59
     * @param \DOMXPath $xpath
60
     * @return CdrResponse
61
     */
62 8
    private function getResponseByXpath(\DOMXPath $xpath)
63
    {
64 8
        $resp = $xpath->query('/x:ApplicationResponse/cac:DocumentResponse/cac:Response');
65
66 8
        if ($resp->length !== 1) {
67 2
            return null;
68
        }
69 6
        $obj = $resp[0];
70
71 6
        $cdr = new CdrResponse();
72 6
        $cdr->setId($this->getValueByName($obj,'ReferenceID'))
73 6
            ->setCode($this->getValueByName($obj,'ResponseCode'))
74 6
            ->setDescription($this->getValueByName($obj,'Description'));
75
76 6
        return $cdr;
77
    }
78
79
80
    /**
81
     * @param \DOMElement $node
82
     * @param string $name
83
     * @return string
84
     */
85 6
    private function getValueByName(\DOMElement $node, $name)
86
    {
87 6
        $values = $node->getElementsByTagName($name);
88 6
        if ($values->length !== 1) {
89
            return '';
90
        }
91 6
        return $values[0]->nodeValue;
92
    }
93
94
    /**
95
     * @param \DOMXPath $xpath
96
     * @return string[]
97
     */
98 6
    private function getNotes(\DOMXPath $xpath)
99
    {
100 6
        $nodes = $xpath->query('/x:ApplicationResponse/cbc:Note');
101 6
        $notes = [];
102 6
        if ($nodes->length === 0) {
103 4
            return $notes;
104
        }
105
106
        /**@var \DOMElement $node*/
107 2
        foreach ($nodes as $node)
108
        {
109 2
            $notes[] = $node->nodeValue;
110 2
        }
111
112 2
        return $notes;
113
    }
114
}