Passed
Pull Request — master (#739)
by Michael
02:38
created

YamlConfiguration::doLoad()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 5.0592

Importance

Changes 0
Metric Value
cc 5
nc 5
nop 1
dl 0
loc 28
ccs 13
cts 15
cp 0.8667
crap 5.0592
rs 9.1608
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Migrations\Configuration;
6
7
use Doctrine\Migrations\Configuration\Exception\YamlNotAvailable;
8
use Doctrine\Migrations\Configuration\Exception\YamlNotValid;
9
use Symfony\Component\Yaml\Exception\ParseException;
10
use Symfony\Component\Yaml\Yaml;
11
use function assert;
12
use function class_exists;
13
use function file_get_contents;
14
use function is_array;
15
16
/**
17
 * The YamlConfiguration class is responsible for loading migration configuration information from a YAML file.
18
 *
19
 * @internal
20
 */
21
class YamlConfiguration extends AbstractFileConfiguration
22
{
23
    /**
24
     * @inheritdoc
25
     */
26 32
    protected function doLoad(string $file) : void
27
    {
28 32
        if (! class_exists(Yaml::class)) {
29
            throw YamlNotAvailable::new();
30
        }
31
32 32
        $content = file_get_contents($file);
33
34 32
        assert($content !== false);
35
36
        try {
37 32
            $config = Yaml::parse($content);
38 1
        } catch (ParseException $e) {
39 1
            throw YamlNotValid::malformed();
40
        }
41
42 31
        if (! is_array($config)) {
43
            throw YamlNotValid::invalid();
44
        }
45
46 31
        if (isset($config['migrations_directory'])) {
47 22
            $config['migrations_directory'] = $this->getDirectoryRelativeToFile(
48 22
                $file,
49 22
                $config['migrations_directory']
50
            );
51
        }
52
53 31
        $this->setConfiguration($config);
54 28
    }
55
}
56