Completed
Push — master ( d17808...738038 )
by Dieter
07:30
created

Base::createMiniRuleGetFromPricing()   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 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\DocIssuanceIssueTicketOptions;
29
use Amadeus\Client\RequestOptions\InfoEncodeDecodeCityOptions;
30
use Amadeus\Client\RequestOptions\MiniRuleGetFromPricingOptions;
31
use Amadeus\Client\RequestOptions\MiniRuleGetFromPricingRecOptions;
32
use Amadeus\Client\RequestOptions\OfferConfirmAirOptions;
33
use Amadeus\Client\RequestOptions\OfferConfirmCarOptions;
34
use Amadeus\Client\RequestOptions\OfferConfirmHotelOptions;
35
use Amadeus\Client\RequestOptions\OfferCreateOptions;
36
use Amadeus\Client\RequestOptions\OfferVerifyOptions;
37
use Amadeus\Client\RequestOptions\PriceXplorerExtremeSearchOptions;
38
use Amadeus\Client\RequestOptions\RequestOptionsInterface;
39
use Amadeus\Client\RequestOptions\SalesReportsDisplayQueryReportOptions;
40
use Amadeus\Client\RequestOptions\SecurityAuthenticateOptions;
41
use Amadeus\Client\RequestOptions\TicketCreateTsmFromPricingOptions;
42
use Amadeus\Client\RequestOptions\ServiceIntegratedPricingOptions;
43
use Amadeus\Client\RequestOptions\TicketCreateTstFromPricingOptions;
44
use Amadeus\Client\RequestOptions\TicketDeleteTstOptions;
45
use Amadeus\Client\RequestOptions\TicketDisplayTstOptions;
46
use Amadeus\Client\Struct;
47
48
/**
49
 * Base request creator - the default request creator.
50
 *
51
 * In charge of building the request objects that are used by the Soap Client.
52
 * They are built depending on:
53
 * - Which message is being created.
54
 * - Which request options were provided.
55
 * - What message version is active in the current WSDL?
56
 *
57
 * @package Amadeus\Client\RequestCreator
58
 * @author Dieter Devlieghere <[email protected]>
59
 */
