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

Batch::getFullEndpoint()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 11
cts 11
cp 1
rs 9.552
c 0
b 0
f 0
cc 4
nc 6
nop 0
crap 4
1
<?php
2
3
namespace Digitonic\IexCloudSdk\Stocks;
4
5
use Digitonic\IexCloudSdk\Contracts\IEXCloud;
6
use Digitonic\IexCloudSdk\Exceptions\WrongData;
7
use Digitonic\IexCloudSdk\Requests\BaseRequest;
8
9
class Batch extends BaseRequest
10
{
11
    const ENDPOINT = 'stock/{symbol}/batch?';
12
13
    protected $symbol = 'market';
14
15
    /**
16
     * @var string
17
     */
18
    private $symbols;
19
20
    /**
21
     * @var string
22
     */
23
    private $types;
24
25
    /**
26
     * @var string
27
     */
28
    private $range;
29
30
    /**
31
     * Create constructor.
32
     *
33
     * @param  IEXCloud  $api
34
     */
35 7
    public function __construct(IEXCloud $api)
36
    {
37 7
        parent::__construct($api);
38 7
    }
39
40 5
    public function setSymbols(...$symbols): self
41
    {
42 5
        if (count($symbols) === 1) {
43 3
            $this->symbol = $symbols[0];
44
        }
45
46 5
        $this->symbols = implode(',', $symbols);
47
48 5
        return $this;
49
    }
50
51 4
    public function setTypes(...$types): self
52
    {
53 4
        $this->types = implode(',', $types);
54
55 4
        return $this;
56
    }
57
58 2
    public function setRange(string $range): self
59
    {
60 2
        $this->range = $range;
61
62 2
        return $this;
63
    }
64
65
    /**
66
     * @return string
67
     */
68 4
    public function getFullEndpoint(): string
69
    {
70
        $params = [
71 4
            'types' => $this->types,
72
        ];
73
74 4
        if (count(explode(',', $this->symbols)) > 1) {
75 1
            $params['symbols'] = $this->symbols;
76
        }
77
78 4
        if ($this->range) {
79 2
            if (in_array('chart', explode(',', $this->types))) {
80 1
                $params['range'] = $this->range;
81
            }
82
        }
83
84 4
        $query = http_build_query($params);
85
86 4
        $endpoint = str_replace('{symbol}', $this->symbol, self::ENDPOINT);
87 4
        $endpoint = $endpoint . $query;
88
89 4
        return $endpoint;
90
    }
91
92
    /**
93
     * @return bool|void
94
     */
95 6
    protected function validateParams(): void
96
    {
97 6
        if (empty($this->symbols)) {
98 1
            throw WrongData::invalidValuesProvided('Please provide a symbol to query!');
99
        }
100
101 5
        if (empty($this->types)) {
102 1
            throw WrongData::invalidValuesProvided(
103
                'Types Required: comma delimited list of endpoints to call. ' .
104 1
                'The names should match the individual endpoint names. Limited to 10 endpoints.'
105
            );
106
        }
107 4
    }
108
}
109