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

AbstractRequest::sendData()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2.0185

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 10
cts 12
cp 0.8333
rs 9.6
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2.0185
1
<?php
2
3
namespace Omnipay\ZipPay\Message;
4
5
use Omnipay\Common\Message\AbstractRequest as BaseAbstractRequest;
6
7
/**
8
 * Abstract Request
9
 *
10
 */
11
abstract class AbstractRequest extends BaseAbstractRequest
12
{
13
    protected $liveEndpoint = 'https://api.zipmoney.com.au/merchant/v1';
14
    protected $testEndpoint = 'https://api.sandbox.zipmoney.com.au/merchant/v1';
15
16 15
    public function getKey()
17
    {
18 15
        return $this->getParameter('key');
19
    }
20
21 60
    public function setKey($value)
22
    {
23 60
        return $this->setParameter('key', $value);
24
    }
25
26 45
    public function getApiKey()
27
    {
28 45
        return $this->getParameter('apiKey');
29
    }
30
31 60
    public function setApiKey($value)
32
    {
33 60
        return $this->setParameter('apiKey', $value);
34
    }
35
36 30
    public function getHeaders()
37
    {
38
        return [
39 30
            'Content-Type' => 'application/json',
40 30
            'Authorization' => 'Bearer ' . $this->getApiKey(),
41 30
            'Zip-Version' => '2017-03-01',
42 10
        ];
43
    }
44
45 30
    public function sendData($data)
46
    {
47 30
        $headers = $this->getHeaders();
48
49 30
        $url = $this->getEndpoint();
50 30
        $body = null;
51 30
        if ($this->getHttpMethod() === 'GET') {
52
            $url = $this->getEndpoint().'?'.http_build_query($data, '', '&');
53
        } else {
54 30
            $body = json_encode($data);
55
        }
56
57 30
        $response = $this->httpClient->request($this->getHttpMethod(), $url, $headers, $body);
58
59 30
        $responseHeaders = $response->getHeaders();
60
61 30
        $data = json_decode($response->getBody(), true);
62
63 30
        return $this->createResponse($data, $responseHeaders, $response->getStatusCode());
64
    }
65
66
    public function getHttpMethod()
67
    {
68
        return 'GET';
69
    }
70
71 30
    protected function getBaseData()
72
    {
73 30
        return [];
74
    }
75
76 30
    protected function getEndpoint()
77
    {
78 30
        return $this->getTestMode() ? $this->testEndpoint : $this->liveEndpoint;
79
    }
80
81
    protected function createResponse($data, $headers = [], $status = 404)
82
    {
83
        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...
84
    }
85
}
86