Completed
Push — master ( 529db1...994f25 )
by Dieter
06:13
created

AddMultiElements::addSegments()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 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\Struct\Pnr;
24
25
use Amadeus\Client\RequestOptions\Pnr\Element;
26
use Amadeus\Client\RequestOptions\Pnr\Element\ReceivedFrom;
27
use Amadeus\Client\RequestOptions\Pnr\Segment;
28
use Amadeus\Client\RequestOptions\Pnr\Traveller;
29
use Amadeus\Client\RequestOptions\Pnr\TravellerGroup;
30
use Amadeus\Client\RequestOptions\PnrAddMultiElementsBase;
31
use Amadeus\Client\RequestOptions\PnrAddMultiElementsOptions;
32
use Amadeus\Client\RequestOptions\PnrCreatePnrOptions;
33
use Amadeus\Client\Struct\BaseWsMessage;
34
use Amadeus\Client\Struct\InvalidArgumentException;
35
use Amadeus\Client\Struct\Pnr\AddMultiElements\AirAuxItinerary;
36
use Amadeus\Client\Struct\Pnr\AddMultiElements\DataElementsIndiv;
37
use Amadeus\Client\Struct\Pnr\AddMultiElements\DataElementsMaster;
38
use Amadeus\Client\Struct\Pnr\AddMultiElements\ElementManagementItinerary;
39
use Amadeus\Client\Struct\Pnr\AddMultiElements\ItineraryInfo;
40
use Amadeus\Client\Struct\Pnr\AddMultiElements\OriginDestinationDetails;
41
use Amadeus\Client\Struct\Pnr\AddMultiElements\Reference;
42
use Amadeus\Client\Struct\Pnr\AddMultiElements\ReferenceForDataElement;
43
use Amadeus\Client\Struct\Pnr\AddMultiElements\ReferenceForSegment;
44
use Amadeus\Client\Struct\Pnr\AddMultiElements\TravellerInfo;
45
46
/**
47
 * Structure class for representing the PNR_AddMultiElements request message
48
 *
49
 * @package Amadeus\Client\Struct\Pnr
50
 * @author Dieter Devlieghere <[email protected]>
51
 */
