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

FormattedFile   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Test Coverage

Coverage 80%

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 38
ccs 16
cts 20
cp 0.8
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setDefaultLoaders() 0 17 1
A getConfiguration() 0 12 3
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