60
class Base implements RequestCreatorInterface
61
{
62
    /**
63
     * Parameters
64
     *
65
     * @var RequestCreatorParams
66
     */
67
    protected $params;
68
69
    /**
70
     * Associative array of messages (as keys) and versions (as values) that are present in the WSDL.
71
     *
72
     * @var array
73
     */
74
    protected $messagesAndVersions = [];
75
76
    /**
77
     * Base Request Creator constructor.
78
     *
79
     * @param RequestCreatorParams $params
80
     */
81
    public function __construct(RequestCreatorParams $params)
82
    {
83
        $this->params = $params;
84
        $this->messagesAndVersions = $params->messagesAndVersions;
85
    }
86
87
    /**
88
     * Create a request message for a given message with a set of options.
89
     *
90
     * @param string $messageName the message name as named in the WSDL
91
     * @param RequestOptionsInterface $params
92
     * @throws Struct\InvalidArgumentException When invalid input is detected during message creation.
93
     * @throws InvalidMessageException when trying to create a request for a message that is not in your WSDL.
94
     * @return mixed the created request
95
     */
96
    public function createRequest($messageName, RequestOptionsInterface $params)
97
    {
98
        $this->checkMessageIsInWsdl($messageName);
99
100
        $builder = $this->findBuilderForMessage($messageName);
101
102
        $methodName = 'create'.str_replace("_", "", $messageName);
103
104
        if (method_exists($builder, $methodName)) {
105
            return $builder->$methodName($params, $this->getActiveVersionFor($messageName));
106
        } else {
107
            throw new \RuntimeException('Message '.$methodName.' is not implemented in '.__CLASS__);
108
        }
109
    }
110
111
    /**
112
     * Security_SignOut
113
     *
114
     * @return Struct\Security\SignOut
115
     */
116
    protected function createSecuritySignOut()
117
    {
118
        return new Struct\Security\SignOut();
119
    }
120
121
    /**
122
     * Create request object for Security_Authenticate message
123
     *
124
     * @param SecurityAuthenticateOptions $params
125
     * @return Struct\Security\Authenticate
126
     */
127
    protected function createSecurityAuthenticate(SecurityAuthenticateOptions $params)
128
    {
129
        return new Struct\Security\Authenticate($params);
130
    }
131
132
    /**
133
     * Offer_VerifyOffer
134
     *
135
     * @param OfferVerifyOptions $params
136
     * @return Struct\Offer\Verify
137
     */
138
    protected function createOfferVerifyOffer(OfferVerifyOptions $params)
139
    {
140
        $req = new Struct\Offer\Verify(
141
            $params->offerReference,
142
            $params->segmentName
143
        );
144
145
        return $req;
146
    }
147
148
    /**
149
     * @param OfferConfirmAirOptions $params
150
     * @return Struct\Offer\ConfirmAir
151
     */
152
    protected function createOfferConfirmAirOffer(OfferConfirmAirOptions $params)
153
    {
154
        return new Struct\Offer\ConfirmAir($params);
155
    }
156
157
158
    /**
159
     * Offer_ConfirmHotelOffer
160
     *
161
     * @param OfferConfirmHotelOptions $params
162
     * @return Struct\Offer\ConfirmHotel
163
     */
164
    protected function createOfferConfirmHotelOffer(OfferConfirmHotelOptions $params)
165
    {
166
        return new Struct\Offer\ConfirmHotel($params);
167
    }
168
169
    /**
170
     * @param OfferConfirmCarOptions $params
171
     * @return Struct\Offer\ConfirmCar
172
     */
173
    protected function createOfferConfirmCarOffer(OfferConfirmCarOptions $params)
174
    {
175
        return new Struct\Offer\ConfirmCar($params);
176
    }
177
178
    /**
179
     * Offer_CreateOffer
180
     *
181
     * Ok, I just realised this function name is a bit confusing. Sorry.
182
     *
183
     * @param OfferCreateOptions $params
184
     * @return Struct\Offer\Create
185
     */
186
    protected function createOfferCreateOffer(OfferCreateOptions $params)
187
    {
188
        return new Struct\Offer\Create($params);
189
    }
190
191
    /**
192
     * Command_Cryptic
193
     *
194
     * @param CommandCrypticOptions $params
195
     * @return Struct\Command\Cryptic
196
     */
197
    protected function createCommandCryptic(CommandCrypticOptions $params)
198
    {
199
        return new Struct\Command\Cryptic($params->entry);
200
    }
201
202
    /**
203
     * Info_EncodeDecodeCity
204
     *
205
     * @param InfoEncodeDecodeCityOptions $params
206
     * @return Struct\Info\EncodeDecodeCity
207
     */
208
    protected function createInfoEncodeDecodeCity(InfoEncodeDecodeCityOptions $params)
209
    {
210
        return new Struct\Info\EncodeDecodeCity($params);
211
    }
212
213
    /**
214
     * MiniRule_GetFromPricingRec
215
     *
216
     * @param MiniRuleGetFromPricingRecOptions $params
217
     * @return Struct\MiniRule\GetFromPricingRec
218
     */
219
    protected function createMiniRuleGetFromPricingRec(MiniRuleGetFromPricingRecOptions $params)
220
    {
221
        return new Struct\MiniRule\GetFromPricingRec($params);
222
    }
223
224
    /**
225
     * MiniRule_GetFromPricing
226
     *
227
     * @param MiniRuleGetFromPricingOptions $params
228
     * @return Struct\MiniRule\GetFromPricing
229
     */
230
    protected function createMiniRuleGetFromPricing(MiniRuleGetFromPricingOptions $params)
231
    {
232
        return new Struct\MiniRule\GetFromPricing($params);
233
    }
234
235
    /**
236
     * Ticket_CreateTstFromPricing
237
     *
238
     * @param TicketCreateTstFromPricingOptions $params
239
     * @return Struct\Ticket\CreateTSTFromPricing
240
     */
241
    protected function createTicketCreateTSTFromPricing(TicketCreateTstFromPricingOptions $params)
242
    {
243
        return new Struct\Ticket\CreateTSTFromPricing($params);
244
    }
245
246
    /**
247
     * Ticket_CreateTSMFromPricing
248
     *
249
     * @param TicketCreateTsmFromPricingOptions $params
250
     * @return Struct\Ticket\CreateTSMFromPricing
251
     */
252
    protected function createTicketCreateTSMFromPricing(TicketCreateTsmFromPricingOptions $params)
253
    {
254
        return new Struct\Ticket\CreateTSMFromPricing($params);
255
    }
256
257
    /**
258
     * Ticket_DeleteTST
259
     *
260
     * @param TicketDeleteTstOptions $params
261
     * @return Struct\Ticket\DeleteTST
262
     */
263
    protected function createTicketDeleteTST(TicketDeleteTstOptions $params)
264
    {
265
        return new Struct\Ticket\DeleteTST($params);
266
    }
267
268
    /**
269
     * Ticket_DisplayTST
270
     *
271
     * @param TicketDisplayTstOptions $params
272
     * @return Struct\Ticket\DisplayTST
273
     */
274
    protected function createTicketDisplayTST(TicketDisplayTstOptions $params)
275
    {
276
        return new Struct\Ticket\DisplayTST($params);
277
    }
278
279
    /**
280
     * DocIssuance_IssueTicket
281
     *
282
     * @param DocIssuanceIssueTicketOptions $params
283
     * @return Struct\DocIssuance\IssueTicket
284
     */
285
    protected function createDocIssuanceIssueTicket(DocIssuanceIssueTicketOptions $params)
286
    {
287
        return new Struct\DocIssuance\IssueTicket($params);
288
    }
289
290
    /**
291
     * PriceXplorer_ExtremeSearch
292
     *
293
     * @param PriceXplorerExtremeSearchOptions $params
294
     * @return Struct\PriceXplorer\ExtremeSearch
295
     */
296
    protected function createPriceXplorerExtremeSearch(PriceXplorerExtremeSearchOptions $params)
297
    {
298
        return new Struct\PriceXplorer\ExtremeSearch($params);
299
    }
300
301
    /**
302
     * SalesReports_DisplayQueryReport
303
     *
304
     * @param SalesReportsDisplayQueryReportOptions $params
305
     * @return Struct\SalesReports\DisplayQueryReport
306
     */
307
    protected function createSalesReportsDisplayQueryReport(SalesReportsDisplayQueryReportOptions $params)
308
    {
309
        return new Struct\SalesReports\DisplayQueryReport($params);
310
    }
311
312
    /**
313
     * Service_IntegratedPricing
314
     *
315
     * @param ServiceIntegratedPricingOptions $params
316
     * @return Struct\Service\IntegratedPricing
317
     */
318
    protected function createServiceIntegratedPricing(ServiceIntegratedPricingOptions $params)
319
    {
320
        return new Struct\Service\IntegratedPricing($params);
321
    }
322
323
    /**
324
     * Check if a given message is in the active WSDL(s). Throws exception if it isn't.
325
     *
326
     * @throws InvalidMessageException if message is not in loaded WSDL(s).
327
     * @param string $messageName
328
     */
329
    protected function checkMessageIsInWsdl($messageName)
330
    {
331
        if (!array_key_exists($messageName, $this->messagesAndVersions)) {
332
            throw new InvalidMessageException('Message "' . $messageName . '" is not in WDSL');
333
        }
334
    }
335
336
    /**
337
     * Get the version number active in the WSDL for the given message
338
     *
339
     * @param string $messageName
340
     * @return float|string
341
     */
342
    protected function getActiveVersionFor($messageName)
343
    {
344
        return $this->messagesAndVersions[$messageName];
345
    }
346
347
    /**
348
     * Find the correct builder for a given message
349
     *
350
     * Message build methods in all builders must adhere to the
351
     * 'create'<message name without underscores> logic as used in createRequest method.
352
     *
353
     * @param string $messageName
354
     * @return Fare|Pnr|Queue|Base|Air
355
     */
356
    protected function findBuilderForMessage($messageName)
357
    {
358
        $section = strtolower(substr($messageName, 0, strpos($messageName, '_')));
359
360
        switch ($section) {
361
            case 'fare':
362
                $builder = new Fare();
363
                break;
364
            case 'pnr':
365
                $builder = new Pnr($this->params);
366
                break;
367
            case 'air':
368
                $builder = new Air();
369
                break;
370
            case 'queue':
371
                $builder = new Queue();
372
                break;
373
            default:
374
                $builder = $this;
375
                break;
376
        }
377
378
        return $builder;
379
    }
380
}
381