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

FormattedFile   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Test Coverage

Coverage 80%

Importance

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

2 Methods

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