Completed
Push — master ( fc1ace...8ff8f8 )
by Dieter
07:27
created

Base::analyzeFareMasterPricerCalendarResponse()   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\ResponseHandler;
24
25
use Amadeus\Client\Exception;
26
use Amadeus\Client\ResponseHandler\Air\RetrieveSeatMap;
27
use Amadeus\Client\Result;
28
use Amadeus\Client\Session\Handler\SendResult;
29
30
/**
31
 * Default Response Handler
32
 *
33
 * @package Amadeus\Client\ResponseHandler
34
 * @author Dieter Devlieghere <[email protected]>
35
 */
36
class Base implements ResponseHandlerInterface
37
{
38
    /**
39
     * Default namespace prefix we'll be using for xpath queries
40
     *
41
     * Why not "m"? It's as good as any other letter.
42
     */
43
    const XMLNS_PREFIX = "m";
44
45
    /**
46
     * Analyze the response from the server and throw an exception when an error has been detected.
47
     *
48
     * @param SendResult $sendResult The Send Result from the Session Handler
49
     * @param string $messageName The message that was called
50
     *
51
     * @throws Exception
52
     * @throws \RuntimeException
53
     * @return Result
54
     */
55
    public function analyzeResponse($sendResult, $messageName)
56
    {
57
        $methodName = 'analyze' . str_replace('_', '', ucfirst($messageName)) . 'Response';
58
59
        if (!empty($sendResult->exception)) {
60
            return $this->makeResultForException($sendResult);
61
        } elseif (method_exists($this, $methodName)) {
62
            return $this->$methodName(
63
                $sendResult
64
            );
65
        } else {
66
            return new Result($sendResult, Result::STATUS_UNKNOWN);
67
        }
68
    }
69
70
    /**
71
     * Analysing a Security_Authenticate
72
     *
73
     * @param SendResult $response Security_Authenticate result
74
     * @return Result
75
     */
76
    protected function analyzeSecurityAuthenticateResponse($response)
77
    {
78
        return $this->analyzeSimpleResponseErrorCodeAndMessage($response);
79
    }
80
81
    /**
82
     * Analysing a Security_Authenticate
83
     *
84
     * @param SendResult $response Security_Authenticate result
85
     * @return Result
86
     */
87
    protected function analyzeSecuritySignOutResponse($response)
88
    {
89
        return $this->analyzeSimpleResponseErrorCodeAndMessage($response);
90
    }
91
92
    /**
93
     * Unknown response for Command_Cryptic because you need to analyse the cryptic response yourself
94
     *
95
     * @param SendResult $response
96
     * @return Result
97
     */
98
    protected function analyzeCommandCrypticResponse($response)
99
    {
100
        $ccResult = new Result($response, Result::STATUS_UNKNOWN);
101
        $ccResult->messages[] = new Result\NotOk(
102
            0,
103
            "Response handling not supported for cryptic entries"
104
        );
105
106
        return $ccResult;
107
    }
108
109 View Code Duplication
    protected function analyzeAirMultiAvailabilityResponse($response)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
110
    {
111
        $analyzeResponse = new Result($response);
112
113
        $message = null;
114
115
        $domXpath = $this->makeDomXpath($response->responseXml);
116
117
        $codeNode = $domXpath->query("//m:errorOrWarningSection/m:errorOrWarningInfo//m:code")->item(0);
118
        if ($codeNode instanceof \DOMNode) {
119
            $analyzeResponse->status = Result::STATUS_ERROR;
120
121
            $categoryNode = $domXpath->query("//m:errorOrWarningSection/m:errorOrWarningInfo//m:type")->item(0);
122
            if ($categoryNode instanceof \DOMNode) {
123
                $analyzeResponse->status = $this->makeStatusFromErrorQualifier($categoryNode->nodeValue);
124
            }
125
126
            $messageNodes = $domXpath->query('//m:errorOrWarningSection/m:textInformation/m:freeText');
127
            if ($messageNodes->length > 0) {
128
                $message = $this->makeMessageFromMessagesNodeList($messageNodes);
129
            }
130
            $analyzeResponse->messages [] = new Result\NotOk($codeNode->nodeValue, $message);
131
        }
132
133
        return $analyzeResponse;
134
    }
135
136
    protected function analyzeAirSellFromRecommendationResponse($response)
137
    {
138
        $analyzeResponse = new Result($response);
139
140
        $errMsgMap = [
141
            "288" => "UNABLE TO SATISFY, NEED CONFIRMED FLIGHT STATUS",
142
            "390" => "UNABLE TO REFORMAT"
143
        ];
144
145
        $domXpath = $this->makeDomXpath($response->responseXml);
146
147
        $codeNode = $domXpath->query("//m:errorSegment/m:errorDetails/m:errorCode")->item(0);
148
        if ($codeNode instanceof \DOMNode) {
149
            $analyzeResponse->status = Result::STATUS_ERROR;
150
151
            $categoryNode = $domXpath->query("//m:errorSegment/m:errorDetails/m:errorCategory")->item(0);
152
            if ($categoryNode instanceof \DOMNode) {
153
                $analyzeResponse->status = $this->makeStatusFromErrorQualifier($categoryNode->nodeValue);
154
            }
155
156
            $message = (array_key_exists($codeNode->nodeValue, $errMsgMap)) ?
157
                $errMsgMap[$codeNode->nodeValue] : 'UNKNOWN ERROR';
158
159
            $analyzeResponse->messages [] = new Result\NotOk($codeNode->nodeValue, $message);
160
        }
161
162
        return $analyzeResponse;
163
    }
164
165
    /**
166
     * @param SendResult $response
167
     * @return Result
168
     */
169
    protected function analyzeAirFlightInfoResponse($response)
170
    {
171
        $analyzeResponse = new Result($response);
172
173
        $code = null;
174
        $message = null;
175
176
        $domXpath = $this->makeDomXpath($response->responseXml);
177
178
        $categoryNodes = $domXpath->query('//m:responseError/m:errorInfo/m:errorDetails/m:errorCategory');
179
        if ($categoryNodes->length > 0) {
180
            $analyzeResponse->status = $this->makeStatusFromErrorQualifier($categoryNodes->item(0)->nodeValue);
181
        }
182
183
        $codeNodes = $domXpath->query('//m:responseError/m:errorInfo/m:errorDetails/m:errorCode');
184
        if ($codeNodes->length > 0) {
185
            $code = $codeNodes->item(0)->nodeValue;
186
        }
187
188
        $messageNodes = $domXpath->query('//m:responseError/m:interactiveFreeText/m:freeText');
189
        if ($messageNodes->length > 0) {
190
            $message = $this->makeMessageFromMessagesNodeList($messageNodes);
191
        }
192
193
        if (!is_null($message) && !is_null($code)) {
194
            $analyzeResponse->messages[] = new Result\NotOk($code, $message);
195
        }
196
197
        return $analyzeResponse;
198
    }
199
200
    /**
201
     * @param SendResult $response
202
     * @return Result
203
     */
204
    protected function analyzeAirRetrieveSeatMapResponse($response)
205
    {
206
        $analyzeResponse = new Result($response);
207
208
        $code = null;
0 ignored issues
show
Unused Code introduced by
$code 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...
209
        $message = null;
0 ignored issues
show
Unused Code introduced by
$message 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...
210
211
        $domXpath = $this->makeDomXpath($response->responseXml);
212
213
        $errorCodeNode = $domXpath->query('//m:errorInformation/m:errorDetails/m:code');
214 View Code Duplication
        if ($errorCodeNode->length > 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
215
            $analyzeResponse->status = Result::STATUS_ERROR;
216
217
            $errCode = $errorCodeNode->item(0)->nodeValue;
218
            $level = null;
219
            $errDesc = null;
0 ignored issues
show
Unused Code introduced by
$errDesc 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...
220
221
            $errorLevelNode = $domXpath->query('//m:errorInformation/m:errorDetails/m:processingLevel');
222
            if ($errorLevelNode->length > 0) {
223
                $level = RetrieveSeatMap::decodeProcessingLevel($errorLevelNode->item(0)->nodeValue);
224
            }
225
226
            $errorDescNode = $domXpath->query('//m:errorInformation/m:errorDetails/m:description');
227
            if ($errorDescNode->length > 0) {
228
                $errDesc = $errorDescNode->item(0)->nodeValue;
229
            } else {
230
                $errDesc = RetrieveSeatMap::findMessage($errCode);
231
            }
232
233
            $analyzeResponse->messages[] = new Result\NotOk(
234
                $errCode,
235
                $errDesc,
236
                $level
237
            );
238
        }
239
240
        $codeNode = $domXpath->query('//m:warningInformation/m:warningDetails/m:number');
241 View Code Duplication
        if ($codeNode->length > 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
242
            $analyzeResponse->status = Result::STATUS_WARN;
243
244
            $warnCode = $codeNode->item(0)->nodeValue;
245
            $level = null;
246
            $warnDesc = null;
0 ignored issues
show
Unused Code introduced by
$warnDesc 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...
247
248
            $levelNode = $domXpath->query('//m:warningInformation/m:warningDetails/m:processingLevel');
249
            if ($levelNode->length > 0) {
250
                $level = RetrieveSeatMap::decodeProcessingLevel($levelNode->item(0)->nodeValue);
251
            }
252
253
            $descNode = $domXpath->query('//m:warningInformation/m:warningDetails/m:description');
254
            if ($descNode->length > 0) {
255
                $warnDesc = $descNode->item(0)->nodeValue;
256
            } else {
257
                $warnDesc = RetrieveSeatMap::findMessage($warnCode);
258
            }
259
260
            $analyzeResponse->messages[] = new Result\NotOk(
261
                $warnCode,
262
                $warnDesc,
263
                $level
264
            );
265
        }
266
267
        return $analyzeResponse;
268
    }
269
270
    /**
271
     * Analysing a PNR_Retrieve response
272
     *
273
     * @param SendResult $response PNR_Retrieve result
274
     * @return Result
275
     */
276
    protected function analyzePnrRetrieveResponse($response)
277
    {
278
        return $this->analyzePnrReply($response);
279
    }
280
281
    /**
282
     * @param SendResult $response PNR_AddMultiElements result
283
     * @return Result
284
     */
285
    protected function analyzePnrAddMultiElementsResponse($response)
286
    {
287
        return $this->analyzePnrReply($response);
288
    }
289
290
    /**
291
     * @param SendResult $response PNR_Cancel result
292
     * @return Result
293
     */
294
    protected function analyzePnrCancelResponse($response)
295
    {
296
        return $this->analyzePnrReply($response);
297
    }
298
299
    /**
300
     * Analysing a PNR_Reply
301
     *
302
     * @param SendResult $response PNR_Retrieve result
303
     * @return Result
304
     */
305
    protected function analyzePnrReply($response)
306
    {
307
        $analyzeResponse = new Result($response);
308
309
        $domXpath = $this->makeDomXpath($response->responseXml);
310
311
        //General Errors:
312
        $queryAllErrorCodes = "//m:generalErrorInfo//m:errorOrWarningCodeDetails/m:errorDetails/m:errorCode";
313
        $queryAllErrorMsg = "//m:generalErrorInfo/m:errorWarningDescription/m:freeText";
314
315
        $errorCodeNodeList = $domXpath->query($queryAllErrorCodes);
316
317 View Code Duplication
        if ($errorCodeNodeList->length > 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
318
            $analyzeResponse->status = Result::STATUS_ERROR;
319
320
            $code = $errorCodeNodeList->item(0)->nodeValue;
321
            $errorTextNodeList = $domXpath->query($queryAllErrorMsg);
322
            $message = $this->makeMessageFromMessagesNodeList($errorTextNodeList);
323
324
            $analyzeResponse->messages[] = new Result\NotOk($code, trim($message), 'general');
325
        }
326
327
        //Segment errors:
328
        $querySegmentErrorCodes = "//m:originDestinationDetails//m:errorInfo/m:errorOrWarningCodeDetails/m:errorDetails/m:errorCode";
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 133 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
329
        $querySegmentErrorMsg = "//m:originDestinationDetails//m:errorInfo/m:errorWarningDescription/m:freeText";
330
331
        $errorCodeNodeList = $domXpath->query($querySegmentErrorCodes);
332
333 View Code Duplication
        if ($errorCodeNodeList->length > 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
334
            $analyzeResponse->status = Result::STATUS_ERROR;
335
336
            $code = $errorCodeNodeList->item(0)->nodeValue;
337
            $errorTextNodeList = $domXpath->query($querySegmentErrorMsg);
338
            $message = $this->makeMessageFromMessagesNodeList($errorTextNodeList);
339
340
            $analyzeResponse->messages[] = new Result\NotOk($code, trim($message), 'segment');
341
        }
342
343
        //Element errors:
344
        $queryElementErrorCodes = "//m:dataElementsIndiv/m:elementErrorInformation/m:errorOrWarningCodeDetails/m:errorDetails/m:errorCode";
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 139 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
345
        $queryElementErrorMsg = "//m:dataElementsIndiv//m:elementErrorInformation/m:errorWarningDescription/m:freeText";
346
347
        $errorCodeNodeList = $domXpath->query($queryElementErrorCodes);
348
349 View Code Duplication
        if ($errorCodeNodeList->length > 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
350
            $analyzeResponse->status = Result::STATUS_ERROR;
351
352
            $code = $errorCodeNodeList->item(0)->nodeValue;
353
354
            $errorTextNodeList = $domXpath->query($queryElementErrorMsg);
355
            $message = $this->makeMessageFromMessagesNodeList($errorTextNodeList);
356
357
            $analyzeResponse->messages[] = new Result\NotOk($code, trim($message), 'element');
358
        }
359
360
        return $analyzeResponse;
361
    }
362
363
    /**
364
     * @param SendResult $response Pnr_RetrieveAndDisplay response
365
     * @return Result
366
     * @throws Exception
367
     */
368
    protected function analyzePnrRetrieveAndDisplayResponse($response)
369
    {
370
        return $this->analyzeSimpleResponseErrorCodeAndMessage($response);
371
    }
372
373
    /**
374
     * Analysing a PNR_TransferOwnershipReply
375
     *
376
     * @param SendResult $response PNR_TransferOwnership response
377
     * @return Result
378
     * @throws Exception
379
     */
380
    protected function analyzePNRTransferOwnershipResponse($response)
381
    {
382
        return $this->analyzeSimpleResponseErrorCodeAndMessage($response);
383
    }
384
385
    /**
386
     * Analysing a PNR_DisplayHistoryReply
387
     *
388
     * @param SendResult $response PNR_DisplayHistoryReply result
389
     * @return Result
390
     * @throws Exception
391
     */
392
    protected function analyzePnrDisplayHistoryResponse($response)
393
    {
394
        $analyzeResponse = new Result($response);
395
396
        $domXpath = $this->makeDomXpath($response->responseXml);
397
398
        $queryAllErrorCodes = "//m:generalErrorGroup//m:errorNumber/m:errorDetails/m:errorCode";
399
        $queryAllErrorMsg = "//m:generalErrorGroup/m:genrealErrorText/m:freeText";
400
401
        $errorCodeNodeList = $domXpath->query($queryAllErrorCodes);
402
403 View Code Duplication
        if ($errorCodeNodeList->length > 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
404
            $analyzeResponse->status = Result::STATUS_ERROR;
405
406
            $code = $errorCodeNodeList->item(0)->nodeValue;
407
            $errorTextNodeList = $domXpath->query($queryAllErrorMsg);
408
            $message = $this->makeMessageFromMessagesNodeList($errorTextNodeList);
409
410
            $analyzeResponse->messages[] = new Result\NotOk($code, trim($message));
411
        }
412
413
        return $analyzeResponse;
414
    }
415
416
    /**
417
     * @param SendResult $response Queue_RemoveItem response
418
     * @return Result
419
     * @throws Exception
420
     */
421
    protected function analyzeQueueRemoveItemResponse($response)
422
    {
423
        return $this->analyzeGenericQueueResponse($response);
424
    }
425
426
    /**
427
     * @param SendResult $response Queue_MoveItem response
428
     * @return Result
429
     * @throws Exception
430
     */
431
    protected function analyzeQueueMoveItemResponse($response)
432
    {
433
        return $this->analyzeGenericQueueResponse($response);
434
    }
435
436
    /**
437
     * @param SendResult $response Queue_PlacePNR response
438
     * @return Result
439
     * @throws Exception
440
     */
441
    protected function analyzeQueuePlacePNRResponse($response)
442
    {
443
        return $this->analyzeGenericQueueResponse($response);
444
    }
445
446
    /**
447
     * @param SendResult $response Queue_List result
448
     * @return Result
449
     * @throws Exception
450
     */
451
    protected function analyzeQueueListResponse($response)
452
    {
453
        return $this->analyzeGenericQueueResponse($response);
454
    }
455
456
    /**
457
     * Analyze a generic Queue response
458
     *
459
     * @param SendResult $response Queue_*Reply result
460
     * @return Result
461
     * @throws Exception
462
     */
463
    protected function analyzeGenericQueueResponse($response)
464
    {
465
        $analysisResponse = new Result($response);
466
467
        $domDoc = $this->loadDomDocument($response->responseXml);
468
469
        $errorCodeNode = $domDoc->getElementsByTagName("errorCode")->item(0);
470
471
        if (!is_null($errorCodeNode)) {
472
            $analysisResponse->status = Result::STATUS_WARN;
473
474
            $errorCode = $errorCodeNode->nodeValue;
475
            $errorMessage = $this->getErrorTextFromQueueErrorCode($errorCode);
476
477
            $analysisResponse->messages[] = new Result\NotOk($errorCode, $errorMessage);
478
        }
479
480
        return $analysisResponse;
481
    }
482
483
484
    /**
485
     * @param SendResult $response Fare_PricePNRWithBookingClass result
486
     * @return Result
487
     * @throws Exception
488
     */
489 View Code Duplication
    protected function analyzeFarePricePNRWithBookingClassResponse($response)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
490
    {
491
        $analyzeResponse = new Result($response);
492
493
        $domXpath = $this->makeDomXpath($response->responseXml);
494
495
        $queryErrorCode = "//m:applicationError//m:errorOrWarningCodeDetails/m:errorDetails/m:errorCode";
496
        $queryErrorCategory = "//m:applicationError//m:errorOrWarningCodeDetails/m:errorDetails/m:errorCategory";
497
        $queryErrorMsg = "//m:applicationError/m:errorWarningDescription/m:freeText";
498
499
        $errorCodeNodeList = $domXpath->query($queryErrorCode);
500
501
        if ($errorCodeNodeList->length > 0) {
502
            $analyzeResponse->status = Result::STATUS_ERROR;
503
504
            $errorCatNode = $domXpath->query($queryErrorCategory)->item(0);
505
            if ($errorCatNode instanceof \DOMNode) {
506
                $analyzeResponse->status = $this->makeStatusFromErrorQualifier($errorCatNode->nodeValue);
507
            }
508
509
            $analyzeResponse->messages[] = new Result\NotOk(
510
                $errorCodeNodeList->item(0)->nodeValue,
511
                $this->makeMessageFromMessagesNodeList(
512
                    $domXpath->query($queryErrorMsg)
513
                )
514
            );
515
        }
516
517
        return $analyzeResponse;
518
    }
519
520
    /**
521
     * @param SendResult $response
522
     * @return Result
523
     */
524
    protected function analyzeFareMasterPricerCalendarResponse($response)
525
    {
526
        return $this->analyzeFareMasterPricerTravelBoardSearchResponse($response);
527
    }
528
529
    /**
530
     * @param SendResult $response
531
     * @return Result
532
     */
533
    protected function analyzeFareMasterPricerTravelBoardSearchResponse($response)
534
    {
535
        $analyzeResponse = new Result($response);
536
537
        $domXpath = $this->makeDomXpath($response->responseXml);
538
539
        $queryErrCode = "//m:applicationError//m:applicationErrorDetail/m:error";
540
        $queryErrMsg = "//m:errorMessageText/m:description";
541
542
        $codeNode = $domXpath->query($queryErrCode)->item(0);
543
544
        if ($codeNode instanceof \DOMNode) {
545
            $analyzeResponse->status = Result::STATUS_ERROR;
546
547
            $errMsg = '';
548
            $errMsgNode = $domXpath->query($queryErrMsg)->item(0);
549
            if ($errMsgNode instanceof \DOMNode) {
550
                $errMsg = $errMsgNode->nodeValue;
551
            }
552
553
            $analyzeResponse->messages[] = new Result\NotOk(
554
                $codeNode->nodeValue,
555
                $errMsg
556
            );
557
        }
558
559
        return $analyzeResponse;
560
    }
561
562
    /**
563
     * @param SendResult $response
564
     * @return Result
565
     */
566
    protected function analyzeFareConvertCurrencyResponse($response)
567
    {
568
        return $this->analyzeSimpleResponseErrorCodeAndMessage($response);
569
    }
570
571
    /**
572
     * @param SendResult $response
573
     * @return Result
574
     */
575
    protected function analyzeFareCheckRulesResponse($response)
576
    {
577
        return $this->analyzeSimpleResponseErrorCodeAndMessage($response);
578
    }
579
580
    /**
581
     * @param SendResult $response
582
     * @return Result
583
     */
584
    protected function analyzeFareInformativePricingWithoutPNRResponse($response)
585
    {
586
        return $this->analyzeSimpleResponseErrorCodeAndMessage($response);
587
    }
588
589
590
    /**
591
     * @param SendResult $response
592
     * @return Result
593
     */
594
    protected function analyzeDocIssuanceIssueTicketResponse($response)
595
    {
596
        return $this->analyzeSimpleResponseErrorCodeAndMessageStatusCode($response);
597
    }
598
599
    /**
600
     * @param SendResult $response Ticket_DeleteTST result
601
     * @return Result
602
     */
603
    protected function analyzeTicketDisplayTSTResponse($response)
604
    {
605
        return $this->analyzeSimpleResponseErrorCodeAndMessage($response);
606
    }
607
608
    /**
609
     * @param SendResult $response Ticket_DeleteTST result
610
     * @return Result
611
     */
612
    protected function analyzeTicketDeleteTSTResponse($response)
613
    {
614
        return $this->analyzeTicketCreateTSTFromPricingResponse($response);
615
    }
616
617
    /**
618
     * @param SendResult $response Ticket_CreateTSTFromPricing result
619
     * @return Result
620
     */
621 View Code Duplication
    protected function analyzeTicketCreateTSTFromPricingResponse($response)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
622
    {
623
        $analyzeResponse = new Result($response);
624
625
        $domDoc = $this->loadDomDocument($response->responseXml);
626
627
        $errorCodeNode = $domDoc->getElementsByTagName("applicationErrorCode")->item(0);
628
629
        if (!is_null($errorCodeNode)) {
630
            $analyzeResponse->status = Result::STATUS_ERROR;
631
632
            $errorCatNode = $domDoc->getElementsByTagName("codeListQualifier")->item(0);
633
            if ($errorCatNode instanceof \DOMNode) {
634
                $analyzeResponse->status = $this->makeStatusFromErrorQualifier($errorCatNode->nodeValue);
635
            }
636
637
            $errorCode = $errorCodeNode->nodeValue;
638
            $errorTextNodeList = $domDoc->getElementsByTagName("errorFreeText");
639
640
            $analyzeResponse->messages[] = new Result\NotOk(
641
                $errorCode,
642
                $this->makeMessageFromMessagesNodeList($errorTextNodeList)
643
            );
644
        }
645
646
        return $analyzeResponse;
647
    }
648
649
    /**
650
     * @param SendResult $response
651
     * @return Result
652
     */
653
    protected function analyzeOfferConfirmCarOfferResponse($response)
654
    {
655
        return $this->analyzeGenericOfferResponse($response);
656
    }
657
658
    /**
659
     * @param SendResult $response
660
     * @return Result
661
     */
662
    protected function analyzeOfferConfirmHotelOfferResponse($response)
663
    {
664
        $analyzeResponse = new Result($response);
665
666
        $domXpath = $this->makeDomXpath($response->responseXml);
667
668
        $codeNode = $domXpath->query("//m:errorDetails/m:errorCode")->item(0);
669
        if ($codeNode instanceof \DOMNode) {
670
            $analyzeResponse->status = Result::STATUS_ERROR;
671
672
            $categoryNode = $domXpath->query("//m:errorDetails/m:errorCategory")->item(0);
673
            if ($categoryNode instanceof \DOMNode) {
674
                $analyzeResponse->status = $this->makeStatusFromErrorQualifier($categoryNode->nodeValue);
675
            }
676
677
            $msgNode = $domXpath->query('//m:errorDescription/m:freeText')->item(0);
678
679
            $analyzeResponse->messages[] = new Result\NotOk(
680
                $codeNode->nodeValue,
681
                trim($msgNode->nodeValue)
682
            );
683
        }
684
685
        return $analyzeResponse;
686
    }
687
688
    /**
689
     * @param SendResult $response
690
     * @return Result
691
     */
692
    protected function analyzeOfferConfirmAirOfferResponse($response)
693
    {
694
        return $this->analyzeGenericOfferResponse($response);
695
    }
696
697
    /**
698
     * @param SendResult $response
699
     * @return Result
700
     */
701
    protected function analyzeOfferVerifyOfferResponse($response)
702
    {
703
        return $this->analyzeGenericOfferResponse($response);
704
    }
705
706
    /**
707
     * @param SendResult $response
708
     * @return Result
709
     */
710
    protected function analyzeGenericOfferResponse($response)
711
    {
712
        $analyzeResponse = new Result($response);
713
714
        $domXpath = $this->makeDomXpath($response->responseXml);
715
716
        $msgNode = $domXpath->query('//m:errorsDescription/m:errorWarningDescription/m:freeText')->item(0);
717
718
        if ($msgNode instanceof \DOMNode) {
719
            if (trim($msgNode->nodeValue) === "OFFER CONFIRMED SUCCESSFULLY" ||
720
                trim($msgNode->nodeValue) === "OFFER VERIFIED SUCCESSFULLY"
721
            ) {
722
                $analyzeResponse->messages[] = new Result\NotOk(
723
                    0,
724
                    trim($msgNode->nodeValue)
725
                );
726
                return $analyzeResponse;
727
            }
728
729
            $categoryNode = $domXpath->query('//m:errorDetails/m:errorCategory')->item(0);
730
            if ($categoryNode instanceof \DOMNode) {
731
                $analyzeResponse->status = $this->makeStatusFromErrorQualifier($categoryNode->nodeValue);
732
            }
733
734
            $codeNode = $domXpath->query('//m:errorDetails/m:errorCode')->item(0);
735
736
            $analyzeResponse->messages[] = new Result\NotOk(
737
                $codeNode->nodeValue,
738
                trim($msgNode->nodeValue)
739
            );
740
        }
741
742
        return $analyzeResponse;
743
    }
744
745
    protected function analyzeMiniRuleGetFromPricingRecResponse($response)
746
    {
747
        $analyzeResponse = new Result($response);
748
749
        $domXpath = $this->makeDomXpath($response->responseXml);
750
751
        $statusNode = $domXpath->query('//m:responseDetails/m:statusCode')->item(0);
752
        if ($statusNode instanceof \DOMNode) {
753
            $code = $statusNode->nodeValue;
754
755
            if ($code !== 'O') {
756
                $categoryNode = $domXpath->query('//m:errorOrWarningCodeDetails/m:errorDetails/m:errorCategory')->item(0);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 122 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
757
                $analyzeResponse->status = $this->makeStatusFromErrorQualifier($categoryNode->nodeValue);
758
759
                $codeNode = $domXpath->query('//m:errorOrWarningCodeDetails/m:errorDetails/m:errorCode')->item(0);
760
                $msgNode = $domXpath->query('//m:errorWarningDescription/m:freeText')->item(0);
761
762
                if ($codeNode instanceof \DOMNode && $msgNode instanceof \DOMNode) {
763
                    $analyzeResponse->messages[] = new Result\NotOk(
764
                        $codeNode->nodeValue,
765
                        $msgNode->nodeValue
766
                    );
767
                }
768
            }
769
        }
770
771
        return $analyzeResponse;
772
    }
773
774
    /**
775
     * @param SendResult $response
776
     * @return Result
777
     */
778
    protected function analyzeInfoEncodeDecodeCityResponse($response)
779
    {
780
        return $this->analyzeSimpleResponseErrorCodeAndMessage($response);
781
    }
782
783
    /**
784
     * @param SendResult $response
785
     * @return Result
786
     */
787
    protected function analyzePriceXplorerExtremeSearchResponse($response)
788
    {
789
        return $this->analyzeSimpleResponseErrorCodeAndMessage($response);
790
    }
791
792
    /**
793
     * @param SendResult $response
794
     * @return Result
795
     */
796
    protected function analyzeSalesReportsDisplayQueryReportResponse($response)
797
    {
798
        return $this->analyzeSimpleResponseErrorCodeAndMessage($response);
799
    }
800
801
    /**
802
     * @param SendResult $response WebService message Send Result
803
     * @return Result
804
     * @throws Exception
805
     */
806 View Code Duplication
    protected function analyzeSimpleResponseErrorCodeAndMessage($response)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
807
    {
808
        $analyzeResponse = new Result($response);
809
810
        $domDoc = $this->loadDomDocument($response->responseXml);
811
812
        $errorCodeNode = $domDoc->getElementsByTagName("errorCode")->item(0);
813
814
        if (!is_null($errorCodeNode)) {
815
            $errorCatNode = $domDoc->getElementsByTagName("errorCategory")->item(0);
816
            if ($errorCatNode instanceof \DOMNode) {
817
                $analyzeResponse->status = $this->makeStatusFromErrorQualifier($errorCatNode->nodeValue);
818
            } else {
819
                $analyzeResponse->status = Result::STATUS_ERROR;
820
            }
821
822
            $errorCode = $errorCodeNode->nodeValue;
823
            $errorTextNodeList = $domDoc->getElementsByTagName("freeText");
824
825
            $analyzeResponse->messages[] = new Result\NotOk(
826
                $errorCode,
827
                $this->makeMessageFromMessagesNodeList($errorTextNodeList)
828
            );
829
        }
830
831
        return $analyzeResponse;
832
    }
833
834
    /**
835
     * @param SendResult $response WebService message Send Result
836
     * @return Result
837
     * @throws Exception
838
     */
839 View Code Duplication
    protected function analyzeSimpleResponseErrorCodeAndMessageStatusCode($response)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
840
    {
841
        $analyzeResponse = new Result($response);
842
843
        $domDoc = $this->loadDomDocument($response->responseXml);
844
845
        $errorCodeNode = $domDoc->getElementsByTagName("errorCode")->item(0);
846
847
        if (!is_null($errorCodeNode)) {
848
            $analyzeResponse->status = Result::STATUS_ERROR;
849
850
            $errorCatNode = $domDoc->getElementsByTagName("statusCode")->item(0);
851
            if ($errorCatNode instanceof \DOMNode) {
852
                $analyzeResponse->status = $this->makeStatusFromErrorQualifier($errorCatNode->nodeValue);
853
            }
854
855
            $errorCode = $errorCodeNode->nodeValue;
856
            $errorTextNodeList = $domDoc->getElementsByTagName("freeText");
857
858
            $analyzeResponse->messages[] = new Result\NotOk(
859
                $errorCode,
860
                $this->makeMessageFromMessagesNodeList($errorTextNodeList)
861
            );
862
        }
863
864
        return $analyzeResponse;
865
    }
866
867
    /**
868
     * Returns the errortext from a Queue_*Reply errorcode
869
     *
870
     * This function is necessary because the core only responds
871
     * with errorcode but does not send an errortext.
872
     *
873
     * The errorcodes for all Queue_*Reply messages are the same.
874
     *
875
     * @link https://webservices.amadeus.com/extranet/viewArea.do?id=10
876
     * @param string $errorCode
877
     * @return string the errortext for this errorcode.
878
     */
879
    protected function getErrorTextFromQueueErrorCode($errorCode)
880
    {
881
        $recognizedErrors = [
882
            '723' => "Invalid category",
883
            '911' => "Unable to process - system error",
884
            '913' => "Item/data not found or data not existing in processing host",
885
            '79D' => "Queue identifier has not been assigned for specified office identification",
886
            '91C' => "invalid record locator",
887
            '91F' => "Invalid queue number",
888
            '921' => "target not specified",
889
            '922' => "Targetted queue has wrong queue type",
890
            '926' => "Queue category empty",
891
            '928' => "Queue category not assigned",
892
            '92A' => "Queue category full",
893
        ];
894
895
        $errorMessage = (array_key_exists($errorCode, $recognizedErrors)) ? $recognizedErrors[$errorCode] : '';
896
897
        if ($errorMessage === '') {
898
            $errorMessage = "QUEUE ERROR '" . $errorCode . "' (Error message unavailable)";
899
        }
900
901
        return $errorMessage;
902
    }
903
904
    /**
905
     * @param string $response
906
     * @return \DOMDocument
907
     * @throws Exception when there's a problem loading the message
908
     */
909
    protected function loadDomDocument($response)
910
    {
911
        $domDoc = new \DOMDocument('1.0', 'UTF-8');
912
913
        $loadResult = $domDoc->loadXML($response);
914
        if ($loadResult === false) {
915
            throw new Exception('Could not load response message into DOMDocument');
916
        }
917
918
        return $domDoc;
919
    }
920
921
    /**
922
     * @param string $qualifier
923
     * @return string Result::STATUS_*
924
     */
925
    protected function makeStatusFromErrorQualifier($qualifier)
926
    {
927
        $status = null;
0 ignored issues
show
Unused Code introduced by
$status 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...
928
929
        switch ($qualifier) {
930
            case 'INF':
931
                $status = Result::STATUS_INFO;
932
                break;
933
            case 'WEC':
934
            case 'WZZ': //Mutually defined warning
935
            case 'WA': //Info line Warning - PNR_AddMultiElements
936
            case 'W':
937
                $status = Result::STATUS_WARN;
938
                break;
939
            case 'EC':
940
            case 'X':
941
            case '001': //Air_MultiAvailability
942
                $status = Result::STATUS_ERROR;
943
                break;
944
            case 'O':
945
                $status = Result::STATUS_OK;
946
                break;
947
            case 'ZZZ': //Mutually defined
948
            default:
949
                $status = Result::STATUS_UNKNOWN;
950
                break;
951
        }
952
953
        return $status;
954
    }
955
956
957
    /**
958
     * Make a Xpath-queryable object for an XML string
959
     *
960
     * registers TNS namespace with prefix self::XMLNS_PREFIX
961
     *
962
     * @param string $response
963
     * @return \DOMXPath
964
     * @throws Exception when there's a problem loading the message
965
     */
966
    protected function makeDomXpath($response)
967
    {
968
        $domDoc = $this->loadDomDocument($response);
969
        $domXpath = new \DOMXPath($domDoc);
970
971
        $domXpath->registerNamespace(
972
            self::XMLNS_PREFIX,
973
            $domDoc->documentElement->lookupNamespaceUri(null)
974
        );
975
976
        return $domXpath;
977
    }
978
979
    /**
980
     * Convert a DomNodeList of nodes containing a (potentially partial) error message into a string.
981
     *
982
     * @param \DOMNodeList $errorTextNodeList
983
     * @return string|null
984
     */
985
    protected function makeMessageFromMessagesNodeList($errorTextNodeList)
986
    {
987
        return implode(
988
            ' - ',
989
            array_map(
990
                function ($item) {
991
                    return trim($item->nodeValue);
992
                },
993
                iterator_to_array($errorTextNodeList)
994
            )
995
        );
996
    }
997
998
    /**
999
     * @param SendResult $sendResult
1000
     * @return Result
1001
     */
1002
    protected function makeResultForException($sendResult)
1003
    {
1004
        $result = new Result($sendResult, Result::STATUS_FATAL);
1005
1006
        $result->messages[] = $this->makeMessageFromException($sendResult->exception);
1007
1008
        return $result;
1009
    }
1010
1011
    /**
1012
     * @param \Exception $exception
1013
     * @return Result\NotOk
1014
     * @throws Exception
1015
     */
1016
    protected function makeMessageFromException(\Exception $exception)
1017
    {
1018
        $message = new Result\NotOk();
1019
1020
        if ($exception instanceof \SoapFault) {
1021
            $info = explode('|', $exception->getMessage());
1022
            $message->code = $info[0];
1023
            if (count($info) === 3) {
1024
                $message->level = $info[1];
1025
                $message->text = $info[2];
1026
            }
1027
        }
1028
1029
        return $message;
1030
    }
1031
}
1032