YamlConsoleConfigFactory::decode()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 3
nop 1
dl 0
loc 14
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * This file is part of PhpUnitGen.
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\Exception\InvalidConfigException;
15
use Symfony\Component\Yaml\Exception\ParseException;
16
use Symfony\Component\Yaml\Yaml;
17
18
/**
19
 * Class YamlConsoleConfigFactory.
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 YamlConsoleConfigFactory extends AbstractConsoleConfigFactory
28
{
29
    /**
30
     * {@inheritdoc}
31
     */
32
    protected function decode(string $configPath): array
33
    {
34
        try {
35
            $configArray = Yaml::parse(file_get_contents($configPath));
36
        } catch (ParseException $exception) {
37
            throw new InvalidConfigException(sprintf(
38
                'Unable to parse YAML config: %s',
39
                $exception->getMessage()
40
            ));
41
        }
42
        if (! is_array($configArray)) {
43
            throw new InvalidConfigException('Invalid YAML config');
44
        }
45
        return $configArray;
46
    }
47
}
48