Response::getRedirectUrl()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Omnipay\ZipPay\Message;
4
5
use Omnipay\Common\Message\AbstractResponse;
6
use Omnipay\Common\Message\RequestInterface;
7
8
/**
9
 * Response
10
 */
11
class Response extends AbstractResponse
12
{
13
    protected $headers;
14
    protected $status;
15
16 33
    public function __construct(RequestInterface $request, $data, $headers, $status)
17
    {
18 33
        $this->request = $request;
19 33
        $this->data = $data;
20 33
        $this->headers = $headers;
21 33
        $this->status = $status;
22 33
    }
23
24 24
    public function isSuccessful()
25
    {
26 24
        $statusCode = $this->getCode();
27
28 24
        if (is_null($statusCode)) {
29
            return false;
30
        }
31
32 24
        return $statusCode >= 200 && $statusCode <= 399;
33
    }
34
35 24
    public function getCode()
36
    {
37 24
        return $this->status;
38
    }
39
40
    public function getTransactionReference()
41
    {
42
        return $this->getDataField('id');
43
    }
44
45 30
    public function isRedirect()
46
    {
47 30
        return !empty($this->getRedirectUrl());
48
    }
49
50 30
    public function getRedirectUrl()
51
    {
52 30
        return $this->getDataField('uri');
53
    }
54
55 3
    public function getHeaders()
56
    {
57 3
        return $this->headers;
58
    }
59
60 15
    public function getErrorCode()
61
    {
62 15
        if ($this->isSuccessful()) {
63
            return null;
64
        }
65
66 15
        return $this->getDataField('error.code');
67
    }
68
69
    public function getErrorDetails()
70
    {
71
        if ($this->isSuccessful()) {
72
            return null;
73
        }
74
75
        return $this->getDataField('error.details');
76
    }
77
78 15
    public function getMessage()
79
    {
80 15
        if ($this->isSuccessful()) {
81
            return $this->getDataField('state');
82
        } else {
83 15
            return $this->getDataField('error.message');
84
        }
85
    }
86
87
    protected function getHeader($field)
88
    {
89
        if (empty($this->headers[$field])) {
90
            return null;
91
        }
92
93
        return reset($this->headers[$field]);
94
    }
95
96 30
    protected function getDataField($field)
97
    {
98 30
        if (strpos($field, '.') === false) {
99 30
            return $this->getTopLevelDataField($field);
100
        }
101
102 15
        return $this->getNestedDataField($field);
103
    }
104
105 30
    protected function getTopLevelDataField($field)
106
    {
107 30
        if (isset($this->data[$field])) {
108 3
            return $this->data[$field];
109
        }
110
111 27
        return null;
112
    }
113
114 15
    protected function getNestedDataField($field)
115
    {
116 15
        $levels = explode('.', $field);
117 15
        $data = $this->data;
118 15
        foreach ($levels as $level) {
119 15
            if (!isset($data[$level])) {
120
                return null;
121
            }
122
123 15
            $data = $data[$level];
124 5
        }
125
126 15
        return $data;
127
    }
128
}
129