Completed
Push — master ( 45600c...8f79c4 )
by Paul
10:17 queued 06:57
created

ConsoleConfig::getFiles()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * This file is part of PHPUnit Generator.
5
 *
6
 * (c) 2017-2018 Paul Thébaud <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE.md
9
 * file that was distributed with this source code.
10
 */
11
12
namespace PhpUnitGen\Configuration;
13
14
use PhpUnitGen\Configuration\ConfigurationInterface\ConsoleConfigInterface;
15
use PhpUnitGen\Exception\InvalidConfigException;
16
use Respect\Validation\Validator;
17
18
/**
19
 * Class ConsoleConfig.
20
 *
21
 * @author     Paul Thébaud <[email protected]>.
22
 * @copyright  2017-2018 Paul Thébaud <[email protected]>.
23
 * @license    https://opensource.org/licenses/MIT The MIT license.
24
 * @link       https://github.com/paul-thebaud/phpunit-generator
25
 * @since      Class available since Release 2.0.0.
26
 */
27
class ConsoleConfig extends BaseConfig implements ConsoleConfigInterface
28
{
29
    /**
30
     * {@inheritdoc}
31
     */
32
    protected function validate($config): void
33
    {
34
        parent::validate($config);
35
36
        $this->validateBooleans($config);
37
        $this->validateIncludeRegex($config);
38
        $this->validateExcludeRegex($config);
39
        $this->validateDirs($config);
40
        $this->validateFiles($config);
41
    }
42
43
    /**
44
     * Validate all boolean attributes contained in configuration.
45
     *
46
     * @param mixed $config The configuration.
47
     *
48
     * @throws InvalidConfigException If a boolean attribute is invalid.
49
     */
50
    private function validateBooleans($config): void
51
    {
52
        // Check boolean parameters
53
        if (! Validator::key('overwrite', Validator::boolType())->validate($config)) {
54
            throw new InvalidConfigException('"overwrite" parameter must be set as a boolean.');
55
        }
56
        if (! Validator::key('backup', Validator::boolType())->validate($config)) {
57
            throw new InvalidConfigException('"backup" parameter must be set as a boolean.');
58
        }
59
        if (! Validator::key('ignore', Validator::boolType())->validate($config)) {
60
            throw new InvalidConfigException('"ignore" parameter must be set as a boolean.');
61
        }
62
    }
63
64
    /**
65
     * Validate the include regex.
66
     *
67
     * @param mixed $config The configuration.
68
     *
69
     * @throws InvalidConfigException If a string attribute is invalid.
70
     */
71
    private function validateIncludeRegex($config): void
72
    {
73
        if (! Validator::key('include', Validator::stringType())->validate($config)
74
            && ! Validator::key('include', Validator::nullType())->validate($config)
75
        ) {
76
            throw new InvalidConfigException('"include" parameter must be set as a string or a null value.');
77
        }
78
    }
79
80
    /**
81
     * Validate the exclude regex.
82
     *
83
     * @param mixed $config The configuration.
84
     *
85
     * @throws InvalidConfigException If a string attribute is invalid.
86
     */
87
    private function validateExcludeRegex($config): void
88
    {
89
        if (! Validator::key('exclude', Validator::stringType())->validate($config)
90
            && ! Validator::key('exclude', Validator::nullType())->validate($config)
91
        ) {
92
            throw new InvalidConfigException('"exclude" parameter must be set as a string or a null value.');
93
        }
94
    }
95
96
    /**
97
     * Validate directories contained in configuration.
98
     *
99
     * @param mixed $config The configuration.
100
     *
101
     * @throws InvalidConfigException If a directory is invalid (source or target).
102
     */
103
    private function validateDirs($config): void
104
    {
105
        // Check that dirs key exists
106
        if (! Validator::key('dirs')->validate($config)) {
107
            throw new InvalidConfigException('"dirs" parameter is not an array.');
108
        }
109
        // Clean dirs value
110
        if ($config['dirs'] === null) {
111
            $this->config['dirs'] = $config['dirs'] = [];
112
        }
113
        // Validate each dirs
114
        if (! Validator::arrayVal()
115
            ->each(Validator::stringType(), Validator::stringType())->validate($config['dirs'])
116
        ) {
117
            throw new InvalidConfigException('Some directories in "dirs" parameter are not strings.');
118
        }
119
    }
120
121
    /**
122
     * Validate files contained in configuration.
123
     *
124
     * @param mixed $config The configuration.
125
     *
126
     * @throws InvalidConfigException If a file is invalid (source or target).
127
     */
128
    private function validateFiles($config): void
129
    {
130
        // Check that files key exists
131
        if (! Validator::key('files')->validate($config)) {
132
            throw new InvalidConfigException('"files" parameter is not an array.');
133
        }
134
        // Clean files value
135
        if ($config['files'] === null) {
136
            $this->config['files'] = $config['files'] = [];
137
        }
138
        // Validate each files
139
        if (! Validator::arrayVal()
140
            ->each(Validator::stringType(), Validator::stringType())->validate($config['files'])
141
        ) {
142
            throw new InvalidConfigException('Some files in "files" parameter are not strings.');
143
        }
144
    }
145
146
    /**
147
     * {@inheritdoc}
148
     */
149
    public function hasOverwrite(): bool
150
    {
151
        return $this->config['overwrite'];
152
    }
153
154
    /**
155
     * {@inheritdoc}
156
     */
157
    public function hasBackup(): bool
158
    {
159
        return $this->config['backup'];
160
    }
161
162
    /**
163
     * {@inheritdoc}
164
     */
165
    public function hasIgnore(): bool
166
    {
167
        return $this->config['ignore'];
168
    }
169
170
    /**
171
     * {@inheritdoc}
172
     */
173
    public function getIncludeRegex(): ?string
174
    {
175
        return $this->config['include'];
176
    }
177
178
    /**
179
     * {@inheritdoc}
180
     */
181
    public function getExcludeRegex(): ?string
182
    {
183
        return $this->config['exclude'];
184
    }
185
186
    /**
187
     * {@inheritdoc}
188
     */
189
    public function getDirectories(): array
190
    {
191
        return $this->config['dirs'];
192
    }
193
194
    /**
195
     * {@inheritdoc}
196
     */
197
    public function getFiles(): array
198
    {
199
        return $this->config['files'];
200
    }
201
}
202