Base::loadSessionHandler()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 10
cc 2
nc 2
nop 2
crap 2
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 Amadeus\Client\RequestCreator\RequestCreatorInterface;
26
use Amadeus\Client\ResponseHandler\ResponseHandlerInterface;
27
use Amadeus\Client\Session\Handler\HandlerFactory;
28
use Amadeus\Client\Session\Handler\HandlerInterface;
29
use Amadeus\Client\RequestCreator\Factory as RequestCreatorFactory;
30
use Amadeus\Client\ResponseHandler\Base as ResponseHandlerBase;
31
32
/**
33
 * Base Client
34
 *
35
 * Responsible for loading constructor params etc.
36
 *
37
 * @package Amadeus\Client
38
 * @author Dieter Devlieghere <[email protected]>
39
 */
40
class Base
41
{
42
    /**
43
     * Session Handler will be sending all the messages and handling all session-related things.
44
     *
45
     * @var HandlerInterface
46
     */
47
    protected $sessionHandler;
48
49
    /**
50
     * Request Creator is will create the correct message structure to send to the SOAP server.
51
     *
52
     * @var RequestCreatorInterface
53
     */
54
    protected $requestCreator;
55
56
    /**
57
     * Response Handler will check the received response for errors.
58
     *
59
     * @var ResponseHandlerInterface
60
     */
61
    protected $responseHandler;
62
63
    /**
64
     * Authentication parameters
65
     *
66
     * @var Params\AuthParams
67
     */
68
    protected $authParams;
69
70
    /**
71
     * Whether to return the response of a message as XML as well as PHP object
72
     *
73
     * @var bool
74
     */
75
    protected $returnResultXml;
76
77
78
    /**
79
     * Loads Client parameters
80
     *
81
     * @param Params $params
82
     * @param string $receivedFromIdentifier
83
     * @param string $version
84
     */
85 500
    protected function loadClientParams(Params $params, $receivedFromIdentifier, $version)
86
    {
87 500
        if ($params->authParams instanceof Params\AuthParams) {
0 ignored issues
show
introduced by
$params->authParams is always a sub-type of Amadeus\Client\Params\AuthParams.
Loading history...
88 10
            $this->authParams = $params->authParams;
89 10
            if (isset($params->sessionHandlerParams) &&
90 8
                $params->sessionHandlerParams instanceof Params\SessionHandlerParams
91 4
            ) {
92 8
                $params->sessionHandlerParams->authParams = $this->authParams;
93 2
            }
94 494
        } elseif (isset($params->sessionHandlerParams) &&
95 318
            $params->sessionHandlerParams->authParams instanceof Params\AuthParams
96 196
        ) {
97
            //@deprecated - Provide backwards compatibility with old authparams structure.
98
            //Github Issue 40 - retrieve AuthParams from sessionhandlerparams if not generally available
99 60
            $this->authParams = $params->sessionHandlerParams->authParams;
100 24
        }
101
102 500
        $this->sessionHandler = $this->loadSessionHandler(
103 500
            $params->sessionHandler,
104 500
            $params->sessionHandlerParams
105 200
        );
106
107 495
        $this->requestCreator = $this->loadRequestCreator(
108 495
            $params->requestCreator,
109 495
            $params->requestCreatorParams,
110 495
            $receivedFromIdentifier."-".$version,
111 495
            $this->sessionHandler->getOriginatorOffice(),
112 495
            $this->sessionHandler->getMessagesAndVersions()
113 198
        );
114
115 495
        $this->responseHandler = $this->loadResponseHandler(
116 495
            $params->responseHandler
117 198
        );
118
119 495
        $this->returnResultXml = $params->returnXml;
120 495
    }
121
122
    /**
123
     * Load the session handler
124
     *
125
     * Either load the provided session handler or create one depending on incoming parameters.
126
     *
127
     * @param HandlerInterface|null $sessionHandler
128
     * @param Params\SessionHandlerParams|null $params
129
     * @return HandlerInterface
130
     */
131 500
    protected function loadSessionHandler($sessionHandler, $params)
132
    {
133 500
        if ($sessionHandler instanceof HandlerInterface) {
134 435
            $newSessionHandler = $sessionHandler;
135 174
        } else {
136 65
            $newSessionHandler = HandlerFactory::createHandler($params);
137
        }
138
139 495
        return $newSessionHandler;
140
    }
141
142
    /**
143
     * Load a request creator
144
     *
145
     * A request creator is responsible for generating the correct request to send.
146
     *
147
     * @param RequestCreatorInterface|null $requestCreator
148
     * @param Params\RequestCreatorParams $params
149
     * @param string $libIdentifier Library identifier & version string (for Received From)
150
     * @param string $originatorOffice The Office we are signed in with.
151
     * @param array $mesVer Messages & Versions array of active messages in the WSDL
152
     * @return RequestCreatorInterface
153
     * @throws \RuntimeException
154
     */
155 495
    protected function loadRequestCreator($requestCreator, $params, $libIdentifier, $originatorOffice, $mesVer)
156
    {
157 495
        if ($requestCreator instanceof RequestCreatorInterface) {
158 5
            $newRequestCreator = $requestCreator;
159 2
        } else {
160 490
            $params->originatorOfficeId = $originatorOffice;
161 490
            $params->messagesAndVersions = $mesVer;
162
163 490
            $newRequestCreator = RequestCreatorFactory::createRequestCreator(
164 490
                $params,
165 196
                $libIdentifier
166 196
            );
167
        }
168
169 495
        return $newRequestCreator;
170
    }
171
172
    /**
173
     * Load a response handler
174
     *
175
     * @param ResponseHandlerInterface|null $responseHandler
176
     * @return ResponseHandlerInterface
177
     * @throws \RuntimeException
178
     */
179 495
    protected function loadResponseHandler($responseHandler)
180
    {
181 495
        if ($responseHandler instanceof ResponseHandlerInterface) {
182 385
            $newResponseHandler = $responseHandler;
183 154
        } else {
184 110
            $newResponseHandler = new ResponseHandlerBase();
185
        }
186
187 495
        return $newResponseHandler;
188
    }
189
}
190