Completed
Push — master ( 6685f6...03cc5c )
by Arnaud
11s
created

ConfigurationLoader::loadFromContainer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace JK\SamBundle\Configuration\Loader;
4
5
use Exception;
6
use JK\SamBundle\DependencyInjection\Configuration;
7
use SplFileInfo;
8
use Symfony\Component\Config\Definition\Processor;
9
use Symfony\Component\DependencyInjection\ContainerInterface;
10
use Symfony\Component\Filesystem\Exception\FileNotFoundException;
11
use Symfony\Component\Filesystem\Filesystem;
12
use Symfony\Component\Yaml\Yaml;
13
14
class ConfigurationLoader
15
{
16
    public function loadFromContainer(ContainerInterface $container)
17
    {
18
        return $container->getParameter('jk.assets');
19
    }
20
21
    public function loadFromFile($path)
22
    {
23
        // the file should exists
24
        $fileSystem = new Filesystem();
25
26
        if (!$fileSystem->exists($path)) {
27
            throw new FileNotFoundException();
28
        }
29
        // the file should be a yml file
30
        $configurationFile = new SplFileInfo($path);
31
32
        if ($configurationFile->getExtension() !== 'yml') {
33
            throw new Exception('Only yml are allowed for assets configuration loading');
34
        }
35
        // parse configuration using Symfony processor
36
        $configuration = Yaml::parse(file_get_contents($path));
37
        $configurationDI = new Configuration();
38
        $processor = new Processor();
39
40
        return $processor->processConfiguration($configurationDI, $configuration);
41
    }
42
}
43