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

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...
299
     */
300
    protected function findBuilderForMessage($messageName)
301
    {
302
        $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...
303
304
        $section = strtolower(substr($messageName, 0, strpos($messageName, '_')));
305
306
        switch ($section) {
307
            case 'fare':
308
                $builder = new Fare();
309
                break;
310
            case 'pnr':
311
                $builder = new Pnr($this->params);
312
                break;
313
            case 'air':
314
                $builder = new Air();
315
                break;
316
            case 'queue':
317
                $builder = new Queue();
318
                break;
319
            default:
320
                $builder = $this;
321
                break;
322
        }
323
324
        return $builder;
325
    }
326
}
327