Completed
Push — master ( 596f10...f7c3da )
by
unknown
04:06
created

Response   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Test Coverage

Coverage 74.07%

Importance

Changes 0
Metric Value
wmc 24
lcom 2
cbo 1
dl 0
loc 118
ccs 40
cts 54
cp 0.7407
rs 10
c 0
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A isSuccessful() 0 10 3
A getCode() 0 4 1
A getTransactionReference() 0 4 1
A isRedirect() 0 4 1
A getRedirectUrl() 0 4 1
A getHeaders() 0 4 1
A getErrorCode() 0 8 2
A getErrorDetails() 0 8 2
A getMessage() 0 8 2
A getHeader() 0 8 2
A getDataField() 0 8 2
A getTopLevelDataField() 0 8 2
A getNestedDataField() 0 14 3
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