Completed
Push — master ( 5242c7...dcb951 )
by Dieter
07:52
created

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

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
335
336
        $section = strtolower(substr($messageName, 0, strpos($messageName, '_')));
337
338
        switch ($section) {
339
            case 'fare':
340
                $builder = new Fare();
341
                break;
342
            case 'pnr':
343
                $builder = new Pnr($this->params);
344
                break;
345
            case 'air':
346
                $builder = new Air();
347
                break;
348
            case 'queue':
349
                $builder = new Queue();
350
                break;
351
            default:
352
                $builder = $this;
353
                break;
354
        }
355
356
        return $builder;
357
    }
358
}
359