Completed
Branch master (eebe0d)
by Gregorio
02:31 queued 50s
created

AbstractRequest::sendData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 9
cts 9
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 9
nc 1
nop 1
crap 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 27
    public function getHttpMethod()
26
    {
27 27
        return 'POST';
28
    }
29
30
    /**
31
     * @return array
32
     */
33 51
    public function getHeaders()
34
    {
35
        $headers = array(
36
            'Content-Type' => 'application/json'
37 51
        );
38
39 51
        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 51
    public function sendData($data)
52
    {
53 51
        $httpRequest = $this->httpClient->createRequest(
54 51
            $this->getHttpMethod(),
55 51
            $this->getEndpoint() . '.json',
56 51
            $this->getHeaders(),
57
            $data
58 51
        );
59
60 51
        $httpRequest->setAuth($this->getApiKey(), $this->getApiSecret());
61
62 51
        $httpResponse = $httpRequest->send();
63
64 48
        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 36
    protected function fillExistingParameters($data, $map)
75
    {
76 36
        foreach ($map as $key => $parameter) {
77 36
            $value = null;
78 36
            $method = 'get'.ucfirst(Helper::camelCase($parameter));
79 36
            if (method_exists($this, $method)) {
80 36
                $value = $this->$method();
81 36
            } elseif ($this->parameters->has($parameter)) {
82
                $value = $this->parameters->get($parameter);
83
            }
84 36
            if (!is_null($value)) {
85 36
                $data[$key] = $value;
86 36
            }
87 36
        }
88
89 36
        return $data;
90
    }
91
92 48
    protected function createResponse($data)
93
    {
94 48
        return $this->response = new Response($this, $data);
95
    }
96
}
97