Passed
Pull Request — master (#481)
by Artem
03:45
created

HandlerValidateFOP   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 31
c 1
b 0
f 0
dl 0
loc 70
ccs 28
cts 28
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A analyze() 0 35 3
A makeStatusForPotentiallyNonExistent() 0 9 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\ResponseHandler\FOP;
24
25
use Amadeus\Client\Exception;
26
use Amadeus\Client\ResponseHandler\StandardResponseHandler;
27
use Amadeus\Client\Result;
28
use Amadeus\Client\Session\Handler\SendResult;
29
30
/**
31
 * HandlerValidateFOP
32
 *
33
 * @package Amadeus\Client\ResponseHandler\FOP
34
 * @author Mike Hernas <[email protected]>
35
 */
36
class HandlerValidateFOP extends StandardResponseHandler
37
{
38
    //General error
39
    const Q_G_ERR = "//m:transmissionError/m:errorOrWarningCodeDetails/m:errorDetails/m:errorCode";
40
    const Q_G_CAT = "//m:transmissionError/m:errorOrWarningCodeDetails/m:errorDetails/m:errorCategory";
41
    const Q_G_MSG = "//m:transmissionError/m:errorWarningDescription/m:freeText";
42
    //Deficient FOP level:
43
    const Q_D_ERR = "//m:fopDescription/m:mopDescription/m:paymentModule/m:mopDetailedData//m:transactionStatus/m:errorOrWarningCodeDetails//m:errorCode";
44
    const Q_D_CAT = "//m:fopDescription/m:mopDescription/m:paymentModule/m:mopDetailedData//m:transactionStatus/m:errorOrWarningCodeDetails//m:errorCategory";
45
    const Q_D_MSG = "//m:fopDescription/m:mopDescription/m:paymentModule/m:mopDetailedData//m:transactionStatus/m:errorWarningDescription//m:freeText";
46
   
47
    /**
48
     * FOP_ValidateFormOfPayment Analyze the result from the message operation and check for any error messages
49
     *
50
     * @param SendResult $response
51
     * @return Result
52
     * @throws Exception
53
     */
54 5
    public function analyze(SendResult $response)
55
    {
56 5
        $analyzeResponse = new Result($response);
57
58 5
        $domXpath = $this->makeDomXpath($response->responseXml);
59
60
        //General error level in the transmissionError location:
61 5
        $errorCodeNodeList = $domXpath->query(self::Q_G_ERR);
62 5
        if ($errorCodeNodeList->length > 0) {
63 5
            $errorCatNode = $domXpath->query(self::Q_G_CAT)->item(0);
64 5
            $analyzeResponse->setStatus($this->makeStatusForPotentiallyNonExistent($errorCatNode));
65
66 5
            $code = $errorCodeNodeList->item(0)->nodeValue;
67 5
            $errorTextNodeList = $domXpath->query(self::Q_G_MSG);
68 5
            $message = $this->makeMessageFromMessagesNodeList($errorTextNodeList);
69
70 5
            $analyzeResponse->messages[] = new Result\NotOk($code, trim($message), 'general');
71 2
        }
72
73
        //Deficient FOP level errors:
74 5
        $errorCodeNodeList = $domXpath->query(self::Q_D_ERR);
75
76 5
        if ($errorCodeNodeList->length > 0) {
77 5
            $errorCatNode = $domXpath->query(self::Q_D_CAT)->item(0);
78 5
            $analyzeResponse->setStatus($this->makeStatusForPotentiallyNonExistent($errorCatNode));
79
80 5
            $code = $errorCodeNodeList->item(0)->nodeValue;
81
82 5
            $errorTextNodeList = $domXpath->query(self::Q_D_MSG);
83 5
            $message = $this->makeMessageFromMessagesNodeList($errorTextNodeList);
84
85 5
            $analyzeResponse->messages[] = new Result\NotOk($code, trim($message), 'deficient_fop');
86 2
        }
87
88 5
        return $analyzeResponse;
89
    }
90
91
    /**
92
     * Make status from a category DOMNode or default status.
93
     *
94
     * @param \DOMNode|null $errorCatNode
95
     * @return string
96
     */
97 5
    protected function makeStatusForPotentiallyNonExistent($errorCatNode)
98
    {
99 5
        if ($errorCatNode instanceof \DOMNode) {
100 5
            $status = $this->makeStatusFromErrorQualifier($errorCatNode->nodeValue);
101 2
        } else {
102 5
            $status = Result::STATUS_ERROR;
103
        }
104
105 5
        return $status;
106
    }
107
}
108