Completed
Push — develop ( d537c4...fbe971 )
by Paul
02:01
created

ConsoleConfig::validateDirs()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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