AbstractRequest::validate()   C
last analyzed

Complexity

Conditions 13
Paths 12

Size

Total Lines 35
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 13.0976

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 13
eloc 25
c 1
b 0
f 0
nc 12
nop 1
dl 0
loc 35
ccs 22
cts 24
cp 0.9167
crap 13.0976
rs 6.6166

How to fix   Complexity   

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
namespace  Omnipay\Moneris\Message;
4
5
use Omnipay\Common\Exception\InvalidRequestException;
6
7
abstract class AbstractRequest extends \Omnipay\Common\Message\AbstractRequest
8
{
9
    const MAIL_ORDER_TELEPHONE_ORDER_SINGLE = 1;
10
    const MAIL_ORDER_TELEPHONE_ORDER_RECURRING = 2;
11
    const MAIL_ORDER_TELEPHONE_ORDER_INSTALMENT = 3;
12
    const MAIL_ORDER_TELEPHONE_ORDER_UNKNOWN_CLASSIFICATION = 4;
13
    const AUTHENTICATED_E_COMMERCE_TRANSACTION_VBV = 5;
14
    const NON_AUTHENTICATED_E_COMMERCE_TRANSACTION_VBV = 6;
15
    const SSL_ENABLED_MERCHANT = 7;
16
17
    /**
18
     * Allowable values for the e-commerce transaction category being processed.
19
     *
20
     * @var array
21
     * @link https://developer.moneris.com/en/Documentation/NA/E-Commerce%20Solutions/API/Purchase
22
     */
23
    const ECOMMERCE_INDICATORS = [
24
        self::MAIL_ORDER_TELEPHONE_ORDER_SINGLE,
25
        self::MAIL_ORDER_TELEPHONE_ORDER_RECURRING,
26
        self::MAIL_ORDER_TELEPHONE_ORDER_INSTALMENT,
27
        self::MAIL_ORDER_TELEPHONE_ORDER_UNKNOWN_CLASSIFICATION,
28
        self::AUTHENTICATED_E_COMMERCE_TRANSACTION_VBV,
29
        self::NON_AUTHENTICATED_E_COMMERCE_TRANSACTION_VBV,
30
        self::SSL_ENABLED_MERCHANT,
31
    ];
32
33
    public $testEndpoint = 'https://esqa.moneris.com:443/gateway2/servlet/MpgRequest';
34
    public $liveEndpoint = 'https://www3.moneris.com:443/gateway2/servlet/MpgRequest';
35
36 45
    public function getEndpoint()
37
    {
38 45
        return $this->getTestMode() ? $this->testEndpoint : $this->liveEndpoint;
39
    }
40
41 72
    public function getMerchantId()
42
    {
43 72
        return $this->getParameter('merchantId');
44
    }
45
46 90
    public function setMerchantId($value)
47
    {
48 90
        return $this->setParameter('merchantId', $value);
49
    }
50
51 72
    public function getMerchantKey()
52
    {
53 72
        return $this->getParameter('merchantKey');
54
    }
55
56 90
    public function setMerchantKey($value)
57
    {
58 90
        return $this->setParameter('merchantKey', $value);
59
    }
60
61 66
    public function getCryptType()
62
    {
63 66
        return $this->getParameter('cryptType');
64
    }
65
66 123
    public function setCryptType($value)
67
    {
68 123
        return $this->setParameter('cryptType', $value);
69
    }
70
71 30
    public function getPaymentMethod()
72
    {
73 30
        return $this->getParameter('paymentMethod');
74
    }
75
76 48
    public function setPaymentMethod($value)
77
    {
78 48
        return $this->setParameter('paymentMethod', $value);
79
    }
80
81 3
    public function getPaymentProfile()
82
    {
83 3
        return $this->getParameter('paymentProfile');
84
    }
85
86 3
    public function setPaymentProfile($value)
87
    {
88 3
        return $this->setParameter('paymentProfile', $value);
89
    }
90
91 21
    public function getOrderNumber()
92
    {
93 21
        return $this->getParameter('orderNumber');
94
    }
95
96 51
    public function setOrderNumber($value)
97
    {
98 51
        return $this->setParameter('orderNumber', $value);
99
    }
100
101 45
    protected function getHttpMethod()
102
    {
103 45
        return 'POST';
104
    }
105
106
    /**
107
     * Validate the request.
108
     *
109
     * @param string ... a variable length list of required parameters
110
     * @throws InvalidRequestException
111
     * @see Omnipay\Common\ParametersTrait::validate()
112
     */
113 87
    public function validate(...$args)
114
    {
115 87
        foreach ($args as $key) {
116 87
            $value = $this->parameters->get($key);
117
118
            switch ($key) {
119 87
                case 'orderNumber':
120 48
                    if (! isset($value)) {
121 3
                        throw new InvalidRequestException("The $key parameter is required");
122
                    } elseif (empty($value)) {
123 3
                        throw new InvalidRequestException("The $key parameter cannot be empty");
124 42
                    } elseif (strlen($value) > 50) {
125
                        throw new InvalidRequestException("The $key parameter cannot be longer than 50 characters");
126
                    }
127 42
                    break;
128
129 87
                case 'cryptType':
130 33
                    if (! isset($value)) {
131 3
                        throw new InvalidRequestException("The $key parameter is required");
132 30
                    } elseif (! in_array($value, self::ECOMMERCE_INDICATORS)) {
133 3
                        throw new InvalidRequestException("The $key is invalid");
134
                    }
135 27
                    break;
136
137 87
                case 'description':
138 24
                    if (isset($value) && strlen($value) > 20) {
139 3
                        throw new InvalidRequestException("The $key parameter cannot be longer than 20 characters");
140
                    }
141 21
                    break;
142
143
                default:
144 87
                    if (! isset($value)) {
145 12
                        throw new InvalidRequestException("The $key parameter is required");
146
                    }
147 84
                    break;
148
            }
149
        }
150 66
    }
151
152 45
    public function sendData($data)
153
    {
154
        $headers = [
155 45
            'Content-Type' => 'application/xml',
156
        ];
157
158 45
        $httpResponse = $this->httpClient->request($this->getHttpMethod(), $this->getEndpoint(), $headers, $data);
159
160
        try {
161 45
            $xmlResponse = simplexml_load_string($httpResponse->getBody()->getContents());
162 3
        } catch (\Exception $e) {
163 3
            $xmlResponse = (string) $httpResponse->getBody(true);
0 ignored issues
show
Unused Code introduced by
The call to Psr\Http\Message\MessageInterface::getBody() has too many arguments starting with true. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

163
            $xmlResponse = (string) $httpResponse->/** @scrutinizer ignore-call */ getBody(true);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
164
        }
165
166 45
        return $this->response = new Response($this, $xmlResponse);
167
    }
168
}
169