Passed
Push — master ( 229462...d74785 )
by Andreas
02:20 queued 14s
created

ConfigurationLoader::addLoader()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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