ConfigurationLoader   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Test Coverage

Coverage 15.38%

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 6
dl 0
loc 45
ccs 2
cts 13
cp 0.1538
rs 10
c 0
b 0
f 0

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
/**
15
 * Use to load the configuration from a yaml file or from the container.
16
 */
17
class ConfigurationLoader
18
{
19
    /**
20
     * Load the configuration using the service container.
21
     *
22
     * @param ContainerInterface $container
23
     *
24
     * @return array
25
     */
26 1
    public function loadFromContainer(ContainerInterface $container)
27
    {
28 1
        return $container->getParameter('jk.assets');
29
    }
30
    
31
    /**
32
     * Load the configuration from a yaml file in the file system.
33
     *
34
     * @param string $path
35
     *
36
     * @return array
37
     *
38
     * @throws Exception
39
     */
40
    public function loadFromFile($path)
41
    {
42
        // the file should exists
43
        $fileSystem = new Filesystem();
44
45
        if (!$fileSystem->exists($path)) {
46
            throw new FileNotFoundException();
47
        }
48
        // the file should be a yml file
49
        $configurationFile = new SplFileInfo($path);
50
51
        if ($configurationFile->getExtension() !== 'yml') {
52
            throw new Exception('Only yml are allowed for assets configuration loading');
53
        }
54
        // parse configuration using Symfony processor
55
        $configuration = Yaml::parse(file_get_contents($path));
56
        $configurationDI = new Configuration();
57
        $processor = new Processor();
58
59
        return $processor->processConfiguration($configurationDI, $configuration);
60
    }
61
}
62