Completed
Pull Request — master (#189)
by Dieter
10:43
created

SoapHeader2::handlePostMessage()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 8
cts 8
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 6
nc 4
nop 4
crap 4
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\Session\Handler;
24
25
use Amadeus\Client;
26
27
/**
28
 * SoapHeader2: Session Handler for web service applications using Amadeus WS Soap Header v2.
29
 *
30
 * @package Amadeus\Client\Session\Handler
31
 * @author Dieter Devlieghere <[email protected]>
32
 */
33
class SoapHeader2 extends Base
34
{
35
    /**
36
     * Namespace of SoapHeader V2
37
     */
38
    const CORE_WS_V2_SESSION_NS = 'http://xml.amadeus.com/ws/2009/01/WBS_Session-2.0.xsd';
39
40
    /**
41
     * Node for Session
42
     */
43
    const NODENAME_SESSION = "Session";
44
45
    /**
46
     * Node for Session ID
47
     */
48
    const NODENAME_SESSIONID = "SessionId";
49
    /**
50
     * Node for Session Sequence Number
51
     */
52
    const NODENAME_SEQENCENR = "SequenceNumber";
53
    /**
54
     * Node for Session Security Token
55
     */
56
    const NODENAME_SECURITYTOKEN = "SecurityToken";
57
58
    /**
59
     * Prepare to send a next message and checks if authenticated
60
     *
61
     * @param string $messageName
62
     * @param array $messageOptions
63
     * @throws InvalidSessionException When trying to send a message without session.
64
     */
65 12
    protected function prepareForNextMessage($messageName, $messageOptions)
66
    {
67 12
        if (!$this->isAuthenticated && $messageName !== 'Security_Authenticate') {
68 4
            throw new InvalidSessionException('No active session');
69
        }
70
71 8
        $this->getSoapClient($messageName)->__setSoapHeaders(null);
72
73 8
        if ($this->isAuthenticated === true && is_int($this->sessionData['sequenceNumber'])) {
74 4
            $this->sessionData['sequenceNumber']++;
75
76 4
            $session = new Client\Struct\HeaderV2\Session(
77 4
                $this->sessionData['sessionId'],
78 4
                $this->sessionData['sequenceNumber'],
79 4
                $this->sessionData['securityToken']
80 2
            );
81
82 4
            $this->getSoapClient($messageName)->__setSoapHeaders(
83 4
                new \SoapHeader(self::CORE_WS_V2_SESSION_NS, self::NODENAME_SESSION, $session)
84 2
            );
85 2
        }
86 8
    }
87
88
    /**
89
     * Handles post message actions
90
     *
91
     * Handles session state based on received response
92
     *
93
     * @param string $messageName
94
     * @param string $lastResponse
95
     * @param array $messageOptions
96
     * @param mixed $result
97
     */
98 8
    protected function handlePostMessage($messageName, $lastResponse, $messageOptions, $result)
99
    {
100 8
        if ($messageName === "Security_Authenticate") {
101 4
            $this->sessionData = $this->getSessionDataFromHeader($lastResponse);
102 4
            $this->isAuthenticated = (!empty($this->sessionData['sessionId']) &&
103 4
                !empty($this->sessionData['sequenceNumber']) &&
104 4
                !empty($this->sessionData['securityToken']));
105 2
        }
106 8
    }
107
108
    /**
109
     * @param string $responseMsg the full response XML received.
110
     * @return array
111
     */
112 4
    protected function getSessionDataFromHeader($responseMsg)
113
    {
114
        $newSessionData = [
115 4
            'sessionId' => null,
116 2
            'sequenceNumber' => null,
117
            'securityToken' => null
118 2
        ];
119
120 4
        $responseDomDoc = new \DOMDocument('1.0', 'UTF-8');
121 4
        $ok = $responseDomDoc->loadXML($responseMsg);
122
123 4
        if ($ok) {
124 4
            $sessionId = $responseDomDoc->getElementsByTagName(self::NODENAME_SESSIONID)->item(0)->nodeValue;
125 4
            if ($sessionId) {
126 4
                $newSessionData['sessionId'] = $sessionId;
127 2
            }
128 4
            $sequence = (int) $responseDomDoc->getElementsByTagName(self::NODENAME_SEQENCENR)->item(0)->nodeValue;
129 4
            if ($sequence) {
130 4
                $newSessionData['sequenceNumber'] = $sequence;
131 2
            }
132 4
            $securityToken = $responseDomDoc->getElementsByTagName(self::NODENAME_SECURITYTOKEN)->item(0)->nodeValue;
133 4
            if ($securityToken) {
134 4
                $newSessionData['securityToken'] = $securityToken;
135 2
            }
136 2
        }
137
138 4
        unset($responseDomDoc);
139
140 4
        return $newSessionData;
141
    }
142
143
    /**
144
     * Cannot set stateless on Soap Header v2
145
     *
146
     * @param bool $stateful
147
     * @throws UnsupportedOperationException
148
     */
149 52
    public function setStateful($stateful)
150
    {
151 52
        if ($stateful === false) {
152 4
            throw new UnsupportedOperationException('Stateful messages are mandatory on SoapHeader 2');
153
        }
154 52
    }
155
156
    /**
157
     * Soap Header 2 sessions are always stateful
158
     *
159
     * @return bool
160
     */
161 4
    public function isStateful()
162
    {
163 4
        return true;
164
    }
165
166
    /**
167
     * Is the TransactionFlowLink header enabled?
168
     *
169
     * @return bool
170
     */
171 4
    public function isTransactionFlowLinkEnabled()
172
    {
173 4
        return false; //Not supported
174
    }
175
176
    /**
177
     * Enable or disable TransactionFlowLink header
178
     *
179
     * @throws UnsupportedOperationException when used on unsupported WSAP versions
180
     * @param bool $enabled
181
     */
182 52
    public function setTransactionFlowLink($enabled)
183
    {
184 52
        if ($enabled === true) {
185 4
            throw new UnsupportedOperationException('TransactionFlowLink header is not supported on SoapHeader 2');
186
        }
187 52
    }
188
189
    /**
190
     * Get the TransactionFlowLink Consumer ID
191
     *
192
     * @return string|null
193
     */
194 4
    public function getConsumerId()
195
    {
196 4
        return null; //Not supported
197
    }
198
199
    /**
200
     * Set the TransactionFlowLink Consumer ID
201
     *
202
     * @throws UnsupportedOperationException when used on unsupported WSAP versions
203
     * @param string $id
204
     * @return void
205
     */
206 52
    public function setConsumerId($id)
207
    {
208 52
        if (!is_null($id)) {
209 4
            throw new UnsupportedOperationException('TransactionFlowLink header is not supported on SoapHeader 2');
210
        }
211 52
    }
212
213
214
    /**
215
     * Make SoapClient options for Soap Header 2 handler
216
     *
217
     * @return array
218
     */
219 8 View Code Duplication
    protected function makeSoapClientOptions()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
220
    {
221 8
        $options = $this->soapClientOptions;
222 8
        $options['classmap'] = array_merge(Classmap::$soapheader2map, Classmap::$map);
223
224 8
        if (!empty($this->params->soapClientOptions)) {
225 4
            $options = array_merge($options, $this->params->soapClientOptions);
226 2
        }
227
228 8
        return $options;
229
    }
230
}
231