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

ConfigurationLoader   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 4
c 1
b 0
f 1
lcom 0
cbo 6
dl 0
loc 29
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A loadFromContainer() 0 4 1
A loadFromFile() 0 21 3
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