Completed
Push — master ( 738038...14ebb8 )
by Dieter
11:17
created

Base::checkMessageIsInWsdl()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 1
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\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\OfferConfirmAirOptions;
34
use Amadeus\Client\RequestOptions\OfferConfirmCarOptions;
35
use Amadeus\Client\RequestOptions\OfferConfirmHotelOptions;
36
use Amadeus\Client\RequestOptions\OfferCreateOptions;
37
use Amadeus\Client\RequestOptions\OfferVerifyOptions;
38
use Amadeus\Client\RequestOptions\PriceXplorerExtremeSearchOptions;
39
use Amadeus\Client\RequestOptions\RequestOptionsInterface;
40
use Amadeus\Client\RequestOptions\SalesReportsDisplayQueryReportOptions;
41
use Amadeus\Client\RequestOptions\SecurityAuthenticateOptions;
42
use Amadeus\Client\RequestOptions\TicketCreateTsmFromPricingOptions;
43
use Amadeus\Client\RequestOptions\ServiceIntegratedPricingOptions;
44
use Amadeus\Client\RequestOptions\TicketCreateTstFromPricingOptions;
45
use Amadeus\Client\RequestOptions\TicketDeleteTstOptions;
46
use Amadeus\Client\RequestOptions\TicketDisplayTstOptions;
47
use Amadeus\Client\Struct;
48
49
/**
50
 * Base request creator - the default request creator.
51
 *
52
 * In charge of building the request objects that are used by the Soap Client.
53
 * They are built depending on:
54
 * - Which message is being created.
55
 * - Which request options were provided.
56
 * - What message version is active in the current WSDL?
57
 *
58
 * @package Amadeus\Client\RequestCreator
59
 * @author Dieter Devlieghere <[email protected]>
60
 */
