Test Failed
Pull Request — master (#13)
by
unknown
04:04
created

Batch::validateParams()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.0261

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 6
cts 7
cp 0.8571
rs 9.8333
c 0
b 0
f 0
cc 3
nc 3
nop 0
crap 3.0261
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 3
    public function __construct(IEXCloud $api)
36
    {
37 3
        parent::__construct($api);
38 3
    }
39
40 1
    public function setSymbols(...$symbols): self
41
    {
42 1
        if (count($symbols) === 1) {
43
            $this->symbol = $symbols[0];
44
        }
45
46 1
        $this->symbols = implode(',', $symbols);
47
48 1
        return $this;
49
    }
50
51
    public function setTypes(...$types): self
52
    {
53
        $this->types = implode(',', $types);
54
55
        return $this;
56
    }
57
58
    public function setRange(string $range): self
59
    {
60
        $this->range = $range;
61
62
        return $this;
63
    }
64
65
    /**
66
     * @return string
67
     */
68
    public function getFullEndpoint(): string
69
    {
70
        $params = [
71
            'types' => $this->types,
72
        ];
73
74
        if (count(explode(',', $this->symbols)) > 1) {
75
            $params['symbols'] = $this->symbols;
76
        }
77
78
        if ($this->range) {
79
            if (in_array('chart', explode(',', $this->types))) {
80
                $params['range'] = $this->range;
81
            }
82
        }
83
84
        $query = http_build_query($params);
85
86
        $endpoint = str_replace('{symbol}', $this->symbol, self::ENDPOINT);
87
        $endpoint = $endpoint . $query;
88
89
        return $endpoint;
90
    }
91
92
    /**
93
     * @return bool|void
94
     * @throws WrongData
95
     */
96 2
    protected function validateParams(): void
97
    {
98 2
        if (empty($this->symbols)) {
99 1
            throw WrongData::invalidValuesProvided('Please provide a symbol to query!');
100
        }
101
102 1
        if (empty($this->types)) {
103 1
            throw WrongData::invalidValuesProvided(
104
                'Types Required: comma delimited list of endpoints to call. ' .
105 1
                'The names should match the individual endpoint names. Limited to 10 endpoints.'
106
            );
107
        }
108
    }
109
}
110