Passed
Pull Request — master (#6)
by
unknown
11:25
created

AbstractRequest::validate()   C

Complexity

Conditions 13
Paths 12

Size

Total Lines 35
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 182

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