61
class Base implements RequestCreatorInterface
62
{
63
    /**
64
     * Parameters
65
     *
66
     * @var RequestCreatorParams
67
     */
68
    protected $params;
69
70
    /**
71
     * Associative array of messages (as keys) and versions (as values) that are present in the WSDL.
72
     *
73
     * @var array
74
     */
75
    protected $messagesAndVersions = [];
76
77
    /**
78
     * Base Request Creator constructor.
79
     *
80
     * @param RequestCreatorParams $params
81
     */
82 83
    public function __construct(RequestCreatorParams $params)
83
    {
84 83
        $this->params = $params;
85 83
        $this->messagesAndVersions = $params->messagesAndVersions;
86 83
    }
87
88
    /**
89
     * Create a request message for a given message with a set of options.
90
     *
91
     * @param string $messageName the message name as named in the WSDL
92
     * @param RequestOptionsInterface $params
93
     * @throws Struct\InvalidArgumentException When invalid input is detected during message creation.
94
     * @throws InvalidMessageException when trying to create a request for a message that is not in your WSDL.
95
     * @return mixed the created request
96
     */
97 72
    public function createRequest($messageName, RequestOptionsInterface $params)
98
    {
99 72
        $this->checkMessageIsInWsdl($messageName);
100
101 69
        $builder = $this->findBuilderForMessage($messageName);
102
103 69
        $methodName = 'create'.str_replace("_", "", $messageName);
104
105 69
        if (method_exists($builder, $methodName)) {
106 66
            return $builder->$methodName($params, $this->getActiveVersionFor($messageName));
107
        } else {
108 3
            throw new \RuntimeException('Message '.$methodName.' is not implemented in '.__CLASS__);
109
        }
110
    }
111
112
    /**
113
     * Security_SignOut
114
     *
115
     * @return Struct\Security\SignOut
116
     */
117 1
    protected function createSecuritySignOut()
118
    {
119 1
        return new Struct\Security\SignOut();
120
    }
121
122
    /**
123
     * Create request object for Security_Authenticate message
124
     *
125
     * @param SecurityAuthenticateOptions $params
126
     * @return Struct\Security\Authenticate
127
     */
128 1
    protected function createSecurityAuthenticate(SecurityAuthenticateOptions $params)
129
    {
130 1
        return new Struct\Security\Authenticate($params);
131
    }
132
133
    /**
134
     * Offer_VerifyOffer
135
     *
136
     * @param OfferVerifyOptions $params
137
     * @return Struct\Offer\Verify
138
     */
139 4
    protected function createOfferVerifyOffer(OfferVerifyOptions $params)
140
    {
141 4
        $req = new Struct\Offer\Verify(
142 4
            $params->offerReference,
143 4
            $params->segmentName
144 4
        );
145
146 4
        return $req;
147
    }
148
149
    /**
150
     * @param OfferConfirmAirOptions $params
151
     * @return Struct\Offer\ConfirmAir
152
     */
153 1
    protected function createOfferConfirmAirOffer(OfferConfirmAirOptions $params)
154
    {
155 1
        return new Struct\Offer\ConfirmAir($params);
156
    }
157
158
159
    /**
160
     * Offer_ConfirmHotelOffer
161
     *
162
     * @param OfferConfirmHotelOptions $params
163
     * @return Struct\Offer\ConfirmHotel
164
     */
165 1
    protected function createOfferConfirmHotelOffer(OfferConfirmHotelOptions $params)
166
    {
167 1
        return new Struct\Offer\ConfirmHotel($params);
168
    }
169
170
    /**
171
     * @param OfferConfirmCarOptions $params
172
     * @return Struct\Offer\ConfirmCar
173
     */
174 1
    protected function createOfferConfirmCarOffer(OfferConfirmCarOptions $params)
175
    {
176 1
        return new Struct\Offer\ConfirmCar($params);
177
    }
178
179
    /**
180
     * Offer_CreateOffer
181
     *
182
     * Ok, I just realised this function name is a bit confusing. Sorry.
183
     *
184
     * @param OfferCreateOptions $params
185
     * @return Struct\Offer\Create
186
     */
187 1
    protected function createOfferCreateOffer(OfferCreateOptions $params)
188
    {
189 1
        return new Struct\Offer\Create($params);
190
    }
191
192
    /**
193
     * Command_Cryptic
194
     *
195
     * @param CommandCrypticOptions $params
196
     * @return Struct\Command\Cryptic
197
     */
198 1
    protected function createCommandCryptic(CommandCrypticOptions $params)
199
    {
200 1
        return new Struct\Command\Cryptic($params->entry);
201
    }
202
203
    /**
204
     * Info_EncodeDecodeCity
205
     *
206
     * @param InfoEncodeDecodeCityOptions $params
207
     * @return Struct\Info\EncodeDecodeCity
208
     */
209 1
    protected function createInfoEncodeDecodeCity(InfoEncodeDecodeCityOptions $params)
210
    {
211 1
        return new Struct\Info\EncodeDecodeCity($params);
212
    }
213
214
    /**
215
     * MiniRule_GetFromPricingRec
216
     *
217
     * @param MiniRuleGetFromPricingRecOptions $params
218
     * @return Struct\MiniRule\GetFromPricingRec
219
     */
220 1
    protected function createMiniRuleGetFromPricingRec(MiniRuleGetFromPricingRecOptions $params)
221
    {
222 1
        return new Struct\MiniRule\GetFromPricingRec($params);
223
    }
224
225
    /**
226
     * MiniRule_GetFromPricing
227
     *
228
     * @param MiniRuleGetFromPricingOptions $params
229
     * @return Struct\MiniRule\GetFromPricing
230
     */
231 1
    protected function createMiniRuleGetFromPricing(MiniRuleGetFromPricingOptions $params)
232
    {
233 1
        return new Struct\MiniRule\GetFromPricing($params);
234
    }
235
236
    /**
237
     * Ticket_CreateTstFromPricing
238
     *
239
     * @param TicketCreateTstFromPricingOptions $params
240
     * @return Struct\Ticket\CreateTSTFromPricing
241
     */
242 1
    protected function createTicketCreateTSTFromPricing(TicketCreateTstFromPricingOptions $params)
243
    {
244 1
        return new Struct\Ticket\CreateTSTFromPricing($params);
245
    }
246
247
    /**
248
     * Ticket_CreateTSMFromPricing
249
     *
250
     * @param TicketCreateTsmFromPricingOptions $params
251
     * @return Struct\Ticket\CreateTSMFromPricing
252
     */
253 1
    protected function createTicketCreateTSMFromPricing(TicketCreateTsmFromPricingOptions $params)
254
    {
255 1
        return new Struct\Ticket\CreateTSMFromPricing($params);
256
    }
257
258
    /**
259
     * Ticket_DeleteTST
260
     *
261
     * @param TicketDeleteTstOptions $params
262
     * @return Struct\Ticket\DeleteTST
263
     */
264 1
    protected function createTicketDeleteTST(TicketDeleteTstOptions $params)
265
    {
266 1
        return new Struct\Ticket\DeleteTST($params);
267
    }
268
269
    /**
270
     * Ticket_DisplayTST
271
     *
272
     * @param TicketDisplayTstOptions $params
273
     * @return Struct\Ticket\DisplayTST
274
     */
275 1
    protected function createTicketDisplayTST(TicketDisplayTstOptions $params)
276
    {
277 1
        return new Struct\Ticket\DisplayTST($params);
278
    }
279
280
    /**
281
     * DocIssuance_IssueTicket
282
     *
283
     * @param DocIssuanceIssueTicketOptions $params
284
     * @return Struct\DocIssuance\IssueTicket
285
     */
286 1
    protected function createDocIssuanceIssueTicket(DocIssuanceIssueTicketOptions $params)
287
    {
288 1
        return new Struct\DocIssuance\IssueTicket($params);
289
    }
290
291
    /**
292
     * DocIssuance_IssueMiscellaneousDocuments
293
     *
294
     * @param DocIssuanceIssueMiscDocOptions $params
295
     * @return Struct\DocIssuance\IssueTicket
0 ignored issues
show
Documentation introduced by
Should the return type not be Struct\DocIssuance\IssueMiscellaneousDocuments?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
296
     */
297 1
    protected function createDocIssuanceIssueMiscellaneousDocuments(DocIssuanceIssueMiscDocOptions $params)
298
    {
299 1
        return new Struct\DocIssuance\IssueMiscellaneousDocuments($params);
300
    }
301
302
    /**
303
     * PriceXplorer_ExtremeSearch
304
     *
305
     * @param PriceXplorerExtremeSearchOptions $params
306
     * @return Struct\PriceXplorer\ExtremeSearch
307
     */
308 1
    protected function createPriceXplorerExtremeSearch(PriceXplorerExtremeSearchOptions $params)
309
    {
310 1
        return new Struct\PriceXplorer\ExtremeSearch($params);
311
    }
312
313
    /**
314
     * SalesReports_DisplayQueryReport
315
     *
316
     * @param SalesReportsDisplayQueryReportOptions $params
317
     * @return Struct\SalesReports\DisplayQueryReport
318
     */
319 1
    protected function createSalesReportsDisplayQueryReport(SalesReportsDisplayQueryReportOptions $params)
320
    {
321 1
        return new Struct\SalesReports\DisplayQueryReport($params);
322
    }
323
324
    /**
325
     * Service_IntegratedPricing
326
     *
327
     * @param ServiceIntegratedPricingOptions $params
328
     * @return Struct\Service\IntegratedPricing
329
     */
330 1
    protected function createServiceIntegratedPricing(ServiceIntegratedPricingOptions $params)
331
    {
332 1
        return new Struct\Service\IntegratedPricing($params);
333
    }
334
335
    /**
336
     * Check if a given message is in the active WSDL(s). Throws exception if it isn't.
337
     *
338
     * @throws InvalidMessageException if message is not in loaded WSDL(s).
339
     * @param string $messageName
340
     */
341 72
    protected function checkMessageIsInWsdl($messageName)
342
    {
343 72
        if (!array_key_exists($messageName, $this->messagesAndVersions)) {
344 3
            throw new InvalidMessageException('Message "' . $messageName . '" is not in WDSL');
345
        }
346 69
    }
347
348
    /**
349
     * Get the version number active in the WSDL for the given message
350
     *
351
     * @param string $messageName
352
     * @return float|string
353
     */
354 66
    protected function getActiveVersionFor($messageName)
355
    {
356 66
        return $this->messagesAndVersions[$messageName];
357
    }
358
359
    /**
360
     * Find the correct builder for a given message
361
     *
362
     * Message build methods in all builders must adhere to the
363
     * 'create'<message name without underscores> logic as used in createRequest method.
364
     *
365
     * @param string $messageName
366
     * @return Fare|Pnr|Queue|Base|Air
367
     */
368 69
    protected function findBuilderForMessage($messageName)
369
    {
370 69
        $section = strtolower(substr($messageName, 0, strpos($messageName, '_')));
371
372
        switch ($section) {
373 69
            case 'fare':
374 21
                $builder = new Fare();
375 21
                break;
376 48
            case 'pnr':
377 14
                $builder = new Pnr($this->params);
378 14
                break;
379 34
            case 'air':
380 4
                $builder = new Air();
381 4
                break;
382 30
            case 'queue':
383 7
                $builder = new Queue();
384 7
                break;
385 23
            default:
386 23
                $builder = $this;
387 23
                break;
388 23
        }
389
390 69
        return $builder;
391
    }
392
}
393