Completed
Push — master ( 8f168b...4b1795 )
by Dieter
09:49
created

Base::createOfferCreateOffer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
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\RequestCreator;
24
25
use Amadeus\Client\InvalidMessageException;
26
use Amadeus\Client\Params\RequestCreatorParams;
27
use Amadeus\Client\RequestOptions\CommandCrypticOptions;
28
use Amadeus\Client\RequestOptions\DocIssuanceIssueMiscDocOptions;
29
use Amadeus\Client\RequestOptions\DocIssuanceIssueTicketOptions;
30
use Amadeus\Client\RequestOptions\InfoEncodeDecodeCityOptions;
31
use Amadeus\Client\RequestOptions\MiniRuleGetFromPricingOptions;
32
use Amadeus\Client\RequestOptions\MiniRuleGetFromPricingRecOptions;
33
use Amadeus\Client\RequestOptions\PriceXplorerExtremeSearchOptions;
34
use Amadeus\Client\RequestOptions\RequestOptionsInterface;
35
use Amadeus\Client\RequestOptions\SalesReportsDisplayQueryReportOptions;
36
use Amadeus\Client\RequestOptions\SecurityAuthenticateOptions;
37
use Amadeus\Client\RequestOptions\ServiceIntegratedPricingOptions;
38
use Amadeus\Client\Struct;
39
40
/**
41
 * Base request creator - the default request creator.
42
 *
43
 * In charge of building the request objects that are used by the Soap Client.
44
 * They are built depending on:
45
 * - Which message is being created.
46
 * - Which request options were provided.
47
 * - What message version is active in the current WSDL?
48
 *
49
 * @package Amadeus\Client\RequestCreator
50
 * @author Dieter Devlieghere <[email protected]>
51
 */
