Completed
Pull Request — master (#2)
by Sujip
14:35
created

AbstractRequest::getHeaders()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
ccs 0
cts 0
cp 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Omnipay\ZipPay\Message;
4
5
use Omnipay\Common\Message\AbstractRequest as BaseAbstractRequest;
6
use Omnipay\ZipPay\Helper\Uuid;
7
8
/**
9
 * Abstract Request
10
 *
11
 */
12
abstract class AbstractRequest extends BaseAbstractRequest
13
{
14
    /**
15
     * @var string
16 15
     */
17
    protected $liveEndpoint = 'https://api.zipmoney.com.au/merchant/v1';
18 15
    /**
19
     * @var string
20
     */
21 60
    protected $testEndpoint = 'https://api.sandbox.zipmoney.com.au/merchant/v1';
22
23 60
    /**
24
     * @return string
25
     */
26 45
    public function getKey()
27
    {
28 45
        return $this->getParameter('key');
29
    }
30
31 60
    /**
32
     * @param $value
33 60
     * @return string
34
     */
35
    public function setKey($value)
36 30
    {
37
        return $this->setParameter('key', $value);
38
    }
39 30
40 30
    /**
41 30
     * @return string
42 10
     */
43
    public function getApiKey()
44
    {
45 30
        return $this->getParameter('apiKey');
46
    }
47 30
48
    /**
49 30
     * @param $value
50 30
     * @return string
51 30
     */
52
    public function setApiKey($value)
53
    {
54 30
        return $this->setParameter('apiKey', $value);
55
    }
56
57 30
    /**
58
     * Get Idempotency Key
59 30
     * @return string Idempotency Key
60
     */
61 30
    public function getIdempotencyKey()
62
    {
63 30
        return $this->getParameter('idempotencyKey') ?: Uuid::create();
64
    }
65
66
    /**
67
     * Set Idempotency Key
68
     * @param  string $value Idempotency Key
69
     */
70
    public function setIdempotencyKey($value)
71 30
    {
72
        return $this->setParameter('idempotencyKey', $value);
73 30
    }
74
75
    /**
76 30
     * @param $value
77
     * @return string
78 30
     */
79
    public function setSSLCertificatePath($value)
80
    {
81
        return $this->setParameter('sslCertificatePath', $value);
82
    }
83
84
    /**
85
     * @return string
86
     */
87
    public function getSSLCertificatePath()
88
    {
89
        return $this->getParameter('sslCertificatePath');
90
    }
91
92
    /**
93
     * @return array
94
     */
95
    public function getHeaders()
96
    {
97
        return [
98
            'Content-Type' => 'application/json',
99
            'Authorization' => 'Bearer ' . $this->getApiKey(),
100
            'Zip-Version' => '2017-03-01',
101
            'Idempotency-Key' => $this->getIdempotencyKey(),
102
        ];
103
    }
104
105
    /**
106
     * @param $data
107
     * @return Response
108
     */
109
    public function sendData($data)
110
    {
111
        if ($this->getSSLCertificatePath()) {
112
            $this->httpClient->setSslVerification($this->getSSLCertificatePath());
113
        }
114
115
        $endPoint = $this->getEndpoint();
116
117
        if ($this->getHttpMethod() === 'GET') {
118
            $endPoint = $endPoint . '?' . http_build_query($data, '', '&');
119
        }
120
121
        $response = $this->httpClient->createRequest(
122
            $this->getHttpMethod(),
123
            $endPoint,
124
            $this->getHeaders(),
125
            json_encode($data)
126
        )->send();
127
128
        return $this->createResponse($response->json(), $response->getHeaders(), $response->getStatusCode());
129
    }
130
131
    public function getHttpMethod()
132
    {
133
        return 'GET';
134
    }
135
136
    protected function getBaseData()
137
    {
138
        return [];
139
    }
140
141
    /**
142
     * @return string
143
     */
144
    protected function getEndpoint()
145
    {
146
        return $this->getTestMode() ? $this->testEndpoint : $this->liveEndpoint;
147
    }
148
149
    /**
150
     * @param $data
151
     * @param array $headers
152
     * @param $status
153
     * @return Response
154
     */
155
    protected function createResponse($data, $headers = [], $status = 404)
156
    {
157
        return $this->response = new Response($this, $data, $headers);
0 ignored issues
show
Bug introduced by
The call to Response::__construct() misses a required argument $status.

This check looks for function calls that miss required arguments.

Loading history...
158
    }
159
}
160