Completed
Push — master ( b41f42...905377 )
by Gregorio
02:32
created

AbstractRequest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 65.12%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 8
dl 0
loc 88
ccs 28
cts 43
cp 0.6512
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
getEndpoint() 0 1 ?
A getHttpMethod() 0 4 1
A getHeaders() 0 8 1
A sendData() 0 15 1
B fillExistingParameters() 0 17 5
A createResponse() 0 4 1
1
<?php
2
3
namespace Omnipay\Spreedly\Message;
4
5
use Omnipay\Common\Helper;
6
use Omnipay\Common\Message\AbstractRequest as BaseAbstractRequest;
7
use Omnipay\Spreedly\Concerns\HasSpreedlyConfig;
8
9
abstract class AbstractRequest extends BaseAbstractRequest
10
{
11
    use HasSpreedlyConfig;
12
13
    /**
14
     * @var string
15
     */
16
    protected $endpoint = 'https://core.spreedly.com/v1/';
17
18
    /**
19
     * Get HTTP Method.
20
     *
21
     * This is nearly always POST but can be over-ridden in sub classes.
22
     *
23
     * @return string
24
     */
25 18
    public function getHttpMethod()
26
    {
27 18
        return 'POST';
28
    }
29
30
    /**
31
     * @return array
32
     */
33 36
    public function getHeaders()
34
    {
35
        $headers = array(
36
            'Content-Type' => 'application/json'
37 36
        );
38
39 36
        return $headers;
40
    }
41
42
    /**
43
     * @return string
44
     */
45
    abstract public function getEndpoint();
46
47
    /**
48
     * @param array|null $data
49
     * @return \Omnipay\Common\Message\ResponseInterface|Response
50
     */
51 36
    public function sendData($data)
52
    {
53 36
        $httpRequest = $this->httpClient->createRequest(
54 36
            $this->getHttpMethod(),
55 36
            $this->getEndpoint() . '.json',
56 36
            $this->getHeaders(),
57
            $data
58 36
        );
59
60 36
        $httpRequest->setAuth($this->getApiKey(), $this->getApiSecret());
61
62 36
        $httpResponse = $httpRequest->send();
63
64 34
        return $this->createResponse($httpResponse->json());
65
    }
66
67
    /**
68
     * Map data with existing parameters
69
     *
70
     * @param array $data
71
     * @param array $map
72
     * @return array
73
     */
74 24
    protected function fillExistingParameters($data, $map)
75
    {
76 24
        foreach ($map as $key => $parameter) {
77 24
            $value = null;
78 24
            $method = 'get'.ucfirst(Helper::camelCase($parameter));
79 24
            if (method_exists($this, $method)) {
80 24
                $value = $this->$method();
81 24
            } elseif ($this->parameters->has($parameter)) {
82
                $value = $this->parameters->get($parameter);
83
            }
84 24
            if (!is_null($value)) {
85 24
                $data[$key] = $value;
86 24
            }
87 24
        }
88
89 24
        return $data;
90
    }
91
92 34
    protected function createResponse($data)
93
    {
94 34
        return $this->response = new Response($this, $data);
95
    }
96
}
97