52
class Base implements RequestCreatorInterface
53
{
54
    /**
55
     * Parameters
56
     *
57
     * @var RequestCreatorParams
58
     */
59
    protected $params;
60
61
    /**
62
     * Associative array of messages (as keys) and versions (as values) that are present in the WSDL.
63
     *
64
     * @var array
65
     */
66
    protected $messagesAndVersions = [];
67
68
    /**
69
     * List of Request creator builders that have been split off from this class
70
     *
71
     * @var array
72
     */
73
    protected $separateBuilders = [];
74
75
    /**
76
     * Base Request Creator constructor.
77
     *
78
     * @param RequestCreatorParams $params
79
     */
80 83
    public function __construct(RequestCreatorParams $params)
81
    {
82 83
        $this->params = $params;
83 83
        $this->messagesAndVersions = $params->messagesAndVersions;
84
85 83
        $this->separateBuilders = [
86 83
            'fare' => new Fare(),
87 83
            'pnr' => new Pnr($this->params),
88 83
            'air' => new Air(),
89 83
            'queue' => new Queue(),
90 83
            'offer' => new Offer(),
91 83
            'ticket' => new Ticket()
92 83
        ];
93 83
    }
94
95
    /**
96
     * Create a request message for a given message with a set of options.
97
     *
98
     * @param string $messageName the message name as named in the WSDL
99
     * @param RequestOptionsInterface $params
100
     * @throws Struct\InvalidArgumentException When invalid input is detected during message creation.
101
     * @throws InvalidMessageException when trying to create a request for a message that is not in your WSDL.
102
     * @return mixed the created request
103
     */
104 72
    public function createRequest($messageName, RequestOptionsInterface $params)
105
    {
106 72
        $this->checkMessageIsInWsdl($messageName);
107
108 69
        $builder = $this->findBuilderForMessage($messageName);
109
110 69
        $methodName = 'create'.str_replace("_", "", $messageName);
111
112 69
        if (method_exists($builder, $methodName)) {
113 66
            return $builder->$methodName($params, $this->getActiveVersionFor($messageName));
114
        } else {
115 3
            throw new \RuntimeException('Message '.$methodName.' is not implemented in '.__CLASS__);
116
        }
117
    }
118
119
    /**
120
     * Security_SignOut
121
     *
122
     * @return Struct\Security\SignOut
123
     */
124 1
    protected function createSecuritySignOut()
125
    {
126 1
        return new Struct\Security\SignOut();
127
    }
128
129
    /**
130
     * Create request object for Security_Authenticate message
131
     *
132
     * @param SecurityAuthenticateOptions $params
133
     * @return Struct\Security\Authenticate
134
     */
135 1
    protected function createSecurityAuthenticate(SecurityAuthenticateOptions $params)
136
    {
137 1
        return new Struct\Security\Authenticate($params);
138
    }
139
140
    /**
141
     * Command_Cryptic
142
     *
143
     * @param CommandCrypticOptions $params
144
     * @return Struct\Command\Cryptic
145
     */
146 1
    protected function createCommandCryptic(CommandCrypticOptions $params)
147
    {
148 1
        return new Struct\Command\Cryptic($params->entry);
149
    }
150
151
    /**
152
     * Info_EncodeDecodeCity
153
     *
154
     * @param InfoEncodeDecodeCityOptions $params
155
     * @return Struct\Info\EncodeDecodeCity
156
     */
157 1
    protected function createInfoEncodeDecodeCity(InfoEncodeDecodeCityOptions $params)
158
    {
159 1
        return new Struct\Info\EncodeDecodeCity($params);
160
    }
161
162
    /**
163
     * MiniRule_GetFromPricingRec
164
     *
165
     * @param MiniRuleGetFromPricingRecOptions $params
166
     * @return Struct\MiniRule\GetFromPricingRec
167
     */
168 1
    protected function createMiniRuleGetFromPricingRec(MiniRuleGetFromPricingRecOptions $params)
169 1
    {
170 1
        return new Struct\MiniRule\GetFromPricingRec($params);
171
    }
172
173
    /**
174
     * MiniRule_GetFromPricing
175
     *
176
     * @param MiniRuleGetFromPricingOptions $params
177
     * @return Struct\MiniRule\GetFromPricing
178
     */
179 1
    protected function createMiniRuleGetFromPricing(MiniRuleGetFromPricingOptions $params)
180
    {
181 1
        return new Struct\MiniRule\GetFromPricing($params);
182
    }
183
184
    /**
185
     * DocIssuance_IssueTicket
186
     *
187
     * @param DocIssuanceIssueTicketOptions $params
188
     * @return Struct\DocIssuance\IssueTicket
189
     */
190 1
    protected function createDocIssuanceIssueTicket(DocIssuanceIssueTicketOptions $params)
191
    {
192 1
        return new Struct\DocIssuance\IssueTicket($params);
193
    }
194
195
    /**
196
     * DocIssuance_IssueMiscellaneousDocuments
197
     *
198
     * @param DocIssuanceIssueMiscDocOptions $params
199
     * @return Struct\DocIssuance\IssueMiscellaneousDocuments
200
     */
201 1
    protected function createDocIssuanceIssueMiscellaneousDocuments(DocIssuanceIssueMiscDocOptions $params)
202
    {
203 1
        return new Struct\DocIssuance\IssueMiscellaneousDocuments($params);
204
    }
205
206
    /**
207
     * PriceXplorer_ExtremeSearch
208
     *
209
     * @param PriceXplorerExtremeSearchOptions $params
210
     * @return Struct\PriceXplorer\ExtremeSearch
211
     */
212 1
    protected function createPriceXplorerExtremeSearch(PriceXplorerExtremeSearchOptions $params)
213
    {
214 1
        return new Struct\PriceXplorer\ExtremeSearch($params);
215
    }
216
217
    /**
218
     * SalesReports_DisplayQueryReport
219
     *
220
     * @param SalesReportsDisplayQueryReportOptions $params
221
     * @return Struct\SalesReports\DisplayQueryReport
222
     */
223 1
    protected function createSalesReportsDisplayQueryReport(SalesReportsDisplayQueryReportOptions $params)
224
    {
225 1
        return new Struct\SalesReports\DisplayQueryReport($params);
226
    }
227
228
    /**
229
     * Service_IntegratedPricing
230
     *
231
     * @param ServiceIntegratedPricingOptions $params
232
     * @return Struct\Service\IntegratedPricing
233
     */
234 1
    protected function createServiceIntegratedPricing(ServiceIntegratedPricingOptions $params)
235
    {
236 1
        return new Struct\Service\IntegratedPricing($params);
237
    }
238
239
    /**
240
     * Check if a given message is in the active WSDL(s). Throws exception if it isn't.
241
     *
242
     * @throws InvalidMessageException if message is not in loaded WSDL(s).
243
     * @param string $messageName
244
     */
245 72
    protected function checkMessageIsInWsdl($messageName)
246
    {
247 72
        if (!array_key_exists($messageName, $this->messagesAndVersions)) {
248 3
            throw new InvalidMessageException('Message "' . $messageName . '" is not in WDSL');
249
        }
250 69
    }
251
252
    /**
253
     * Get the version number active in the WSDL for the given message
254
     *
255
     * @param string $messageName
256
     * @return float|string
257
     */
258 66
    protected function getActiveVersionFor($messageName)
259
    {
260 66
        return $this->messagesAndVersions[$messageName];
261
    }
262
263
    /**
264
     * Find the correct builder for a given message
265
     *
266
     * Message build methods in all builders must adhere to the
267
     * 'create'<message name without underscores> logic as used in createRequest method.
268
     *
269
     * @param string $messageName
270
     * @return Fare|Pnr|Queue|Base|Air
271
     */
272 69
    protected function findBuilderForMessage($messageName)
273
    {
274 69
        $section = strtolower(substr($messageName, 0, strpos($messageName, '_')));
275
276 69
        if (array_key_exists($section, $this->separateBuilders)) {
277 58
            $builder = $this->separateBuilders[$section];
278 58
        } else {
279 11
            $builder = $this;
280
        }
281
282 69
        return $builder;
283
    }
284
}
285