Completed
Push — master ( 9f831b...a10c55 )
by Jonathan
12s queued 10s
created

ChangelogConfig::setIncludeOpen()   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 9.4285
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 mixed[] */
32
    private $options = ['rootGitHubUrl' => self::DEFAULT_ROOT_GITHUB_URL];
33
34
    /**
35
     * @param string[] $labels
36
     * @param mixed[]  $options
37
     */
38 31
    public function __construct(
39
        string $user = '',
40
        string $repository = '',
41
        string $milestone = '',
42
        array $labels = [],
43
        bool $includeOpen = false,
44
        array $options = []
45
    ) {
46 31
        $this->user        = $user;
47 31
        $this->repository  = $repository;
48 31
        $this->milestone   = $milestone;
49 31
        $this->labels      = $labels;
50 31
        $this->includeOpen = $includeOpen;
51 31
        $this->options     = array_merge($this->options, $options);
52 31
    }
53
54 1
    public function getUser() : string
55
    {
56 1
        return $this->user;
57
    }
58
59 4
    public function setUser(string $user) : self
60
    {
61 4
        $this->user = $user;
62
63 4
        return $this;
64
    }
65
66 1
    public function getRepository() : string
67
    {
68 1
        return $this->repository;
69
    }
70
71 4
    public function setRepository(string $repository) : self
72
    {
73 4
        $this->repository = $repository;
74
75 4
        return $this;
76
    }
77
78 2
    public function getMilestone() : string
79
    {
80 2
        return $this->milestone;
81
    }
82
83 4
    public function setMilestone(string $milestone) : self
84
    {
85 4
        $this->milestone = $milestone;
86
87 4
        return $this;
88
    }
89
90
    /**
91
     * @return string[]
92
     */
93 2
    public function getLabels() : array
94
    {
95 2
        return $this->labels;
96
    }
97
98
    /**
99
     * @param string[] $labels
100
     */
101 3
    public function setLabels(array $labels) : self
102
    {
103 3
        $this->labels = $labels;
104
105 3
        return $this;
106
    }
107
108 1
    public function shouldIncludeOpen() : bool
109
    {
110 1
        return $this->includeOpen;
111
    }
112
113 2
    public function setIncludeOpen(bool $includeOpen) : self
114
    {
115 2
        $this->includeOpen = $includeOpen;
116
117 2
        return $this;
118
    }
119
120
    /**
121
     * @return mixed[]
122
     */
123 1
    public function getOptions() : array
124
    {
125 1
        return $this->options;
126
    }
127
128
    /**
129
     * @param mixed[] $options
130
     */
131 3
    public function setOptions(array $options) : self
132
    {
133 3
        $this->options = $options;
134
135 3
        return $this;
136
    }
137
138
    /**
139
     * @return mixed
140
     */
141 1
    public function getOption(string $name)
142
    {
143 1
        return $this->options[$name] ?? null;
144
    }
145
146
    /**
147
     * @param mixed $value
148
     */
149 1
    public function setOption(string $name, $value) : self
150
    {
151 1
        $this->options[$name] = $value;
152
153 1
        return $this;
154
    }
155
156 6
    public function getMilestoneIssuesUrl(string $label) : string
157
    {
158 6
        $query = urlencode(sprintf(
159 6
            'milestone:"%s" repo:%s/%s%s%s',
160 6
            str_replace('"', '\"', $this->milestone),
161 6
            $this->user,
162 6
            $this->repository,
163 6
            $this->includeOpen ? '' : ' state:closed',
164 6
            $label !== '' ? ' label:' . $label : ''
165
        ));
166
167 6
        return sprintf('%s/search/issues?q=%s', $this->getRootGitHubUrl(), $query);
168
    }
169
170 13
    public function isValid() : bool
171
    {
172 13
        return $this->user !== '' && $this->repository !== '' && $this->milestone !== '';
173
    }
174
175 6
    private function getRootGitHubUrl() : string
176
    {
177 6
        return $this->options['rootGitHubUrl'] ?? self::DEFAULT_ROOT_GITHUB_URL;
178
    }
179
}
180