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

USHolTradeDates::setStartDate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1.0233

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 5
cts 7
cp 0.7143
rs 9.8666
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1.0233
1
<?php
2
3
namespace Digitonic\IexCloudSdk\ReferenceData;
4
5
use Digitonic\IexCloudSdk\Contracts\IEXCloud;
6
use Digitonic\IexCloudSdk\Exceptions\WrongData;
7
use Digitonic\IexCloudSdk\Requests\BaseRequest;
8
9
class USHolTradeDates extends BaseRequest
10
{
11
    const ENDPOINT = 'ref-data/us/dates/{type}/{direction}/{last}/{startDate}';
12
13
    /**
14
     * @var string
15
     */
16
    protected $direction = 'next';
17
18
    /**
19
     * @var string
20
     */
21
    protected $type = 'trade';
22
23
    /**
24
     * @var int
25
     */
26
    protected $last = 1;
27
28
    /**
29
     * @var string
30
     */
31
    protected $startDate = '';
32
33
    /**
34
     * Create constructor.
35
     *
36
     * @param  IEXCloud  $api
37
     */
38 4
    public function __construct(IEXCloud $api)
39
    {
40 4
        parent::__construct($api);
41 4
    }
42
43
    /**
44
     * @param  string  $type
45
     *
46
     * @return USHolTradeDates
47
     */
48 1
    public function setType(string $type): self
49
    {
50 1
        $this->validateInput($type, '/(trade|holiday)/mi', 'Type can be trade or holiday');
51
52
        $this->type = $type;
53
54
        return $this;
55
    }
56
57
    /**
58
     * @param  string  $direction
59
     *
60
     * @return USHolTradeDates
61
     */
62 1
    public function setDirection(string $direction): self
63
    {
64 1
        $this->validateInput($direction, '/(next|last)/mi', 'Direction can be next or last');
65
66
        $this->direction = $direction;
67
68
        return $this;
69
    }
70
71
    /**
72
     * @param  string  $last
73
     *
74
     * @return USHolTradeDates
75
     */
76
    public function setLast(string $last): self
77
    {
78
        $this->last = (int) $last;
79
80
        return $this;
81
    }
82
83
    /**
84
     * @param  string  $startDate
85
     *
86
     * @return USHolTradeDates
87
     */
88 1
    public function setStartDate(string $startDate): self
89
    {
90 1
        $this->validateInput(
91 1
            $startDate,
92 1
            '/(20\d{2})(\d{2})(\d{2})/mi',
93 1
            'Format date to use YYYYMMDD to fetch sentiment data.'
94
        );
95
96
        $this->startDate = $startDate;
97
98
        return $this;
99
    }
100
101
    /**
102
     * @return string
103
     */
104
    protected function getFullEndpoint(): string
105
    {
106
        return self::ENDPOINT;
107
    }
108
109
    /**
110
     * @param  string  $input
111
     * @param  string  $pattern
112
     * @param  string  $errorMessage
113
     */
114 3 View Code Duplication
    private function validateInput(string $input, string $pattern, string $errorMessage): void
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...
115
    {
116 3
        preg_match_all($pattern, $input, $matches, PREG_SET_ORDER, 0);
117
118 3
        if (empty($matches)) {
119 3
            throw WrongData::invalidValuesProvided($errorMessage);
120
        }
121
    }
122
}
123