Completed
Pull Request — master (#1005)
by Asmir
06:55
created

YamlFile::getConfiguration()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 33

Duplication

Lines 6
Ratio 18.18 %

Code Coverage

Tests 15
CRAP Score 6.0585

Importance

Changes 0
Metric Value
dl 6
loc 33
ccs 15
cts 17
cp 0.8824
rs 8.7697
c 0
b 0
f 0
cc 6
nc 6
nop 0
crap 6.0585
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Migrations\Configuration\Migration;
6
7
use Doctrine\Migrations\Configuration\Configuration;
8
use Doctrine\Migrations\Configuration\Exception\FileNotFound;
9
use Doctrine\Migrations\Configuration\Migration\Exception\YamlNotAvailable;
10
use Doctrine\Migrations\Configuration\Migration\Exception\YamlNotValid;
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 11
    public function getConfiguration() : Configuration
22
    {
23 11
        if (! class_exists(Yaml::class)) {
24
            throw YamlNotAvailable::new();
25
        }
26
27 11
        if (! file_exists($this->file)) {
28 1
            throw FileNotFound::new($this->file);
29
        }
30
31 10
        $content = file_get_contents($this->file);
32
33 10
        assert($content !== false);
34
35
        try {
36 10
            $config = Yaml::parse($content);
37 1
        } catch (ParseException $e) {
38 1
            throw YamlNotValid::malformed();
39
        }
40
41 9
        if (! is_array($config)) {
42
            throw YamlNotValid::invalid();
43
        }
44
45 9 View Code Duplication
        if (isset($config['migrations_paths'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
46 4
            $config['migrations_paths'] = $this->getDirectoriesRelativeToFile(
47 4
                $config['migrations_paths'],
48 4
                $this->file
49
            );
50
        }
51
52 9
        return (new ConfigurationArray($config))->getConfiguration();
53
    }
54
}
55