CheckSwissPassValidityMethod::createRequestBody()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 28
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 17
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 28
ccs 18
cts 18
cp 1
crap 1
rs 9.7
1
<?php
2
3
namespace OrcaServices\NovaApi\Method;
4
5
use DOMDocument;
6
use Exception;
7
use OrcaServices\NovaApi\Parameter\NovaCheckSwissPassValidityParameter;
8
use OrcaServices\NovaApi\Parser\NovaApiErrorParser;
9
use OrcaServices\NovaApi\Parser\NovaMessageParser;
10
use OrcaServices\NovaApi\Result\NovaCheckSwissPassValidityResult;
11
use OrcaServices\NovaApi\Soap\NovaApiSoapAction;
12
use OrcaServices\NovaApi\Xml\XmlDocument;
13
14
/**
15
 * SOAP method.
16
 */
17
final class CheckSwissPassValidityMethod implements NovaMethod
18
{
19
    /**
20
     * @var NovaApiSoapAction
21
     */
22
    private $novaSoapAction;
23
24
    /**
25
     * @var NovaApiErrorParser
26
     */
27
    private $novaErrorParser;
28
29
    /**
30
     * @var NovaMessageParser
31
     */
32
    private $novaMessageParser;
33
34
    /**
35
     * NovaSearchPartnerMethod constructor.
36
     *
37
     * @param NovaApiSoapAction $novaSoapAction The novaSoapAction
38
     * @param NovaApiErrorParser $novaErrorParser The novaErrorParser
39
     * @param NovaMessageParser $novaMessageParser The message parser
40
     */
41 13
    public function __construct(
42
        NovaApiSoapAction $novaSoapAction,
43
        NovaApiErrorParser $novaErrorParser,
44
        NovaMessageParser $novaMessageParser
45
    ) {
46 13
        $this->novaSoapAction = $novaSoapAction;
47 13
        $this->novaErrorParser = $novaErrorParser;
48 13
        $this->novaMessageParser = $novaMessageParser;
49 13
    }
50
51
    /**
52
     * Check SwissPass validity.
53
     *
54
     * https://confluence-ext.sbb.ch/display/NOVAUG/pruefeSwissPassGueltigkeit
55
     *
56
     * @param NovaCheckSwissPassValidityParameter $parameter The parameter
57
     *
58
     * @throws Exception If an error occurs
59
     *
60
     * @return NovaCheckSwissPassValidityResult The result
61
     */
62 2
    public function checkSwissPassValidity(
63
        NovaCheckSwissPassValidityParameter $parameter
64
    ): NovaCheckSwissPassValidityResult {
65
        // The SOAP endpoint url
66 2
        $url = $this->novaSoapAction->getNovaSwisspassServiceUrl();
67
68
        // The SOAP action (http header)
69 2
        $soapAction = $this->novaSoapAction->getSoapAction('swisspass', 'pruefeSwissPassGueltigkeit');
70
71
        // The SOAP content (http body)
72 2
        $body = $this->createRequestBody($parameter);
73
74
        try {
75 2
            $xmlContent = $this->novaSoapAction->invokeSoapRequest($url, $soapAction, $body);
76 2
            $xml = XmlDocument::createFromXmlString($xmlContent);
77
78 2
            return $this->createResult($xml);
79
        } catch (Exception $exception) {
80
            throw $this->novaErrorParser->createGeneralException($exception);
81
        }
82
    }
83
84
    /**
85
     * Create SOAP body XML content.
86
     *
87
     * @param NovaCheckSwissPassValidityParameter $parameter The parameters
88
     *
89
     * @return string The xml content
90
     */
91 2
    private function createRequestBody(NovaCheckSwissPassValidityParameter $parameter): string
92
    {
93 2
        $dom = new DOMDocument('1.0', 'utf-8');
94 2
        $dom->formatOutput = true;
95
96 2
        $dom->appendChild($dom->createComment(' powered by Barakuda '));
97
98 2
        $envelope = $dom->createElement('soapenv:Envelope');
99 2
        $dom->appendChild($envelope);
100 2
        $envelope->setAttribute('xmlns:soapenv', 'http://schemas.xmlsoap.org/soap/envelope/');
101
102 2
        $soapHeader = $dom->createElement('soapenv:Header');
103 2
        $envelope->appendChild($soapHeader);
104
105 2
        $body = $dom->createElement('soapenv:Body');
106 2
        $envelope->appendChild($body);
107
108 2
        $method = $dom->createElement('novasp-swisspass:pruefeSwissPassGueltigkeit');
109 2
        $body->appendChild($method);
110
111 2
        $this->novaSoapAction->appendMethodNamespaces($method);
112
113 2
        $method->setAttribute('novasp-swisspass:tkid', $parameter->tkId);
114
115 2
        $this->novaSoapAction->appendDomClientIdentifier($dom, $method, $parameter, 'novasp-swisspass:');
116 2
        $this->novaSoapAction->appendDomCorrelationContext($dom, $method, $parameter, 'novasp-swisspass:');
117
118 2
        return (string)$dom->saveXML();
119
    }
120
121
    /**
122
     * Create result object.
123
     *
124
     * @param XmlDocument $xml The xml document
125
     *
126
     * @return NovaCheckSwissPassValidityResult The result
127
     */
128 2
    private function createResult(XmlDocument $xml): NovaCheckSwissPassValidityResult
129
    {
130 2
        $result = new NovaCheckSwissPassValidityResult();
131 2
        $xml = $xml->withoutNamespaces();
132
        // $content = $xml->getXml();
133
134
        // Find and append all messages
135 2
        foreach ($this->novaMessageParser->findNovaMessages($xml) as $message) {
136 1
            $result->addMessage($message);
137
        }
138
139
        // Root node
140 2
        $responseNode = $xml->queryFirstNode('//pruefeSwissPassGueltigkeitResponse');
141
142 2
        $resultValue = $xml->findAttributeValue('//@resultat', $responseNode);
143 2
        $result->result = $resultValue ?: '';
144
145 2
        $status = $xml->findAttributeValue('//@status', $responseNode);
146 2
        $result->status = $status ?: '';
147
148 2
        return $result;
149
    }
150
}
151