Base   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 150
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 7
dl 0
loc 150
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
B loadClientParams() 0 36 6
A loadSessionHandler() 0 10 2
A loadRequestCreator() 0 16 2
A loadResponseHandler() 0 10 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
    protected function loadClientParams(Params $params, $receivedFromIdentifier, $version)
86
    {
87
        if ($params->authParams instanceof Params\AuthParams) {
88
            $this->authParams = $params->authParams;
89
            if (isset($params->sessionHandlerParams) &&
90
                $params->sessionHandlerParams instanceof Params\SessionHandlerParams
91
            ) {
92
                $params->sessionHandlerParams->authParams = $this->authParams;
93
            }
94
        } elseif (isset($params->sessionHandlerParams) &&
95
            $params->sessionHandlerParams->authParams instanceof Params\AuthParams
96
        ) {
97
            //@deprecated - Provide backwards compatibility with old authparams structure.
98
            //Github Issue 40 - retrieve AuthParams from sessionhandlerparams if not generally available
99
            $this->authParams = $params->sessionHandlerParams->authParams;
100
        }
101
102
        $this->sessionHandler = $this->loadSessionHandler(
103
            $params->sessionHandler,
104
            $params->sessionHandlerParams
105
        );
106
107
        $this->requestCreator = $this->loadRequestCreator(
108
            $params->requestCreator,
109
            $params->requestCreatorParams,
110
            $receivedFromIdentifier."-".$version,
111
            $this->sessionHandler->getOriginatorOffice(),
112
            $this->sessionHandler->getMessagesAndVersions()
113
        );
114
115
        $this->responseHandler = $this->loadResponseHandler(
116
            $params->responseHandler
117
        );
118
119
        $this->returnResultXml = $params->returnXml;
120
    }
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
    protected function loadSessionHandler($sessionHandler, $params)
132
    {
133
        if ($sessionHandler instanceof HandlerInterface) {
134
            $newSessionHandler = $sessionHandler;
135
        } else {
136
            $newSessionHandler = HandlerFactory::createHandler($params);
137
        }
138
139
        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
    protected function loadRequestCreator($requestCreator, $params, $libIdentifier, $originatorOffice, $mesVer)
156
    {
157
        if ($requestCreator instanceof RequestCreatorInterface) {
158
            $newRequestCreator = $requestCreator;
159
        } else {
160
            $params->originatorOfficeId = $originatorOffice;
161
            $params->messagesAndVersions = $mesVer;
162
163
            $newRequestCreator = RequestCreatorFactory::createRequestCreator(
164
                $params,
165
                $libIdentifier
166
            );
167
        }
168
169
        return $newRequestCreator;
170
    }
171
172
    /**
173
     * Load a response handler
174
     *
175
     * @param ResponseHandlerInterface|null $responseHandler
176
     * @return ResponseHandlerInterface
177
     * @throws \RuntimeException
178
     */
179
    protected function loadResponseHandler($responseHandler)
180
    {
181
        if ($responseHandler instanceof ResponseHandlerInterface) {
182
            $newResponseHandler = $responseHandler;
183
        } else {
184
            $newResponseHandler = new ResponseHandlerBase();
185
        }
186
187
        return $newResponseHandler;
188
    }
189
}
190