Completed
Pull Request — master (#99)
by
unknown
05:56
created

Config::validateMinScoreToShow()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 1
1
<?php declare(strict_types = 1);
2
3
namespace Churn\Configuration;
4
5
use Throwable;
6
use Webmozart\Assert\Assert;
7
use DateTime;
8
use InvalidArgumentException;
9
10
class Config
11
{
12
13
    const FILES_TO_SHOW = 10;
14
    const MINIMUM_SCORE_TO_SHOW = 0;
15
    const AMOUNT_OF_PARALLEL_JOBS = 10;
16
    const SHOW_COMMITS_SINCE = '10 years ago';
17
    const FILES_TO_IGNORE = [];
18
    const FILE_EXTENSIONS_TO_PARSE = ['php'];
19
20
    /**
21
     * @var array
22
     */
23
    private $configuration;
24
25
    /**
26
     * Create a config with given configuration
27
     * @param array $configuration The array containing the configuration values.
28
     * @return Config
29
     */
30
    public static function create(array $configuration): Config
31
    {
32
        return new self($configuration);
33
    }
34
35
    /**
36
     * Create a config with default configuration
37
     * @return Config
38
     */
39
    public static function createFromDefaultValues(): Config
40
    {
41
        return new self();
42
    }
43
44
    /**
45
     * Config constructor.
46
     * @param array $configuration Raw config data.
47
     * @throws InvalidArgumentException If parameters is badly defined.
48
     */
49
    private function __construct(array $configuration = [])
50
    {
51
        if (!empty($configuration)) {
52
            $this->validateConfigurationValues($configuration);
53
        }
54
55
        $this->configuration = $configuration;
56
    }
57
58
    /**
59
     * Get the number of files to display in the results table.
60
     * @return integer
61
     */
62
    public function getFilesToShow(): int
63
    {
64
        return $this->configuration['filesToShow'] ?? self::FILES_TO_SHOW;
65
    }
66
67
    /**
68
     * Get the minimum score a file need to display.
69
     * @return integer
70
     */
71
    public function getMinScoreToShow(): int
72
    {
73
        return $this->configuration['minScoreToShow'] ?? self::MINIMUM_SCORE_TO_SHOW;
74
    }
75
76
    /**
77
     * Get the number of parallel jobs to use to process the files.
78
     * @return integer
79
     */
80
    public function getParallelJobs(): int
81
    {
82
        return $this->configuration['parallelJobs'] ?? self::AMOUNT_OF_PARALLEL_JOBS;
83
    }
84
85
    /**
86
     * Get how far back in the git history to go to count commits.
87
     * @return string
88
     */
89
    public function getCommitsSince(): string
90
    {
91
        return $this->configuration['commitsSince'] ?? self::SHOW_COMMITS_SINCE;
92
    }
93
94
    /**
95
     * Get the paths to files to ignore when processing.
96
     * @return array
97
     */
98
    public function getFilesToIgnore(): array
99
    {
100
        return $this->configuration['filesToIgnore'] ?? self::FILES_TO_IGNORE;
101
    }
102
103
    /**
104
     * Get the file extensions to use when processing.
105
     * @return array
106
     */
107
    public function getFileExtensions(): array
108
    {
109
        return $this->configuration['fileExtensions'] ?? self::FILE_EXTENSIONS_TO_PARSE;
110
    }
111
112
    /**
113
     * @param array $configuration The array containing the configuration values.
114
     * @return void
115
     */
116
    private function validateConfigurationValues(array $configuration): void
117
    {
118
        $this->validateFilesToShow($configuration);
119
        $this->validateMinScoreToShow($configuration);
120
        $this->validateParallelJobs($configuration);
121
        $this->validateCommitsSince($configuration);
122
        $this->validateFilesToIgnore($configuration);
123
        $this->validateFileExtensions($configuration);
124
    }
125
126
    /**
127
     * @param array $configuration The array containing the configuration values.
128
     * @return void
129
     */
130
    private function validateFilesToShow(array $configuration): void
131
    {
132
        if (array_key_exists('filesToShow', $configuration)) {
133
            Assert::integer($configuration['filesToShow'], 'Files to show should be an integer');
134
        }
135
    }
136
137
    /**
138
     * @param array $configuration The array containing the configuration values.
139
     * @return void
140
     */
141
    private function validateMinScoreToShow(array $configuration): void
142
    {
143
        if (array_key_exists('minScoreToShow', $configuration)) {
144
            Assert::integer($configuration['minScoreToShow'], 'Minimum score to show should be an integer');
145
        }
146
    }
147
148
    /**
149
     * @param array $configuration The array containing the configuration values.
150
     * @return void
151
     */
152
    private function validateParallelJobs(array $configuration): void
153
    {
154
        if (array_key_exists('parallelJobs', $configuration)) {
155
            Assert::integer($configuration['parallelJobs'], 'Amount of parallel jobs should be an integer');
156
        }
157
    }
158
159
    /**
160
     * @param array $configuration The array containing the configuration values.
161
     * @return void
162
     * @throws InvalidArgumentException If date is in a bad format.
163
     */
164
    private function validateCommitsSince(array $configuration): void
165
    {
166
        if (array_key_exists('commitsSince', $configuration)) {
167
            Assert::string($configuration['commitsSince'], 'Commits since should be a string');
168
            try {
169
                new DateTime($configuration['commitsSince']);
170
            } catch (Throwable $e) {
171
                throw new InvalidArgumentException('Commits since should be in a valid date format');
172
            }
173
        }
174
    }
175
176
    /**
177
     * @param array $configuration The array containing the configuration values.
178
     * @return void
179
     */
180
    private function validateFilesToIgnore(array $configuration): void
181
    {
182
        if (array_key_exists('filesToIgnore', $configuration)) {
183
            Assert::isArray($configuration['filesToIgnore'], 'Files to ignore should be an array of strings');
184
        }
185
    }
186
187
    /**
188
     * @param array $configuration The array containing the configuration values.
189
     * @return void
190
     */
191
    private function validateFileExtensions(array $configuration): void
192
    {
193
        if (array_key_exists('fileExtensions', $configuration)) {
194
            Assert::isArray($configuration['fileExtensions'], 'File extensions should be an array of strings');
195
        }
196
    }
197
}
198