Passed
Push — master ( 756e4d...c425d0 )
by Steven
02:49
created

BaseRequest::validateParams()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Digitonic\IexCloudSdk\Requests;
4
5
use Digitonic\IexCloudSdk\Contracts\IEXCloud;
6
use Digitonic\IexCloudSdk\Exceptions\WrongData;
7
use GuzzleHttp\Psr7\Request;
8
use Illuminate\Support\Collection;
9
10
abstract class BaseRequest
11
{
12
    const ENDPOINT = '';
13
14
    protected $method = 'GET';
15
16
    protected $payload = [];
17
18
    protected $api;
19
20
    protected $symbol = '';
21
22 129
    public function __construct(IEXCloud $api)
23
    {
24 129
        $this->api = $api;
25 129
    }
26
27 14
    public function setSymbol(string $symbol): self
28
    {
29 14
        $this->symbol = $symbol;
30
31 14
        return $this;
32
    }
33
34 80
    public function send(): Collection
35
    {
36 80
        $this->validateParams();
37
38 54
        $request = new Request($this->method, $this->getFullEndpoint(), [], json_encode($this->payload));
39
40 54
        $response = $this->api->send($request);
41
42 54
        return collect(json_decode($response->getBody()->getContents()));
43
    }
44
45 5
    public function get(): Collection
46
    {
47 5
        $this->method = 'GET';
48
49 5
        return $this->send();
50
    }
51
52
    public function post(): Collection
53
    {
54
        $this->method = 'POST';
55
56
        if (empty($this->payload)) {
57
            throw WrongData::invalidValuesProvided('Payload required to perform a POST request');
58
        }
59
60
        return $this->send();
61
    }
62
63
    abstract protected function getFullEndpoint(): string;
64
65 25
    protected function validateParams()
66
    {
67 25
        return true;
68
    }
69
}
70