FileLoader::getLoaderForExtension()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 13
rs 9.4285
cc 3
eloc 7
nc 3
nop 1
1
<?php
2
/*
3
 * This file is part of the Borobudur-Config package.
4
 *
5
 * (c) Hexacodelabs <http://hexacodelabs.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Borobudur\Config;
12
13
use Borobudur\Config\Exception\InvalidArgumentException;
14
use Borobudur\Config\FileLoader\FileLoaderInterface;
15
use Borobudur\Config\FileLoader\FileLocator;
16
use Borobudur\Config\FileLoader\IniFileLoader;
17
use Borobudur\Config\FileLoader\YamlFileLoader;
18
19
/**
20
 * @author      Iqbal Maulana <[email protected]>
21
 * @created     8/12/15
22
 */
23
class FileLoader implements FileLoaderInterface
24
{
25
    /**
26
     * @var FileLoaderInterface[]
27
     */
28
    private $loaders = array();
29
30
    /**
31
     * Constructor.
32
     */
33
    public function __construct()
34
    {
35
        $this->loaders = $this->getDefaultLoaders();
36
    }
37
38
    /**
39
     * Add loader.
40
     *
41
     * @param FileLoaderInterface $loader
42
     */
43
    public function addLoader(FileLoaderInterface $loader)
44
    {
45
        $this->loaders[] = $loader;
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function matchExtension($extension)
52
    {
53
        foreach ($this->loaders as $loader) {
54
            if (true === $loader->matchExtension($extension)) {
55
                return true;
56
            }
57
        }
58
59
        return false;
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function import(FileLocator $file)
66
    {
67
        return $this->getLoaderForExtension($file->getExtension())->import($file);
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function write(array $configs, $file)
74
    {
75
        $this->getLoaderForExtension(pathinfo($file, PATHINFO_EXTENSION))->write($configs, $file);
76
    }
77
78
    /**
79
     * Get loader for extension.
80
     *
81
     * @param string $extension
82
     *
83
     * @return FileLoaderInterface
84
     */
85
    protected function getLoaderForExtension($extension)
86
    {
87
        foreach ($this->loaders as $loader) {
88
            if ($loader->matchExtension($extension)) {
89
                return $loader;
90
            }
91
        }
92
93
        throw new InvalidArgumentException(sprintf(
94
            'Loader cannot process file with extension "%s".',
95
            $extension
96
        ));
97
    }
98
99
    /**
100
     * Get default loaders.
101
     *
102
     * @return array
103
     */
104
    protected function getDefaultLoaders()
105
    {
106
        return array(
107
            new YamlFileLoader(),
108
            new IniFileLoader(),
109
        );
110
    }
111
}
112