Response   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 170
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 57.69%

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 2
dl 0
loc 170
ccs 45
cts 78
cp 0.5769
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getRawData() 0 4 1
A getMessage() 0 4 1
A getCode() 0 4 1
A getTransactionReference() 0 4 1
A getTransactionId() 0 4 1
A getAmount() 0 10 2
A getAmountInteger() 0 4 1
A getPaymentMethodToken() 0 4 1
A getPaymentMethodType() 0 4 1
A getSinceToken() 0 10 3
B isSuccessful() 0 27 5
A getErrors() 0 4 1
1
<?php
2
3
namespace Omnipay\Spreedly\Message;
4
5
use Omnipay\Common\Message\AbstractResponse;
6
use Omnipay\Common\Message\RequestInterface;
7
use Omnipay\Spreedly\Arr;
8
9
class Response extends AbstractResponse
10
{
11
    /**
12
     * The raw data contained in the response.
13
     *
14
     * @var mixed
15
     */
16
    protected $rawData;
17
18 40
    public function __construct(RequestInterface $request, $data)
19
    {
20 40
        $this->request = $request;
21 40
        $this->rawData = $data;
22 40
        $this->data = array_shift($data);
23 40
    }
24
25
    /**
26
     * Is the response successful?
27
     *
28
     * @return boolean
29
     */
30 34
    public function isSuccessful()
31
    {
32
        // Check for succeeded parameter
33 34
        $succeeded = Arr::get($this->data, 'succeeded');
34 34
        if (!is_null($succeeded)) {
35 22
            return $succeeded == true;
36
        }
37
38
        // Check for state parameter
39 12
        $state = Arr::get($this->data, 'state');
40 12
        if (!is_null($state)) {
41 4
            return $state == 'retained';
42
        }
43
44
        // Check for errors array
45 8
        $errors = Arr::get($this->data, 'errors');
46 8
        if (is_array($errors)) {
47 4
            return !count($errors);
48
        }
49
50
        // Check if it's an indexed array (lists)
51 4
        if (array_values($this->data) === $this->data) {
52 2
            return true;
53
        }
54
55 2
        return false;
56
    }
57
58
    /**
59
     * Raw data
60
     *
61
     * @return array Response raw data
62
     */
63 2
    public function getRawData()
64
    {
65 2
        return $this->rawData;
66
    }
67
68
    /**
69
     * Response Message
70
     *
71
     * @return null|string A response message from the payment gateway
72
     */
73 2
    public function getMessage()
74
    {
75 2
        return Arr::get($this->data, 'message');
76
    }
77
78
    /**
79
     * Response code
80
     *
81
     * @return null|string A response code from the payment gateway
82
     */
83 22
    public function getCode()
84
    {
85 22
        return Arr::get($this->data, 'state');
86
    }
87
88
    /**
89
     * Gateway Reference
90
     *
91
     * @return null|string A reference provided by the gateway to represent this transaction
92
     */
93 28
    public function getTransactionReference()
94
    {
95 28
        return Arr::get($this->data, 'token');
96
    }
97
98
    /**
99
     * Get the transaction ID as generated by the merchant website.
100
     *
101
     * @return string
102
     */
103 2
    public function getTransactionId()
104
    {
105 2
        return Arr::get($this->data, 'order_id');
106
    }
107
108
    /**
109
     * Amount
110
     *
111
     * @return null|integer The transaction amount
112
     */
113 12
    public function getAmount()
114
    {
115 12
        $amount = Arr::get($this->data, 'amount');
116
117 12
        if (!is_numeric($amount)) {
118 2
            return null;
119
        }
120
121 10
        return strval(floor($amount / 100)) . '.' . strval($amount % 100);
122
    }
123
124
    /**
125
     * Integer Amount
126
     *
127
     * @return null|integer The transaction integer amount
128
     */
129 6
    public function getAmountInteger()
130
    {
131 6
        return Arr::get($this->data, 'amount');
132
    }
133
134
    /**
135
     * Payment method token
136
     *
137
     * @return null|string The payment method token
138
     */
139 14
    public function getPaymentMethodToken()
140
    {
141 14
        return Arr::get($this->data, 'payment_method.token', Arr::get($this->rawData, 'payment_method.token'));
142
    }
143
144
    /**
145
     * Payment method type
146
     *
147
     * @return null|string The payment method type
148
     */
149 2
    public function getPaymentMethodType()
150
    {
151 2
        return Arr::get($this->data, 'payment_method.payment_method_type', Arr::get($this->rawData, 'payment_method.payment_method_type'));
152
    }
153
154
    /**
155
     * Since token
156
     *
157
     * @return null|string The since token of a paginated list
158
     */
159 4
    public function getSinceToken()
160
    {
161 4
        if (is_array($this->data)) {
162 4
            if ($last = array_pop($this->data)) {
163 4
                return Arr::get($last, 'token');
164
            }
165 4
        }
166
167 4
        return null;
168
    }
169
170
    /**
171
     * @return array|null
172
     */
173
    public function getErrors()
174
    {
175
        return Arr::get($this->data, 'errors');
176
    }
177
178
}
179