Passed
Pull Request — master (#5)
by Steven
02:27
created

SocialSentiment::validateDate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Digitonic\IexCloudSdk\AlternativeData;
4
5
use Digitonic\IexCloudSdk\Contracts\IEXCloud;
6
use Digitonic\IexCloudSdk\Exceptions\WrongData;
7
use Digitonic\IexCloudSdk\Requests\BaseGet;
8
9
class SocialSentiment extends BaseGet
10
{
11
    const ENDPOINT = 'stock/{symbol}/sentiment/{type}/{date}';
12
13
    /**
14
     * @var string
15
     */
16
    private $type = 'daily';
17
18
    /**
19
     * @var string
20
     */
21
    private $date;
22
23
    /**
24
     * Create constructor.
25
     *
26
     * @param  IEXCloud  $api
27
     */
28 5
    public function __construct(IEXCloud $api)
29
    {
30 5
        parent::__construct($api);
31 5
    }
32
33 1
    public function setType(string $type): self
34
    {
35 1
        $this->type = $type;
36
37 1
        return $this;
38
    }
39
40 3
    public function setDate(string $date): self
41
    {
42 3
        $this->validateDate($date);
43
44 2
        $this->date = $date;
45
46 2
        return $this;
47
    }
48
49
    /**
50
     * @return string
51
     */
52 2 View Code Duplication
    protected function getFullEndpoint(): string
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
53
    {
54 2
        $endpoint = str_replace('{symbol}', $this->symbol, self::ENDPOINT);
55 2
        $endpoint = str_replace('{type}', $this->type, $endpoint);
56 2
        $endpoint = str_replace('{date}', $this->date, $endpoint);
57
58 2
        return $endpoint;
59
60
    }
61
62
    /**
63
     * @return bool|void
64
     */
65 3
    protected function validateParams(): void
66
    {
67 3
        if (empty($this->symbol)) {
68 1
            throw WrongData::invalidValuesProvided('Please provide a symbol to query!');
69
        }
70 2
    }
71
72 3
    private function validateDate(string $date)
73
    {
74 3
        $re = '/(20\d{2})(\d{2})(\d{2})/mi';
75
76 3
        preg_match_all($re, $date, $matches, PREG_SET_ORDER, 0);
77
78 3
        if (empty($matches)) {
79 1
            throw WrongData::invalidValuesProvided('Format date to use YYYYMMDD to fetch sentiment data.');
80
        }
81 2
    }
82
}
83