SoapClient::transformIncomingRequest()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 35
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 3.2535

Importance

Changes 0
Metric Value
eloc 23
c 0
b 0
f 0
dl 0
loc 35
ccs 16
cts 23
cp 0.6957
rs 9.552
cc 3
nc 3
nop 1
crap 3.2535
1
<?php
2
/**
3
 * amadeus-ws-client
4
 *
5
 * Copyright 2015 Amadeus Benelux NV
6
 *
7
 * Licensed under the Apache License, Version 2.0 (the "License");
8
 * you may not use this file except in compliance with the License.
9
 * You may obtain a copy of the License at
10
 *
11
 * http://www.apache.org/licenses/LICENSE-2.0
12
 *
13
 * Unless required by applicable law or agreed to in writing, software
14
 * distributed under the License is distributed on an "AS IS" BASIS,
15
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
 * See the License for the specific language governing permissions and
17
 * limitations under the License.
18
 *
19
 * @package Amadeus
20
 * @license https://opensource.org/licenses/Apache-2.0 Apache 2.0
21
 */
22
23
namespace Amadeus\Client;
24
25
use Psr\Log;
26
27
/**
28
 * Amadeus Web Services SoapClient class
29
 *
30
 * This SoapClient will perform an XSLT transformation on the request to be sent to the amadeus server:
31
 * it will remove any empty XML tags except for some specific tags that may be required to be present and empty.
32
 *
33
 * If you do not remove empty XML tags, you will get SoapFaults back from the server when unexpected empty elements
34
 * are present in your request.
35
 *
36
 * @package Amadeus\Client
37
 * @author Dieter Devlieghere <[email protected]>
38
 */
39
class SoapClient extends \SoapClient implements Log\LoggerAwareInterface
40
{
41
    use Log\LoggerAwareTrait;
42
43
    const REMOVE_EMPTY_XSLT_LOCATION = 'SoapClient/removeempty.xslt';
44
45
    /**
46
     * Construct a new SoapClient
47
     *
48
     * @param string $wsdl Location of WSDL file
49
     * @param array $options initialisation options
50
     * @param Log\LoggerInterface|null $logger Error logging object
51
     */
52 25
    public function __construct($wsdl, $options, ?Log\LoggerInterface $logger = null)
53
    {
54 25
        if (!($logger instanceof Log\LoggerInterface)) {
55 15
            $logger = new Log\NullLogger();
56 6
        }
57 25
        $this->setLogger($logger);
58
59 25
        parent::__construct($wsdl, $options);
60 25
    }
61
62
    /**
63
     * __doRequest override of SoapClient
64
     *
65
     * @param string $request The XML SOAP request.
66
     * @param string $location The URL to request.
67
     * @param string $action The SOAP action.
68
     * @param int $version The SOAP version.
69
     * @param int|null $oneWay
70
     * @uses parent::__doRequest
71
     * @return string The XML SOAP response.
72
     * @throws Exception When PHP XSL extension is not enabled or WSDL file isn't readable.
73
     */
74
    #[\ReturnTypeWillChange]
75
    public function __doRequest($request, $location, $action, $version, $oneWay = null)
76
    {
77
        if (!extension_loaded('xsl')) {
78
            throw new Exception('PHP XSL extension is not enabled.');
79
        }
80
81
        $newRequest = $this->transformIncomingRequest($request);
82
83
        return parent::__doRequest($newRequest, $location, $action, $version, $oneWay);
84
    }
85
86
    /**
87
     * @param string $request
88
     * @return string
89
     * @throws Exception when XSLT file isn't readable
90 15
     */
91
    protected function transformIncomingRequest($request)
92 15
    {
93
        $xsltFile = __DIR__ .DIRECTORY_SEPARATOR.self::REMOVE_EMPTY_XSLT_LOCATION;
94 15
        if (!is_readable($xsltFile)) {
95 15
            throw new Exception('XSLT file "'.$xsltFile.'" is not readable!');
96
        }
97
98
        $dom = new \DOMDocument('1.0', 'UTF-8');
99 15
        $dom->loadXML($request);
100 15
        $xslt = new \DOMDocument('1.0', 'UTF-8');
101 15
102
        $xslt->load($xsltFile);
103 15
104
        $processor = new \XSLTProcessor();
105 15
        $processor->importStylesheet($xslt);
106 15
        $transform = $processor->transformToXml($dom);
107 15
        if ($transform === false) {
108 15
            //On transform error: usually when modifying the XSLT transformation incorrectly...
109
            $this->logger?->log(
110
                Log\LogLevel::ERROR,
111
                __METHOD__."__doRequest(): XSLTProcessor::transformToXml "
112
                . "returned FALSE: could not perform transformation!!"
113
            );
114
            $newRequest = $request;
115
        } else {
116
            $newDom = new \DOMDocument('1.0', 'UTF-8');
117 15
            $newDom->preserveWhiteSpace = false;
118 15
            $newDom->loadXML($transform);
119 15
120
            $newRequest = $newDom->saveXML();
121 15
        }
122
123
        unset($processor, $xslt, $dom, $transform);
124 15
125
        return $newRequest;
126 15
    }
127
}
128