Completed
Push — master ( ab7ebf...740609 )
by Grégoire
02:23
created

YamlFile::getConfiguration()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 32
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 6.0585

Importance

Changes 0
Metric Value
cc 6
eloc 17
c 0
b 0
f 0
nc 6
nop 0
dl 0
loc 32
ccs 15
cts 17
cp 0.8824
crap 6.0585
rs 9.0777
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Migrations\Configuration\Configuration;
6
7
use Doctrine\Migrations\Configuration\Configuration;
8
use Doctrine\Migrations\Configuration\Configuration\Exception\YamlNotAvailable;
9
use Doctrine\Migrations\Configuration\Configuration\Exception\YamlNotValid;
10
use Doctrine\Migrations\Configuration\Exception\FileNotFound;
11
use Symfony\Component\Yaml\Exception\ParseException;
12
use Symfony\Component\Yaml\Yaml;
13
use function assert;
14
use function class_exists;
15
use function file_exists;
16
use function file_get_contents;
17
use function is_array;
18
19
final class YamlFile extends ConfigurationFile
20
{
21 10
    public function getConfiguration() : Configuration
22
    {
23 10
        if (! class_exists(Yaml::class)) {
24
            throw YamlNotAvailable::new();
25
        }
26
27 10
        if (! file_exists($this->file)) {
28 1
            throw FileNotFound::new($this->file);
29
        }
30
31 9
        $content = file_get_contents($this->file);
32
33 9
        assert($content !== false);
34
35
        try {
36 9
            $config = Yaml::parse($content);
37 1
        } catch (ParseException $e) {
38 1
            throw YamlNotValid::malformed();
39
        }
40
41 8
        if (! is_array($config)) {
42
            throw YamlNotValid::invalid();
43
        }
44
45 8
        if (isset($config['migrations_paths'])) {
46 3
            $config['migrations_paths'] = $this->getDirectoriesRelativeToFile(
47 3
                $config['migrations_paths'],
48 3
                $this->file
49
            );
50
        }
51
52 8
        return (new ConfigurationArray($config))->getConfiguration();
53
    }
54
}
55