52
class AddMultiElements extends BaseWsMessage
53
{
54
    /**
55
     * @var AddMultiElements\ReservationInfo
56
     */
57
    public $reservationInfo;
58
    /**
59
     * @var AddMultiElements\PnrActions
60
     */
61
    public $pnrActions;
62
    /**
63
     * @var AddMultiElements\TravellerInfo[]
64
     */
65
    public $travellerInfo = [];
66
    /**
67
     * @var AddMultiElements\OriginDestinationDetails[]
68
     */
69
    public $originDestinationDetails = [];
70
    /**
71
     * @var AddMultiElements\DataElementsMaster
72
     */
73
    public $dataElementsMaster;
74
75
    /**
76
     * Create PNR_AddMultiElements object
77
     *
78
     * @param PnrAddMultiElementsBase|null $params
79
     */
80
    public function __construct(PnrAddMultiElementsBase $params = null)
81
    {
82
        if (!is_null($params)) {
83
            if ($params instanceof PnrCreatePnrOptions) {
84
                $this->loadCreatePnr($params);
85
            } elseif ($params instanceof PnrAddMultiElementsOptions) {
86
                $this->loadBare($params);
87
            }
88
        }
89
    }
90
91
    /**
92
     * PNR_AddMultiElements call which only adds requested data to the message
93
     *
94
     * For doing specific actions like ignoring or saving PNR.
95
     *
96
     * @param PnrAddMultiElementsOptions $params
97
     */
98
    protected function loadBare(PnrAddMultiElementsOptions $params)
99
    {
100
        if (!is_null($params->actionCode)) {
101
            $this->pnrActions = new AddMultiElements\PnrActions(
102
                $params->actionCode
103
            );
104
        }
105
106
        if (!is_null($params->recordLocator)) {
107
            $this->reservationInfo = new AddMultiElements\ReservationInfo($params->recordLocator);
108
        }
109
110
        $tattooCounter = 0;
111
112
        $this->addTravellers($params->travellers);
113
114
        if (!empty($params->tripSegments)) {
115
            $this->addSegments($params->tripSegments, $tattooCounter);
116
        }
117
118
        if (!empty($params->elements)) {
119
            $this->addElements(
120
                $params->elements,
121
                $tattooCounter,
122
                $params->receivedFrom
123
            );
124
        } elseif (!is_null($params->receivedFrom)) {
125
            if ($this->dataElementsMaster === null) {
126
                $this->dataElementsMaster = new DataElementsMaster();
127
            }
128
129
            $tattooCounter++;
130
131
            $this->dataElementsMaster->dataElementsIndiv[] = $this->createElement(
132
                new ReceivedFrom(['receivedFrom' => $params->receivedFrom]),
133
                $tattooCounter
134
            );
135
        }
136
    }
137
138
    /**
139
     * Make PNR_AddMultiElements structure from a PnrCreatePnrOptions input.
140
     *
141
     * @throws InvalidArgumentException When invalid input is provided
142
     * @param PnrCreatePnrOptions $params
143
     */
144
    protected function loadCreatePnr(PnrCreatePnrOptions $params)
145
    {
146
        $this->pnrActions = new AddMultiElements\PnrActions(
147
            $params->actionCode
148
        );
149
150
        $tattooCounter = 0;
151
152
        if ($params->travellerGroup !== null) {
153
            $this->addTravellerGroup($params->travellerGroup);
154
        } else {
155
            $this->addTravellers($params->travellers);
156
        }
157
158
        $this->addSegments($params->tripSegments, $tattooCounter);
159
160
        $this->addElements(
161
            $params->elements,
162
            $tattooCounter,
163
            $params->receivedFrom
164
        );
165
    }
166
167
    /**
168
     * @param Segment[] $segments
169
     * @param int $tattooCounter
170
     */
171
    protected function addSegments($segments, &$tattooCounter)
172
    {
173
        $tmpOrigDest = new OriginDestinationDetails();
174
175
        foreach ($segments as $segment) {
176
            $tmpOrigDest->itineraryInfo[] = $this->createSegment($segment, $tattooCounter);
177
        }
178
179
        $this->originDestinationDetails[] = $tmpOrigDest;
180
    }
181
182
    /**
183
     * @param Segment $segment
184
     * @param $tattooCounter
185
     * @return ItineraryInfo
186
     */
187
    protected function createSegment($segment, &$tattooCounter)
188
    {
189
        $createdSegment = null;
0 ignored issues
show
Unused Code introduced by
$createdSegment 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...
190
191
        $tattooCounter++;
192
193
        $reflect = new \ReflectionClass($segment);
194
        $segmentType = $reflect->getShortName();
195
196
        switch ($segmentType) {
197
            case 'Miscellaneous':
198
                /** @var Segment\Miscellaneous $segment */
199
                $createdSegment = new ItineraryInfo($tattooCounter, ElementManagementItinerary::SEGMENT_MISCELLANEOUS);
200
                $createdSegment->airAuxItinerary = new AirAuxItinerary($segmentType, $segment);
201
                break;
202
            case 'Air':
203
                throw new \RuntimeException('NOT YET IMPLEMENTED');
204
                break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
205
            case 'Ghost':
206
                throw new \RuntimeException('NOT YET IMPLEMENTED');
207
                break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
208
            default:
209
                throw new InvalidArgumentException('Segment type ' . $segmentType . ' is not supported');
210
                break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
211
        }
212
213
        if (count($segment->references) > 0) {
214
            $createdSegment->referenceForSegment = new ReferenceForSegment();
215
            foreach ($segment->references as $singleRef) {
216
                $createdSegment->referenceForSegment->reference[] = new Reference($singleRef->type, $singleRef->id);
217
            }
218
        }
219
220
        return $createdSegment;
221
    }
222
223
224
    /**
225
     * @param Traveller[] $travellers
226
     */
227
    protected function addTravellers($travellers)
228
    {
229
        foreach ($travellers as $traveller) {
230
            $this->travellerInfo[] = $this->createTraveller($traveller);
231
        }
232
    }
233
234
    /**
235
     * @param Traveller $traveller
236
     * @return TravellerInfo
237
     */
238
    protected function createTraveller($traveller)
239
    {
240
        return new TravellerInfo($traveller);
241
    }
242
243
    /**
244
     * @param TravellerGroup $group
245
     */
246
    protected function addTravellerGroup($group)
247
    {
248
        $this->travellerInfo[] = new TravellerInfo(null, $group);
249
250
        $this->addTravellers($group->travellers);
251
    }
252
253
    /**
254
     * @param Element[] $elements
255
     * @param int $tattooCounter (BYREF)
256
     * @param string|null $receivedFromString
257
     */
258
    protected function addElements($elements, &$tattooCounter, $receivedFromString = null)
259
    {
260
        if ($this->dataElementsMaster === null) {
261
            $this->dataElementsMaster = new DataElementsMaster();
262
        }
263
264
        //Only add a default RF element if there is no explicitly provided RF element!
265
        $explicitRf = false;
266
267
        foreach ($elements as $element) {
268
            if ($element instanceof Element) {
269
                $this->dataElementsMaster->dataElementsIndiv[] = $this->createElement(
270
                    $element,
271
                    $tattooCounter
272
                );
273
            }
274
275
            if ($element instanceof ReceivedFrom) {
276
                $explicitRf = true;
277
            }
278
        }
279
280
        if ($receivedFromString !== null && !$explicitRf) {
281
            $this->dataElementsMaster->dataElementsIndiv[] = $this->createElement(
282
                new ReceivedFrom(['receivedFrom' => $receivedFromString]),
283
                $tattooCounter
284
            );
285
        }
286
    }
287
288
    /**
289
     * @param Element $element
290
     * @param int $tattooCounter (BYREF)
291
     * @throws InvalidArgumentException
292
     * @return DataElementsIndiv
293
     */
294
    protected function createElement($element, &$tattooCounter)
295
    {
296
        $createdElement = null;
0 ignored issues
show
Unused Code introduced by
$createdElement 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...
297
298
        $tattooCounter++;
299
300
        $createdElement = new DataElementsIndiv($element, $tattooCounter);
301
302
        if (!empty($element->references)) {
303
            $createdElement->referenceForDataElement = new ReferenceForDataElement($element->references);
304
        }
305
306
        return $createdElement;
307
    }
308
}
309