NovaPurchaseServicesMethod::createResult()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 39
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 4.0105

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 22
c 1
b 0
f 0
nc 6
nop 1
dl 0
loc 39
ccs 21
cts 23
cp 0.913
crap 4.0105
rs 9.568
1
<?php
2
3
namespace OrcaServices\NovaApi\Method;
4
5
use DomainException;
6
use DOMDocument;
7
use DOMElement;
8
use Exception;
9
use OrcaServices\NovaApi\Parameter\NovaPurchaseServicesParameter;
10
use OrcaServices\NovaApi\Parser\NovaApiErrorParser;
11
use OrcaServices\NovaApi\Parser\NovaMessageParser;
12
use OrcaServices\NovaApi\Result\NovaPurchaseServicesResult;
13
use OrcaServices\NovaApi\Result\NovaServiceItem;
14
use OrcaServices\NovaApi\Soap\NovaApiSoapAction;
15
use OrcaServices\NovaApi\Xml\XmlDocument;
16
17
/**
18
 * 3 Klang.
19
 */
20
final class NovaPurchaseServicesMethod implements NovaMethod
21
{
22
    /**
23
     * @var NovaApiSoapAction
24
     */
25
    private $novaSoapAction;
26
27
    /**
28
     * @var NovaApiErrorParser
29
     */
30
    private $novaErrorParser;
31
32
    /**
33
     * @var NovaMessageParser
34
     */
35
    private $novaMessageParser;
36
37
    /**
38
     * NovaSearchPartnerMethod constructor.
39
     *
40
     * @param NovaApiSoapAction $novaSoapAction The novaSoapAction
41
     * @param NovaApiErrorParser $novaErrorParser The novaErrorParser
42
     * @param NovaMessageParser $novaMessageParser The message parser
43
     */
44 13
    public function __construct(
45
        NovaApiSoapAction $novaSoapAction,
46
        NovaApiErrorParser $novaErrorParser,
47
        NovaMessageParser $novaMessageParser
48
    ) {
49 13
        $this->novaSoapAction = $novaSoapAction;
50 13
        $this->novaErrorParser = $novaErrorParser;
51 13
        $this->novaMessageParser = $novaMessageParser;
52 13
    }
53
54
    /**
55
     * 3. Klang.
56
     *
57
     * The next step is to accept the requested OFFER.
58
     * This subroutine checks the OFFER and whether the requested PERFORMANCE (e.g. for dynamic quotas)
59
     * is actually available. This turns the OFFER into a real PERFORMANCE, the sale of which is guaranteed by NOVA.
60
     * If desired, NOVA OFFER needs further information (e.g. about the customer) for processing.
61
     *
62
     * Service: https://confluence-ext.sbb.ch/display/NOVAUG/offeriereLeistungen
63
     *
64
     * @param NovaPurchaseServicesParameter $parameter The parameters
65
     *
66
     * @throws Exception if an error occurs
67
     *
68
     * @return NovaPurchaseServicesResult The result data
69
     */
70 3
    public function purchaseService(NovaPurchaseServicesParameter $parameter): NovaPurchaseServicesResult
71
    {
72
        // The SOAP endpoint url
73 3
        $url = $this->novaSoapAction->getNovaSalesServiceUrl();
74
75
        // The SOAP action (http header)
76 3
        $soapAction = $this->novaSoapAction->getSoapAction('vertrieb', 'kaufeLeistungen');
77
78
        // The SOAP content (http body)
79 3
        $body = $this->createRequestBody($parameter);
80
81
        try {
82 3
            $xmlContent = $this->novaSoapAction->invokeSoapRequest($url, $soapAction, $body);
83 3
            $xml = XmlDocument::createFromXmlString($xmlContent);
84
85 3
            return $this->createResult($xml);
86
        } catch (Exception $exception) {
87
            throw $this->novaErrorParser->createGeneralException($exception);
88
        }
89
    }
90
91
    /**
92
     * Create SOAP body XML content.
93
     *
94
     * @param NovaPurchaseServicesParameter $parameter The parameters
95
     *
96
     * @return string The xml content
97
     */
98 3
    private function createRequestBody(NovaPurchaseServicesParameter $parameter): string
99
    {
100 3
        $dom = new DOMDocument('1.0', 'utf-8');
101 3
        $dom->formatOutput = true;
102
103 3
        $dom->appendChild($dom->createComment(' powered by Barakuda '));
104
105 3
        $envelope = $dom->createElement('soapenv:Envelope');
106 3
        $dom->appendChild($envelope);
107 3
        $envelope->setAttribute('xmlns:soapenv', 'http://schemas.xmlsoap.org/soap/envelope/');
108
109 3
        $soapHeader = $dom->createElement('soapenv:Header');
110 3
        $envelope->appendChild($soapHeader);
111
112 3
        $body = $dom->createElement('soapenv:Body');
113 3
        $envelope->appendChild($body);
114
115 3
        $method = $dom->createElement('ns18:kaufeLeistungen');
116 3
        $body->appendChild($method);
117
118 3
        $this->novaSoapAction->appendMethodNamespaces($method);
119
120 3
        $methodRequest = $dom->createElement('ns18:kaufRequest');
121 3
        $method->appendChild($methodRequest);
122
123 3
        $methodRequest->setAttribute('ns18:transaktionsVerhalten', 'ROLLBACK_ON_ERROR');
124 3
        $methodRequest->setAttribute('ns18:fachlogLevel', 'OFF');
125
126 3
        $this->novaSoapAction->appendDomClientIdentifier($dom, $methodRequest, $parameter, 'ns18:');
127 3
        $this->novaSoapAction->appendDomCorrelationContext($dom, $methodRequest, $parameter, 'ns18:');
128
129 3
        $serviceRequest = $dom->createElement('ns18:leistungsKaufRequest');
130 3
        $methodRequest->appendChild($serviceRequest);
131
132 3
        $serviceRequest->setAttribute('ns18:leistungsId', $parameter->novaServiceId);
133
134 3
        $paymentInformation = $dom->createElement('ns18:zahlungsInformation');
135 3
        $serviceRequest->appendChild($paymentInformation);
136
137 3
        $paymentInformation->setAttribute('ns18:zahlungsArtCode', $parameter->paymentTypeCode);
138 3
        $paymentInformation->setAttribute('ns18:externeZahlungsReferenz', '');
139
140 3
        $amount = $dom->createElement('ns18:geldBetrag');
141 3
        $paymentInformation->appendChild($amount);
142
143 3
        $amount->setAttribute('base:betrag', (string)$parameter->price);
144 3
        $amount->setAttribute('base:waehrung', $parameter->currency);
145
146 3
        return (string)$dom->saveXML();
147
    }
148
149
    /**
150
     * Create result object.
151
     *
152
     * @param XmlDocument $xml The xml document
153
     *
154
     * @throws DomainException
155
     *
156
     * @return NovaPurchaseServicesResult The mapped result
157
     */
158 3
    private function createResult(XmlDocument $xml): NovaPurchaseServicesResult
159
    {
160 3
        $result = new NovaPurchaseServicesResult();
161
162 3
        $xml = $xml->withoutNamespaces();
163
164
        // Find and append all messages
165 3
        foreach ($this->novaMessageParser->findNovaMessages($xml) as $message) {
166
            $result->addMessage($message);
167
        }
168
169
        // Root node
170 3
        $responseNode = $xml->queryFirstNode('/Envelope/Body/kaufeLeistungenResponse');
171 3
        $serviceNodes = $xml->queryNodes('kaufResponse/leistung', $responseNode);
172
173
        /** @var DOMElement $serviceNode */
174 3
        foreach ($serviceNodes as $serviceNode) {
175 3
            $serviceItem = new NovaServiceItem();
176
177 3
            $serviceId = $serviceNode->getAttribute('leistungsId');
178
179 3
            if (empty($serviceId)) {
180
                throw new DomainException('SBB-NOVA service ID not found');
181
            }
182
183 3
            $serviceItem->serviceId = $serviceId;
184 3
            $serviceItem->serviceReference = $serviceNode->getAttribute('leistungsReferenz');
185 3
            $serviceItem->serviceStatus = $serviceNode->getAttribute('leistungsStatus');
186 3
            $serviceItem->productNumber = $serviceNode->getAttribute('produktNummer');
187 3
            $serviceItem->tkId = $xml->findNodeValue('//tkid', $serviceNode);
188 3
            $serviceItem->price = $xml->getAttributeValue('verkaufsPreis/geldBetrag/@betrag', $serviceNode);
189 3
            $serviceItem->currency = $xml->getAttributeValue('verkaufsPreis/geldBetrag/@waehrung', $serviceNode);
190 3
            $serviceItem->vatAmount = $xml->getAttributeValue('verkaufsPreis/mwstAnteil/@betrag', $serviceNode);
191 3
            $serviceItem->vatPercent = $xml->getAttributeValue('verkaufsPreis/mwstAnteil/@mwstSatz', $serviceNode);
192
193 3
            $result->services[] = $serviceItem;
194
        }
195
196 3
        return $result;
197
    }
198
}
199