Passed
Pull Request — master (#301)
by Fabien
02:07
created

Config::setDirectoriesToScan()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Churn\Configuration;
6
7
class Config
8
{
9
10
    private const DIRECTORIES_TO_SCAN = [];
11
    private const FILES_TO_SHOW = 10;
12
    private const MINIMUM_SCORE_TO_SHOW = 0.1;
13
    private const AMOUNT_OF_PARALLEL_JOBS = 10;
14
    private const SHOW_COMMITS_SINCE = '10 years ago';
15
    private const FILES_TO_IGNORE = [];
16
    private const FILE_EXTENSIONS_TO_PARSE = ['php'];
17
    private const VCS = 'git';
18
19
    /**
20
     * @var array<string, mixed>
21
     */
22
    private $configuration;
23
24
    /**
25
     * @var string|null
26
     */
27
    private $path;
28
29
    /**
30
     * @param array<mixed> $configuration Raw config data.
31
     * @param string|null $path The path of the configuration file if any.
32
     */
33
    private function __construct(array $configuration = [], ?string $path = null)
34
    {
35
        if ([] !== $configuration) {
36
            (new Validator())->validateConfigurationValues($configuration);
37
        }
38
39
        $this->configuration = $configuration;
40
        $this->path = $path;
41
    }
42
43
    /**
44
     * Create a config with given configuration.
45
     *
46
     * @param array<mixed> $configuration The array containing the configuration values.
47
     * @param string|null $path The path of the configuration file if any.
48
     */
49
    public static function create(array $configuration, ?string $path = null): Config
50
    {
51
        return new self($configuration, $path);
52
    }
53
54
    /**
55
     * Create a config with default configuration.
56
     */
57
    public static function createFromDefaultValues(): Config
58
    {
59
        return new self();
60
    }
61
62
    /**
63
     * Return the path of the configuration file.
64
     */
65
    public function getPath(): ?string
66
    {
67
        return $this->path;
68
    }
69
70
    /**
71
     * Get the paths of directories to scan.
72
     *
73
     * @return array<string>
74
     */
75
    public function getDirectoriesToScan(): array
76
    {
77
        return $this->configuration['directoriesToScan'] ?? self::DIRECTORIES_TO_SCAN;
78
    }
79
80
    /**
81
     * @param array<string> $directories Paths of directories to scan.
82
     */
83
    public function setDirectoriesToScan(array $directories): void
84
    {
85
        $this->configuration['directoriesToScan'] = $directories;
86
    }
87
88
    /**
89
     * Get the number of files to display in the results table.
90
     */
91
    public function getFilesToShow(): int
92
    {
93
        return $this->configuration['filesToShow'] ?? self::FILES_TO_SHOW;
94
    }
95
96
    /**
97
     * Get the minimum score a file need to display (ignored if null).
98
     */
99
    public function getMinScoreToShow(): ?float
100
    {
101
        if (\array_key_exists('minScoreToShow', $this->configuration)) {
102
            return $this->configuration['minScoreToShow'];
103
        }
104
105
        return self::MINIMUM_SCORE_TO_SHOW;
106
    }
107
108
    /**
109
     * Get the number of parallel jobs to use to process the files.
110
     */
111
    public function getParallelJobs(): int
112
    {
113
        return $this->configuration['parallelJobs'] ?? self::AMOUNT_OF_PARALLEL_JOBS;
114
    }
115
116
    /**
117
     * @param integer $parallelJobs Number of parallel jobs.
118
     */
119
    public function setParallelJobs(int $parallelJobs): void
120
    {
121
        $this->configuration['parallelJobs'] = $parallelJobs;
122
    }
123
124
    /**
125
     * Get how far back in the git history to go to count commits.
126
     */
127
    public function getCommitsSince(): string
128
    {
129
        return $this->configuration['commitsSince'] ?? self::SHOW_COMMITS_SINCE;
130
    }
131
132
    /**
133
     * @return array<string>
134
     */
135
    public function getFilesToIgnore(): array
136
    {
137
        return $this->configuration['filesToIgnore'] ?? self::FILES_TO_IGNORE;
138
    }
139
140
    /**
141
     * Get the file extensions to use when processing.
142
     *
143
     * @return array<string>
144
     */
145
    public function getFileExtensions(): array
146
    {
147
        return $this->configuration['fileExtensions'] ?? self::FILE_EXTENSIONS_TO_PARSE;
148
    }
149
150
    /**
151
     * Get the version control system.
152
     */
153
    public function getVCS(): string
154
    {
155
        return $this->configuration['vcs'] ?? self::VCS;
156
    }
157
}
158