Failed Conditions
Push — master ( 9cb424...0e881e )
by Asmir
02:57 queued 11s
created

FormattedFile::setDefaultLoaders()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1.048

Importance

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