Completed
Push — master ( dcb951...0b0e41 )
by Dieter
07:13
created

SoapHeader2::isStateful()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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
    protected function prepareForNextMessage($messageName, $messageOptions)
66
    {
67
        if (!$this->isAuthenticated && $messageName !== 'Security_Authenticate') {
68
            throw new InvalidSessionException('No active session');
69
        }
70
71
        $this->getSoapClient($messageName)->__setSoapHeaders(null);
72
73
        if ($this->isAuthenticated === true && is_int($this->sessionData['sequenceNumber'])) {
74
            $this->sessionData['sequenceNumber']++;
75
76
            $session = new Client\Struct\HeaderV2\Session(
77
                $this->sessionData['sessionId'],
78
                $this->sessionData['sequenceNumber'],
79
                $this->sessionData['securityToken']
80
            );
81
82
            $this->getSoapClient($messageName)->__setSoapHeaders(
83
                new \SoapHeader(self::CORE_WS_V2_SESSION_NS, self::NODENAME_SESSION, $session)
84
            );
85
        }
86
    }
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
    protected function handlePostMessage($messageName, $lastResponse, $messageOptions, $result)
99
    {
100
        if ($messageName === "Security_Authenticate") {
101
            $this->sessionData = $this->getSessionDataFromHeader($lastResponse);
102
            $this->isAuthenticated = (!empty($this->sessionData['sessionId']) &&
103
                !empty($this->sessionData['sequenceNumber']) &&
104
                !empty($this->sessionData['securityToken']));
105
        }
106
    }
107
108
    /**
109
     * @param string $responseMsg the full response XML received.
110
     * @return array
111
     */
112
    protected function getSessionDataFromHeader($responseMsg)
113
    {
114
        $newSessionData = [
115
            'sessionId' => null,
116
            'sequenceNumber' => null,
117
            'securityToken' => null
118
        ];
119
120
        $responseDomDoc = new \DOMDocument('1.0', 'UTF-8');
121
        $ok = $responseDomDoc->loadXML($responseMsg);
122
123
        if ($ok) {
124
            $sessionId = $responseDomDoc->getElementsByTagName(self::NODENAME_SESSIONID)->item(0)->nodeValue;
125
            if ($sessionId) {
126
                $newSessionData['sessionId'] = $sessionId;
127
            }
128
            $sequence = (int)$responseDomDoc->getElementsByTagName(self::NODENAME_SEQENCENR)->item(0)->nodeValue;
129
            if ($sequence) {
130
                $newSessionData['sequenceNumber'] = $sequence;
131
            }
132
            $securityToken = $responseDomDoc->getElementsByTagName(self::NODENAME_SECURITYTOKEN)->item(0)->nodeValue;
133
            if ($securityToken) {
134
                $newSessionData['securityToken'] = $securityToken;
135
            }
136
        }
137
138
        unset($responseDomDoc);
139
140
        return $newSessionData;
141
    }
142
143
    /**
144
     * Cannot set stateless on Soap Header v2
145
     *
146
     * @param bool $stateful
147
     * @throws UnsupportedOperationException
148
     */
149
    public function setStateful($stateful)
150
    {
151
        if ($stateful === false) {
152
            throw new UnsupportedOperationException('Stateful messages are mandatory on SoapHeader 2');
153
        }
154
    }
155
156
    /**
157
     * Soap Header 2 sessions are always stateful
158
     *
159
     * @return bool
160
     */
161
    public function isStateful()
162
    {
163
        return true;
164
    }
165
166
    /**
167
     * Make SoapClient options for Soap Header 2 handler
168
     *
169
     * @return array
170
     */
171 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...
172
    {
173
        $options = $this->soapClientOptions;
174
        $options['classmap'] = array_merge(Classmap::$soapheader2map, Classmap::$map);
175
176
        if (!empty($this->params->soapClientOptions)) {
177
            $options = array_merge($options, $this->params->soapClientOptions);
178
        }
179
180
        return $options;
181
    }
182
}
183