Passed
Push — master ( d02db2...64404d )
by Daniel
01:09 queued 11s
created

NovaSearchServicesMethod::createRequestBody()   B

Complexity

Conditions 7
Paths 8

Size

Total Lines 49
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 31
CRAP Score 7

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 30
c 1
b 0
f 0
nc 8
nop 1
dl 0
loc 49
ccs 31
cts 31
cp 1
crap 7
rs 8.5066
1
<?php
2
3
namespace OrcaServices\NovaApi\Method;
4
5
use DOMDocument;
6
use DOMElement;
7
use Exception;
8
use OrcaServices\NovaApi\Parameter\NovaSearchServicesParameter;
9
use OrcaServices\NovaApi\Parser\NovaApiErrorParser;
10
use OrcaServices\NovaApi\Parser\NovaMessageParser;
11
use OrcaServices\NovaApi\Result\NovaSearchServicesResult;
12
use OrcaServices\NovaApi\Result\NovaServiceResult;
13
use OrcaServices\NovaApi\Soap\NovaApiSoapAction;
14
use OrcaServices\NovaApi\Xml\XmlDocument;
15
16
/**
17
 * SOAP method.
18
 */
19
final class NovaSearchServicesMethod implements NovaMethod
20
{
21
    /**
22
     * @var NovaApiSoapAction
23
     */
24
    private $novaSoapAction;
25
26
    /**
27
     * @var NovaApiErrorParser
28
     */
29
    private $novaErrorParser;
30
31
    /**
32
     * @var NovaMessageParser
33
     */
34
    private $novaMessageParser;
35
36
    /**
37
     * NovaSearchPartnerMethod constructor.
38
     *
39
     * @param NovaApiSoapAction $novaSoapAction The novaSoapAction
40
     * @param NovaApiErrorParser $novaErrorParser The novaErrorParser
41
     * @param NovaMessageParser $novaMessageParser The message parser
42
     */
43 11
    public function __construct(
44
        NovaApiSoapAction $novaSoapAction,
45
        NovaApiErrorParser $novaErrorParser,
46
        NovaMessageParser $novaMessageParser
47
    ) {
48 11
        $this->novaSoapAction = $novaSoapAction;
49 11
        $this->novaErrorParser = $novaErrorParser;
50 11
        $this->novaMessageParser = $novaMessageParser;
51 11
    }
52
53
    /**
54
     * Search NOVA services (customer) for a customer.
55
     *
56
     * https://confluence-ext.sbb.ch/display/NOVAUG/sucheLeistungen
57
     *
58
     * @param NovaSearchServicesParameter $parameter The parameters
59
     *
60
     * @throws Exception
61
     *
62
     * @return NovaSearchServicesResult The services matching the search parameter
63
     */
64 1
    public function searchServices(NovaSearchServicesParameter $parameter): NovaSearchServicesResult
65
    {
66
        // The SOAP endpoint url
67 1
        $url = $this->novaSoapAction->getNovaSalesServiceUrl();
68
69
        // The SOAP action (http header)
70 1
        $soapAction = $this->novaSoapAction->getSoapAction('geschaeftspartner', 'sucheLeistungen');
71
72
        // The SOAP content (http body)
73 1
        $body = $this->createRequestBody($parameter);
74
75
        try {
76 1
            $xmlContent = $this->novaSoapAction->invokeSoapRequest($url, $soapAction, $body);
77 1
            $xml = XmlDocument::createFromXmlString($xmlContent);
78
79 1
            return $this->createResult($xml);
80
        } catch (Exception $exception) {
81
            throw $this->novaErrorParser->createGeneralException($exception);
82
        }
83
    }
84
85
    /**
86
     * Create SOAP body XML content.
87
     *
88
     * @param NovaSearchServicesParameter $parameter The parameters
89
     *
90
     * @return string The xml content
91
     */
92 1
    private function createRequestBody(NovaSearchServicesParameter $parameter): string
93
    {
94 1
        $dom = new DOMDocument('1.0', 'utf-8');
95 1
        $dom->formatOutput = true;
96
97 1
        $dom->appendChild($dom->createComment(' powered by Barakuda '));
98
99 1
        $envelope = $dom->createElement('soapenv:Envelope');
100 1
        $dom->appendChild($envelope);
101 1
        $envelope->setAttribute('xmlns:soapenv', 'http://schemas.xmlsoap.org/soap/envelope/');
102
103 1
        $soapHeader = $dom->createElement('soapenv:Header');
104 1
        $envelope->appendChild($soapHeader);
105
106 1
        $body = $dom->createElement('soapenv:Body');
107 1
        $envelope->appendChild($body);
108
109 1
        $method = $dom->createElement('sucheLeistungen');
110 1
        $body->appendChild($method);
111
112 1
        $searchQuery = $dom->createElement('leistungsSuchRequest');
113 1
        $this->novaSoapAction->appendDomClientIdentifier($dom, $searchQuery, $parameter, '');
114 1
        $this->novaSoapAction->appendDomCorrelationContext($dom, $searchQuery, $parameter, '');
115
116 1
        $service = $dom->createElement('leistung');
117
118 1
        $tkid = $dom->createElement('tkid');
119 1
        $tkid->appendChild($dom->createTextNode($parameter->tkId));
120 1
        $service->appendChild($tkid);
121
122 1
        if ($parameter->periodOfUseStart || $parameter->periodOfUseEnd) {
123 1
            $periodOfUse = $dom->createElement('nutzungsZeitraum');
124 1
            $service->appendChild($periodOfUse);
125
        }
126
127 1
        if ($parameter->periodOfUseStart && isset($periodOfUse)) {
128 1
            $periodOfUse->setAttribute('base:von', $parameter->periodOfUseStart->format('Y-m-d'));
129
        }
130
131 1
        if ($parameter->periodOfUseEnd && isset($periodOfUse)) {
132 1
            $periodOfUse->setAttribute('base:bis', $parameter->periodOfUseEnd->format('Y-m-d'));
133
        }
134
135 1
        $searchQuery->appendChild($service);
136 1
        $method->appendChild($searchQuery);
137
138 1
        $this->novaSoapAction->appendMethodNamespaces($method);
139
140 1
        return (string)$dom->saveXML();
141
    }
142
143
    /**
144
     * Create result object.
145
     *
146
     * @param XmlDocument $xml The xml document
147
     *
148
     * @return NovaSearchServicesResult The mapped result
149
     */
150 1
    private function createResult(XmlDocument $xml): NovaSearchServicesResult
151
    {
152 1
        $result = new NovaSearchServicesResult();
153
154 1
        $xml = $xml->withoutNamespaces();
155
156
        // Find and append all messages
157 1
        foreach ($this->novaMessageParser->findNovaMessages($xml) as $message) {
158
            $result->addMessage($message);
159
        }
160
161
        // Root node
162 1
        $responseNode = $xml->queryFirstNode('/Envelope/Body/sucheLeistungenResponse/leistungsSuchResponse');
163 1
        $serviceNodes = $xml->queryNodes('leistungsSuchErgebnis/leistung', $responseNode);
164
165
        /** @var DOMElement $serviceNode */
166 1
        foreach ($serviceNodes as $serviceNode) {
167 1
            $result->addService($this->createService($serviceNode, $xml));
168
        }
169
170 1
        return $result;
171
    }
172
173
    /**
174
     * Map partner node to NovaPartner object.
175
     *
176
     * @param DOMElement $serviceNode The partnerNode
177
     * @param XmlDocument $xml The xml document
178
     *
179
     * @return NovaServiceResult The new NovaService instance
180
     */
181 1
    private function createService(DOMElement $serviceNode, XmlDocument $xml): NovaServiceResult
182
    {
183 1
        $service = new NovaServiceResult();
184
185 1
        $service->tkId = $xml->getNodeValue('verkaufsParameter/wert/tkid', $serviceNode);
186 1
        $service->validFrom = $xml->createChronosFromXsDateTime(
187 1
            $xml->getAttributeValue('nutzungsInfo/nutzungsZeitraum/tarifierbarerZeitraum/@von', $serviceNode)
188
        );
189 1
        $service->validTo = $xml->createChronosFromXsDateTime(
190 1
            $xml->getAttributeValue('nutzungsInfo/nutzungsZeitraum/tarifierbarerZeitraum/@bis', $serviceNode)
191
        );
192
193 1
        $service->productNumber = $xml->getAttributeValue('@produktNummer', $serviceNode);
194
195 1
        foreach ($xml->queryNodes('geltungsBereich/zonenGeltungsBereich/zonenBuendel/zonen', $serviceNode) as $zone) {
196 1
            $service->addZone($xml->getNodeValue('code', $zone));
197
        }
198
199 1
        return $service;
200
    }
201
}
202