Test Failed
Push — master ( 11d608...27c98c )
by Giancarlos
02:46
created

DomCdrReader::getResponseByXpath()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 0
cts 10
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 10
nc 2
nop 1
crap 6
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
    /**
20
     * Get Cdr using DomDocument.
21
     *
22
     * @param string $xml
23
     * @return CdrResponse
24
     * @throws \Exception
25
     */
26
    public function getCdrResponse($xml)
27
    {
28
        $xpt = $this->getXpath($xml);
29
30
        $cdr = $this->getResponseByXpath($xpt);
31
        if (!$cdr) {
32
            throw new \Exception('Not found cdr response in xml');
33
        }
34
        $cdr->setNotes($this->getNotes($xpt));
35
36
        return $cdr;
37
    }
38
39
    /**
40
     * Get Xpath from xml content.
41
     *
42
     * @param string $xmlContent
43
     * @return \DOMXPath
44
     */
45
    private function getXpath($xmlContent)
46
    {
47
        $doc = new \DOMDocument();
48
        $doc->loadXML($xmlContent);
49
        $xpt = new \DOMXPath($doc);
50
        $xpt->registerNamespace('x', $doc->documentElement->namespaceURI);
51
        return $xpt;
52
    }
53
54
    /**
55
     * @param \DOMXPath $xpath
56
     * @return CdrResponse
57
     */
58
    private function getResponseByXpath(\DOMXPath $xpath)
59
    {
60
        $resp = $xpath->query('/x:ApplicationResponse/cac:DocumentResponse/cac:Response');
61
62
        if ($resp->length !== 1) {
63
            return null;
64
        }
65
        $obj = $resp[0];
66
67
        $cdr = new CdrResponse();
68
        $cdr->setId($this->getValueByName($obj,'ReferenceID'))
69
            ->setCode($this->getValueByName($obj,'ResponseCode'))
70
            ->setDescription($this->getValueByName($obj,'Description'));
71
72
        return $cdr;
73
    }
74
75
76
    /**
77
     * @param \DOMElement $node
78
     * @param string $name
79
     * @return string
80
     */
81
    private function getValueByName(\DOMElement $node, $name)
82
    {
83
        $values = $node->getElementsByTagName($name);
84
        if ($values->length !== 1) {
85
            return '';
86
        }
87
        return $values[0]->nodeValue;
88
    }
89
90
    /**
91
     * @param \DOMXPath $xpath
92
     * @return string[]
93
     */
94
    private function getNotes(\DOMXPath $xpath)
95
    {
96
        $nodes = $xpath->query('/x:ApplicationResponse/cbc:Note');
97
        $notes = [];
98
        if ($nodes->length === 0) {
99
            return $notes;
100
        }
101
102
        /**@var \DOMElement $node*/
103
        foreach ($nodes as $node)
104
        {
105
            $notes[] = $node->nodeValue;
106
        }
107
108
        return $notes;
109
    }
110
}