fillCardDetails()   B
last analyzed

Complexity

Conditions 8
Paths 96

Size

Total Lines 55
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 38
c 1
b 0
f 0
dl 0
loc 55
rs 8.0675
cc 8
nc 96
nop 2

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * amadeus-ws-client
5
 *
6
 * Copyright 2015 Amadeus Benelux NV
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 * http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 *
20
 * @package Amadeus
21
 * @license https://opensource.org/licenses/Apache-2.0 Apache 2.0
22
 */
23
24
namespace Amadeus\Client\ResponseHandler\PAY;
25
26
use Amadeus\Client\ResponseHandler\StandardResponseHandler;
27
use stdClass;
28
29
/**
30
 * VirtualCardDetailsBaseResponseHandler
31
 *
32
 * @package Amadeus\Client\ResponseHandler\PAY
33
 * @author Konstantin Bogomolov <[email protected]>
34
 */
35
abstract class AbstractCardDetailsResponseHandler extends StandardResponseHandler
36
{
37
    protected function fillCardDetails($analyzeResponse, $domXpath)
38
    {
39
        $cardNumber = $domXpath->query('//fop:PrimaryAccountNumber');
40
        if ($cardNumber->length > 0) {
41
            $analyzeResponse->response->Success->VirtualCard->Card->PrimaryAccountNumber =
42
                $cardNumber->item(0)->nodeValue;
43
        }
44
45
        $cvvNumber = $domXpath->query('//fop:CVV');
46
        if ($cvvNumber->length > 0) {
47
            $analyzeResponse->response->Success->VirtualCard->Card->CVV =
48
                $cvvNumber->item(0)->nodeValue;
49
        }
50
51
        $address = $domXpath->query('//fop:AddressVerificationSystemValue');
52
        if ($address->length > 0) {
53
            if (empty($analyzeResponse->response->Success->VirtualCard->Card->AddressVerificationSystemValue)) {
54
                $analyzeResponse->response->Success->VirtualCard->Card->AddressVerificationSystemValue = new stdClass();
55
            }
56
            $analyzeResponse->response->Success->VirtualCard->Card->AddressVerificationSystemValue->Line =
57
                $address->item(0)->nodeValue;
58
            $analyzeResponse->response->Success->VirtualCard->Card->AddressVerificationSystemValue->CityName =
59
                $address->item(0)->getAttribute('CityName');
60
            $analyzeResponse->response->Success->VirtualCard->Card->AddressVerificationSystemValue->PostalCode =
61
                $address->item(0)->getAttribute('PostalCode');
62
            $analyzeResponse->response->Success->VirtualCard->Card->AddressVerificationSystemValue->Country =
63
                $address->item(0)->getAttribute('Country');
64
        }
65
66
        $limitations = $domXpath->query('//pay:Limitations');
67
        if ($limitations->length > 0) {
68
            $limitationsNodesArrayData = $this->nodeToArray($limitations->item(0));
69
            $analyzeResponse->response->Success->VirtualCard->AllowedTransactions =
70
                $limitationsNodesArrayData['pay:AllowedTransactions'][0]['Maximum'];
71
            $analyzeResponse->response->Success->VirtualCard->ValidityPeriod =
72
                $limitationsNodesArrayData['pay:ValidityPeriod'][0]['EndDate'];
73
        }
74
75
        $balance = $domXpath->query('//pay:Value[@Type=\'AvailableBalance\']');
76
77
        if ($balance->length === 0) {
78
            // OnCard = amount actually on card, but some responses may not contain such type
79
            $balance = $domXpath->query('//pay:Value[@Type=\'OnCard\']');
80
        }
81
82
        if ($balance->length > 0) {
83
            $analyzeResponse->response->Success->VirtualCard->Amount =
84
                $balance->item(0)->getAttribute('Amount');
85
            $analyzeResponse->response->Success->VirtualCard->DecimalPlaces =
86
                $balance->item(0)->getAttribute('DecimalPlaces');
87
            $analyzeResponse->response->Success->VirtualCard->CurrencyCode =
88
                $balance->item(0)->getAttribute('CurrencyCode');
89
        }
90
91
        return $analyzeResponse;
92
    }
93
94
    /**
95
     * Using this method because otherwise uploaded XML used for unit-tests for unknown reason does not recognize:
96
     * $limitations = $domXpath->query('//pay:Limitations');
97
     * $limitationsNodes = $limitations->item(0)->childNodes;
98
     * $limitationsNodes->item(0)->getAttribute('Maximum'); <<<--- Call to undefined method DOMText::getAttribute()
99
     *
100
     * So using this nodeToArray() method which works in both unit-tests and real SOAP client
101
     * https://www.php.net/manual/en/class.domdocument.php#101014
102
     * @param $node
103
     * @return array
104
     */
105
    protected function nodeToArray($node): array
106
    {
107
        $result = [];
108
109
        if ($node->hasAttributes()) {
110
            foreach ($node->attributes as $attr) {
111
                $result[$attr->nodeName] = $attr->nodeValue;
112
            }
113
        }
114
115
        if ($node->hasChildNodes()) {
116
            foreach ($node->childNodes as $childNode) {
117
                if ($childNode->nodeType !== XML_TEXT_NODE) {
118
                    $result[$childNode->nodeName][] = $this->nodeToArray($childNode);
119
                }
120
            }
121
        }
122
123
        return $result;
124
    }
125
}
126