Completed
Push — develop ( 8b0772...a0b2b6 )
by Paul
04:54
created

JsonConsoleConfigFactory   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 18
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 10
c 0
b 0
f 0
wmc 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A decode() 0 7 2
1
<?php
2
3
namespace PhpUnitGen\Configuration;
4
5
use PhpUnitGen\Exception\InvalidConfigException;
6
use Respect\Validation\Validator;
7
8
/**
9
 * Class JsonConsoleConfigFactory.
10
 *
11
 * @author     Paul Thébaud <[email protected]>.
12
 * @copyright  2017-2018 Paul Thébaud <[email protected]>.
13
 * @license    https://opensource.org/licenses/MIT The MIT license.
14
 * @link       https://github.com/paul-thebaud/phpunit-generator
15
 * @since      Class available since Release 2.0.0.
16
 */
17
class JsonConsoleConfigFactory extends AbstractConsoleConfigFactory
18
{
19
    /**
20
     * Decode a configuration file to get a configuration array.
21
     *
22
     * @param string $configPath The configuration file path.
23
     *
24
     * @return array The decoded configuration array.
25
     *
26
     * @throws InvalidConfigException If the configuration is invalid.
27
     */
28
    protected function decode(string $configPath): array
29
    {
30
        $configArray = json_decode(file_get_contents($configPath), true);
31
        if (! Validator::arrayType()->validate($configArray)) {
32
            throw new InvalidConfigException('Unable to parse JSON config');
33
        }
34
        return $configArray;
35
    }
36
}
37