HandlerCreateFormOfPayment   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 110
Duplicated Lines 47.27 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 4
dl 52
loc 110
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B analyze() 42 66 5
A makeStatusForPotentiallyNonExistent() 10 10 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
 * HandlerCreateFormOfPayment
32
 *
33
 * @package Amadeus\Client\ResponseHandler\FOP
34
 * @author Dieter Devlieghere <[email protected]>
35
 */
36
class HandlerCreateFormOfPayment 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
    //FP level error
43
    const Q_F_ERR = "//m:fopDescription//m:fpElementError/m:errorOrWarningCodeDetails/m:errorDetails/m:errorCode";
44
    const Q_F_CAT = "//m:fopDescription//m:fpElementError/m:errorOrWarningCodeDetails/m:errorDetails/m:errorCategory";
45
    const Q_F_MSG = "//m:fopDescription//m:fpElementError/m:errorWarningDescription/m:freeText";
46
    //Deficient FOP level:
47
    const Q_D_ERR = "//m:fopDescription/m:mopDescription/m:mopElementError/m:errorOrWarningCodeDetails//m:errorCode";
48
    const Q_D_CAT = "//m:fopDescription/m:mopDescription/m:mopElementError/m:errorOrWarningCodeDetails//m:errorCategory";
49
    const Q_D_MSG = "//m:fopDescription/m:mopDescription/m:mopElementError/m:errorWarningDescription/m:freeText";
50
    //Authorization failure:
51
    const Q_A_ERR = "//m:fopDescription/m:mopDescription/m:paymentModule//m:paymentStatusError/m:errorOrWarningCodeDetails//m:errorCode";
52
    const Q_A_CAT = "//m:fopDescription/m:mopDescription/m:paymentModule//m:paymentStatusError/m:errorOrWarningCodeDetails//m:errorCategory";
53
    const Q_A_MSG = "//m:fopDescription/m:mopDescription/m:paymentModule//m:paymentStatusError/m:errorWarningDescription/m:freeText";
54
55
    /**
56
     * FOP_CreateFormOfPayment Analyze the result from the message operation and check for any error messages
57
     *
58
     * @param SendResult $response
59
     * @return Result
60
     * @throws Exception
61
     */
62
    public function analyze(SendResult $response)
63
    {
64
        $analyzeResponse = new Result($response);
65
66
        $domXpath = $this->makeDomXpath($response->responseXml);
67
68
        //General error level in the transmissionError location:
69
        $errorCodeNodeList = $domXpath->query(self::Q_G_ERR);
70
71 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...
72
            $errorCatNode = $domXpath->query(self::Q_G_CAT)->item(0);
73
            $analyzeResponse->setStatus($this->makeStatusForPotentiallyNonExistent($errorCatNode));
74
75
            $code = $errorCodeNodeList->item(0)->nodeValue;
76
            $errorTextNodeList = $domXpath->query(self::Q_G_MSG);
77
            $message = $this->makeMessageFromMessagesNodeList($errorTextNodeList);
78
79
            $analyzeResponse->messages[] = new Result\NotOk($code, trim($message), 'general');
80
        }
81
82
        //FP level errors via the fopDescription/fpElementError data:
83
        $errorCodeNodeList = $domXpath->query(self::Q_F_ERR);
84
85 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...
86
            $errorCatNode = $domXpath->query(self::Q_F_CAT)->item(0);
87
            $analyzeResponse->setStatus($this->makeStatusForPotentiallyNonExistent($errorCatNode));
88
89
            $code = $errorCodeNodeList->item(0)->nodeValue;
90
            $errorTextNodeList = $domXpath->query(self::Q_F_MSG);
91
            $message = $this->makeMessageFromMessagesNodeList($errorTextNodeList);
92
93
            $analyzeResponse->messages[] = new Result\NotOk($code, trim($message), 'fp');
94
        }
95
96
        //Deficient FOP level errors:
97
        $errorCodeNodeList = $domXpath->query(self::Q_D_ERR);
98
99 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...
100
            $errorCatNode = $domXpath->query(self::Q_D_CAT)->item(0);
101
            $analyzeResponse->setStatus($this->makeStatusForPotentiallyNonExistent($errorCatNode));
102
103
            $code = $errorCodeNodeList->item(0)->nodeValue;
104
105
            $errorTextNodeList = $domXpath->query(self::Q_D_MSG);
106
            $message = $this->makeMessageFromMessagesNodeList($errorTextNodeList);
107
108
            $analyzeResponse->messages[] = new Result\NotOk($code, trim($message), 'deficient_fop');
109
        }
110
111
        //authorization failure:
112
        $errorCodeNodeList = $domXpath->query(self::Q_A_ERR);
113
114 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...
115
            $errorCatNode = $domXpath->query(self::Q_A_CAT)->item(0);
116
            $analyzeResponse->setStatus($this->makeStatusForPotentiallyNonExistent($errorCatNode));
117
118
            $code = $errorCodeNodeList->item(0)->nodeValue;
119
120
            $errorTextNodeList = $domXpath->query(self::Q_A_MSG);
121
            $message = $this->makeMessageFromMessagesNodeList($errorTextNodeList);
122
123
            $analyzeResponse->messages[] = new Result\NotOk($code, trim($message), 'authorization_failure');
124
        }
125
126
        return $analyzeResponse;
127
    }
128
129
    /**
130
     * Make status from a category DOMNode or default status.
131
     *
132
     * @param \DOMNode|null $errorCatNode
133
     * @return string
134
     */
135 View Code Duplication
    protected function makeStatusForPotentiallyNonExistent($errorCatNode)
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...
136
    {
137
        if ($errorCatNode instanceof \DOMNode) {
138
            $status = $this->makeStatusFromErrorQualifier($errorCatNode->nodeValue);
139
        } else {
140
            $status = Result::STATUS_ERROR;
141
        }
142
143
        return $status;
144
    }
145
}
146