Passed
Pull Request — master (#959)
by Asmir
02:10
created

FormattedFile   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Test Coverage

Coverage 85%

Importance

Changes 0
Metric Value
eloc 18
c 0
b 0
f 0
dl 0
loc 38
ccs 17
cts 20
cp 0.85
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\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 4
    private function setDefaultLoaders() : void
22
    {
23 4
        $this->loaders = [
24
            'json' => static function ($file) : ConfigurationLoader {
25
                return new JsonFile($file);
26 4
            },
27
            'php' => static function ($file) : ConfigurationLoader {
28 2
                return new PhpFile($file);
29 4
            },
30
            'xml' => static function ($file) : ConfigurationLoader {
31
                return new XmlFile($file);
32 4
            },
33
            'yaml' => static function ($file) : ConfigurationLoader {
34
                return new YamlFile($file);
35 4
            },
36
            'yml' => static function ($file) : ConfigurationLoader {
37 1
                return new YamlFile($file);
38 4
            },
39
        ];
40 4
    }
41
42 4
    public function getConfiguration() : Configuration
43
    {
44 4
        if (count($this->loaders) === 0) {
45 4
            $this->setDefaultLoaders();
46
        }
47
48 4
        $extension = pathinfo($this->file, PATHINFO_EXTENSION);
49 4
        if (! isset($this->loaders[$extension])) {
50 1
            throw InvalidConfigurationFormat::new($this->file);
51
        }
52
53 3
        return $this->loaders[$extension]($this->file)->getConfiguration();
54
    }
55
}
56