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

FormattedFile::getConfiguration()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
c 1
b 0
f 0
nc 4
nop 0
dl 0
loc 12
ccs 7
cts 7
cp 1
crap 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Migrations\Configuration\Configuration;
6
7
use Doctrine\Migrations\Configuration\Configuration;
8
use const PATHINFO_EXTENSION;
9
use function count;
10
use function pathinfo;
11
12
/**
13
 * @internal
14
 */
15
final class FormattedFile extends ConfigurationFile
16
{
17
    /** @var callable[] */
18
    private $loaders = [];
19
20 3
    private function setDefaultLoaders() : void
21
    {
22 3
        $this->loaders = [
23
            'json' => static function ($file) : ConfigurationLoader {
24
                return new JsonFile($file);
25 3
            },
26
            'php' => static function ($file) : ConfigurationLoader {
27 2
                return new PhpFile($file);
28 3
            },
29
            'xml' => static function ($file) : ConfigurationLoader {
30
                return new XmlFile($file);
31 3
            },
32
            'yaml' => static function ($file) : ConfigurationLoader {
33
                return new YamlFile($file);
34 3
            },
35
            'yml' => static function ($file) : ConfigurationLoader {
36
                return new YamlFile($file);
37 3
            },
38
        ];
39 3
    }
40
41 3
    public function getConfiguration() : Configuration
42
    {
43 3
        if (count($this->loaders) === 0) {
44 3
            $this->setDefaultLoaders();
45
        }
46
47 3
        $extension = pathinfo($this->file, PATHINFO_EXTENSION);
48 3
        if (! isset($this->loaders[$extension])) {
49 1
            throw Configuration\Exception\InvalidConfigurationFormat::new($this->file);
50
        }
51
52 2
        return $this->loaders[$extension]($this->file)->getConfiguration();
53
    }
54
}
55