Passed
Pull Request — master (#42)
by
unknown
04:53 queued 43s
created

ChangelogConfig::setIncludeDate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ChangelogGenerator;
6
7
use function array_merge;
8
use function sprintf;
9
use function str_replace;
10
use function urlencode;
11
12
class ChangelogConfig
13
{
14
    private const DEFAULT_ROOT_GITHUB_URL = 'https://api.github.com';
15
16
    /** @var string */
17
    private $user;
18
19
    /** @var string */
20
    private $repository;
21
22
    /** @var string */
23
    private $milestone;
24
25
    /** @var string[] */
26
    private $labels;
27
28
    /** @var bool */
29
    private $includeOpen;
30
31
    /** @var bool */
32
    private $includeDate = false;
33
34
    /** @var mixed[] */
35
    private $options = [
36
        'rootGitHubUrl' => self::DEFAULT_ROOT_GITHUB_URL,
37
        'dateFormat' => 'Y-m-d',
38
    ];
39
40
    /**
41
     * @param string[] $labels
42
     * @param mixed[]  $options
43
     */
44 39
    public function __construct(
45
        string $user = '',
46
        string $repository = '',
47
        string $milestone = '',
48
        array $labels = [],
49
        bool $includeOpen = false,
50
        array $options = []
51
    ) {
52 39
        $this->user        = $user;
53 39
        $this->repository  = $repository;
54 39
        $this->milestone   = $milestone;
55 39
        $this->labels      = $labels;
56 39
        $this->includeOpen = $includeOpen;
57 39
        $this->options     = array_merge($this->options, $options);
58 39
    }
59
60 1
    public function getUser() : string
61
    {
62 1
        return $this->user;
63
    }
64
65 7
    public function setUser(string $user) : self
66
    {
67 7
        $this->user = $user;
68
69 7
        return $this;
70
    }
71
72 1
    public function getRepository() : string
73
    {
74 1
        return $this->repository;
75
    }
76
77 7
    public function setRepository(string $repository) : self
78
    {
79 7
        $this->repository = $repository;
80
81 7
        return $this;
82
    }
83
84 3
    public function getMilestone() : string
85
    {
86 3
        return $this->milestone;
87
    }
88
89 7
    public function setMilestone(string $milestone) : self
90
    {
91 7
        $this->milestone = $milestone;
92
93 7
        return $this;
94
    }
95
96
    /**
97
     * @return string[]
98
     */
99 4
    public function getLabels() : array
100
    {
101 4
        return $this->labels;
102
    }
103
104
    /**
105
     * @param string[] $labels
106
     */
107 7
    public function setLabels(array $labels) : self
108
    {
109 7
        $this->labels = $labels;
110
111 7
        return $this;
112
    }
113
114 1
    public function shouldIncludeOpen() : bool
115
    {
116 1
        return $this->includeOpen;
117
    }
118
119 8
    public function setIncludeOpen(bool $includeOpen) : self
120
    {
121 8
        $this->includeOpen = $includeOpen;
122
123 8
        return $this;
124
    }
125
126 2
    public function shouldIncludeDate() : bool
127
    {
128 2
        return $this->includeDate;
129
    }
130
131 18
    public function setIncludeDate(bool $includeDate) : self
132
    {
133 18
        $this->includeDate = $includeDate;
134
135 18
        return $this;
136
    }
137
138
    /**
139
     * @return mixed[]
140
     */
141 1
    public function getOptions() : array
142
    {
143 1
        return $this->options;
144
    }
145
146
    /**
147
     * @param mixed[] $options
148
     */
149 3
    public function setOptions(array $options) : self
150
    {
151 3
        $this->options = $options;
152
153 3
        return $this;
154
    }
155
156
    /**
157
     * @return mixed
158
     */
159 2
    public function getOption(string $name)
160
    {
161 2
        return $this->options[$name] ?? null;
162
    }
163
164
    /**
165
     * @param mixed $value
166
     */
167 1
    public function setOption(string $name, $value) : self
168
    {
169 1
        $this->options[$name] = $value;
170
171 1
        return $this;
172
    }
173
174 6
    public function getMilestoneIssuesUrl(string $label) : string
175
    {
176 6
        $query = urlencode(sprintf(
177 6
            'milestone:"%s" repo:%s/%s%s%s',
178 6
            str_replace('"', '\"', $this->milestone),
179 6
            $this->user,
180 6
            $this->repository,
181 6
            $this->includeOpen ? '' : ' state:closed',
182 6
            $label !== '' ? ' label:' . $label : ''
183
        ));
184
185 6
        return sprintf('%s/search/issues?q=%s', $this->getRootGitHubUrl(), $query);
186
    }
187
188 18
    public function isValid() : bool
189
    {
190 18
        return $this->user !== '' && $this->repository !== '' && $this->milestone !== '';
191
    }
192
193 6
    private function getRootGitHubUrl() : string
194
    {
195 6
        return $this->options['rootGitHubUrl'] ?? self::DEFAULT_ROOT_GITHUB_URL;
196
    }
197
}
198