Completed
Branch master (eebe0d)
by Gregorio
02:31 queued 50s
created

Response::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 2
crap 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 51
    public function __construct(RequestInterface $request, $data)
19
    {
20 51
        $this->request = $request;
21 51
        $this->rawData = $data;
22 51
        $this->data = array_shift($data);
23 51
    }
24
25
    /**
26
     * Is the response successful?
27
     *
28
     * @return boolean
29
     */
30 42
    public function isSuccessful()
31
    {
32
        // Check for succeeded parameter
33 42
        $succeeded = Arr::get($this->data, 'succeeded');
34 42
        if (!is_null($succeeded)) {
35 24
            return $succeeded == true;
36
        }
37
38
        // Check for state parameter
39 18
        $state = Arr::get($this->data, 'state');
40 18
        if (!is_null($state)) {
41 6
            return $state == 'retained';
42
        }
43
44
        // Check for errors array
45 12
        $errors = Arr::get($this->data, 'errors');
46 12
        if (!is_null($errors)) {
47 6
            return is_array($errors) && !count($errors);
48
        }
49
50
        // Check if it's an indexed array (lists)
51 6
        if (array_values($this->data) === $this->data) {
52 3
            return true;
53
        }
54
55 3
        return false;
56
    }
57
58
    /**
59
     * Raw data
60
     *
61
     * @return array Response raw data
62
     */
63 3
    public function getRawData()
64
    {
65 3
        return $this->rawData;
66
    }
67
68
    /**
69
     * Response Message
70
     *
71
     * @return null|string A response message from the payment gateway
72
     */
73 3
    public function getMessage()
74
    {
75 3
        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 24
    public function getCode()
84
    {
85 24
        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 33
    public function getTransactionReference()
94
    {
95 33
        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 3
    public function getTransactionId()
104
    {
105 3
        return Arr::get($this->data, 'order_id');
106
    }
107
108
    /**
109
     * Amount
110
     *
111
     * @return null|integer The transaction amount
112
     */
113 15
    public function getAmount()
114
    {
115 15
        $amount = Arr::get($this->data, 'amount');
116
117 15
        if (!is_numeric($amount)) {
118 3
            return null;
119
        }
120
121 12
        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 9
    public function getAmountInteger()
130
    {
131 9
        return Arr::get($this->data, 'amount');
132
    }
133
134
    /**
135
     * Payment method token
136
     *
137
     * @return null|string The payment method token
138
     */
139 12
    public function getPaymentMethodToken()
140
    {
141 12
        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 3
    public function getPaymentMethodType()
150
    {
151 3
        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 6
    public function getSinceToken()
160
    {
161 6
        if (is_array($this->data)) {
162 6
            if ($last = array_pop($this->data)) {
163 6
                return Arr::get($last, 'token');
164
            }
165 6
        }
166
167 6
        return null;
168
    }
169
170
}
171