Passed
Push — master ( fa46b1...3c911a )
by Daniel
03:40 queued 11s
created

NovaSavCreateOffersMethod::createRequestBody()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 38
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 24
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 38
ccs 25
cts 25
cp 1
crap 1
rs 9.536
1
<?php
2
3
namespace OrcaServices\NovaApi\Method;
4
5
use DomainException;
6
use DOMDocument;
7
use DOMElement;
8
use Exception;
9
use InvalidArgumentException;
10
use OrcaServices\NovaApi\Parameter\NovaSavCreateOffersParameter;
11
use OrcaServices\NovaApi\Parser\NovaApiErrorParser;
12
use OrcaServices\NovaApi\Parser\NovaMessageParser;
13
use OrcaServices\NovaApi\Result\NovaSavCreateOffersResult;
14
use OrcaServices\NovaApi\Result\NovaSavOffer;
15
use OrcaServices\NovaApi\Soap\NovaApiSoapAction;
16
use OrcaServices\NovaApi\Xml\XmlDocument;
17
18
/**
19
 * SOAP method.
20
 */
21
final class NovaSavCreateOffersMethod implements NovaMethod
22
{
23
    /**
24
     * @var NovaApiSoapAction
25
     */
26
    private $novaSoapAction;
27
28
    /**
29
     * @var NovaApiErrorParser
30
     */
31
    private $novaErrorParser;
32
33
    /**
34
     * @var NovaMessageParser
35
     */
36
    private $novaMessageParser;
37
38
    /**
39
     * NovaSearchPartnerMethod constructor.
40
     *
41
     * @param NovaApiSoapAction $novaSoapAction The novaSoapAction
42
     * @param NovaApiErrorParser $novaErrorParser The novaErrorParser
43
     * @param NovaMessageParser $novaMessageParser The message parser
44
     */
45 1
    public function __construct(
46
        NovaApiSoapAction $novaSoapAction,
47
        NovaApiErrorParser $novaErrorParser,
48
        NovaMessageParser $novaMessageParser
49
    ) {
50 1
        $this->novaSoapAction = $novaSoapAction;
51 1
        $this->novaErrorParser = $novaErrorParser;
52 1
        $this->novaMessageParser = $novaMessageParser;
53 1
    }
54
55
    /**
56
     * Create SAV offers.
57
     *
58
     * https://confluence.sbb.ch/display/NOVAUG/erstelleSAVAngebote
59
     *
60
     * @param NovaSavCreateOffersParameter $parameter The parameter
61
     *
62
     * @throws Exception If an error occurs
63
     *
64
     * @return NovaSavCreateOffersResult List of SAV offers
65
     */
66 1
    public function createSavOffers(NovaSavCreateOffersParameter $parameter): NovaSavCreateOffersResult
67
    {
68
        // The SOAP endpoint url
69 1
        $url = $this->novaSoapAction->getNovaSalesServiceUrl();
70
71
        // The SOAP action (http header)
72 1
        $soapAction = $this->novaSoapAction->getSoapAction('vertrieb', 'erstelleSAVAngebote');
73
74
        // The SOAP content (http body)
75 1
        $body = $this->createRequestBody($parameter);
76
77
        try {
78 1
            $xmlContent = $this->novaSoapAction->invokeSoapRequest($url, $soapAction, $body);
79 1
            $xml = XmlDocument::createFromXmlString($xmlContent);
80
81 1
            return $this->createResult($xml);
82
        } catch (Exception $exception) {
83
            throw $this->novaErrorParser->createGeneralException($exception);
84
        }
85
    }
86
87
    /**
88
     * Create SOAP body XML content.
89
     *
90
     * @param NovaSavCreateOffersParameter $parameter The parameters
91
     *
92
     * @throws InvalidArgumentException
93
     *
94
     * @return string The xml content
95
     */
96 1
    private function createRequestBody(NovaSavCreateOffersParameter $parameter): string
97
    {
98 1
        $dom = new DOMDocument('1.0', 'utf-8');
99 1
        $dom->formatOutput = true;
100
101 1
        $dom->appendChild($dom->createComment(' powered by Barakuda '));
102
103 1
        $envelope = $dom->createElement('soapenv:Envelope');
104 1
        $dom->appendChild($envelope);
105 1
        $envelope->setAttribute('xmlns:soapenv', 'http://schemas.xmlsoap.org/soap/envelope/');
106
107 1
        $soapHeader = $dom->createElement('soapenv:Header');
108 1
        $envelope->appendChild($soapHeader);
109
110 1
        $body = $dom->createElement('soapenv:Body');
111 1
        $envelope->appendChild($body);
112
113 1
        $method = $dom->createElement('ns21:erstelleSAVAngebote');
114 1
        $body->appendChild($method);
115 1
        $this->appendMethodNamespaces($method);
116
117 1
        $methodRequest = $dom->createElement('ns21:savRequest');
118 1
        $method->appendChild($methodRequest);
119
120 1
        $methodRequest->setAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
121 1
        $methodRequest->setAttribute('xsi:type', 'ns21:ErstattungsAngebotsRequest');
122 1
        $methodRequest->setAttribute('ns21:fachlogLevel', 'OFF');
123
124 1
        $this->novaSoapAction->appendDomClientIdentifier($dom, $methodRequest, $parameter, 'ns21:');
125 1
        $this->novaSoapAction->appendDomCorrelationContext($dom, $methodRequest, $parameter, 'ns21:');
126
127 1
        $refundService = $dom->createElement('ns21:zuErstattendeLeistung');
128 1
        $methodRequest->appendChild($refundService);
129
130 1
        $refundService->setAttribute('ns21:leistungsId', $parameter->serviceId);
131
        // $refundService->setAttribute('ns21:erstattungsGrund', $parameter->reason);
132
133 1
        return (string)$dom->saveXML();
134
    }
135
136
    /**
137
     * Create result object.
138
     *
139
     * @param XmlDocument $xml The xml document
140
     *
141
     * @throws DomainException
142
     *
143
     * @return NovaSavCreateOffersResult The mapped result
144
     */
145 1
    private function createResult(XmlDocument $xml): NovaSavCreateOffersResult
146
    {
147 1
        $result = new NovaSavCreateOffersResult();
148
149 1
        $xml = $xml->withoutNamespaces();
150
151
        // Find and append all messages
152 1
        foreach ($this->novaMessageParser->findNovaMessages($xml) as $message) {
153
            $result->addMessage($message);
154
        }
155
156
        // Root node
157 1
        $responseNode = $xml->queryFirstNode('/Envelope/Body/erstelleSAVAngeboteResponse');
158 1
        $savOfferNodes = $xml->queryNodes('angebotsResponse/angebote/angebot', $responseNode);
159
160
        /** @var DOMElement $savOfferNode */
161 1
        foreach ($savOfferNodes as $savOfferNode) {
162 1
            $offer = new NovaSavOffer();
163
164 1
            $offerId = $savOfferNode->getAttribute('angebotsId');
165
166 1
            if (empty($offerId)) {
167
                throw new DomainException('SBB-NOVA SAV offer ID not found');
168
            }
169
170 1
            $offer->novaOfferId = $offerId;
171 1
            $offer->tkId = $xml->findNodeValue('//tkid', $savOfferNode);
172
173 1
            $result->offers[] = $offer;
174
        }
175
176 1
        return $result;
177
    }
178
179
    /**
180
     * Append default SOAP method namespaces.
181
     *
182
     * @param DOMElement $method The method node
183
     *
184
     * @return void
185
     */
186 1
    private function appendMethodNamespaces(DOMElement $method)
187
    {
188 1
        $method->setAttribute('xmlns:ns22', 'http://nova.voev.ch/services/internal/leistungnotification');
189 1
        $method->setAttribute('xmlns:ns21', $this->novaSoapAction->getServiceNamespace('vertrieb'));
190 1
        $method->setAttribute('xmlns:nova-leistungnotiz', $this->novaSoapAction->getServiceNamespace('leistungnotiz'));
191 1
        $method->setAttribute('xmlns:ns19', $this->novaSoapAction->getServiceNamespace('vertrag'));
192 1
        $method->setAttribute('xmlns:vertriebsbase', $this->novaSoapAction->getServiceNamespace('vertriebsbase'));
193 1
        $method->setAttribute('xmlns:ns17', $this->novaSoapAction->getServiceNamespace('internal'));
194 1
        $method->setAttribute('xmlns:vs', $this->novaSoapAction->getServiceNamespace('vertrieb/vertriebsstammdaten'));
195 1
        $method->setAttribute('xmlns:nova-protokoll', $this->novaSoapAction->getServiceNamespace('vertrieb/protokoll'));
196 1
        $method->setAttribute(
197 1
            'xmlns:nova-erneuerungsinfo',
198 1
            $this->novaSoapAction->getServiceNamespace('vertrieb/erneuerungsinfo')
199
        );
200 1
        $method->setAttribute('xmlns:ns13', $this->novaSoapAction->getServiceNamespace('kuko'));
201 1
        $method->setAttribute(
202 1
            'xmlns:offlinemanagement',
203 1
            $this->novaSoapAction->getServiceNamespace('vertrieb/offlinemanagement')
204
        );
205 1
        $method->setAttribute(
206 1
            'xmlns:nova-monitoring',
207 1
            $this->novaSoapAction->getServiceNamespace('internal/monitoring')
208
        );
209 1
        $method->setAttribute('xmlns:nova-preisauskunft', $this->novaSoapAction->getServiceNamespace('preisauskunft'));
210 1
        $method->setAttribute('xmlns:base', $this->novaSoapAction->getServiceNamespace('base'));
211 1
        $method->setAttribute('xmlns:ns8', $this->novaSoapAction->getServiceNamespace('fachlichervertrag'));
212 1
        $method->setAttribute('xmlns:inkasso', $this->novaSoapAction->getServiceNamespace('inkasso'));
213 1
        $method->setAttribute('xmlns:nova-vertragskonto', $this->novaSoapAction->getServiceNamespace('vertragskonto'));
214 1
        $method->setAttribute('xmlns:novasp-swisspass', $this->novaSoapAction->getServiceNamespace('swisspass'));
215 1
        $method->setAttribute('xmlns:novakuko', $this->novaSoapAction->getServiceNamespace('kundeninteraktion'));
216 1
        $method->setAttribute('xmlns:novagp', $this->novaSoapAction->getServiceNamespace('geschaeftspartner'));
217 1
        $method->setAttribute('xmlns', $this->novaSoapAction->getServiceNamespace('vertrieb'));
218 1
        $method->setAttribute('xmlns:novavt-vertrag', $this->novaSoapAction->getServiceNamespace('vertragbase'));
219 1
    }
220
}
221