Completed
Pull Request — master (#4)
by Kevin
02:00
created

ZenstruckAssetManifestExtension::loadFromFile()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Zenstruck\AssetManifestBundle\DependencyInjection;
4
5
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
6
use Symfony\Component\Config\FileLocator;
7
use Symfony\Component\DependencyInjection\ContainerBuilder;
8
use Symfony\Component\DependencyInjection\Loader;
9
use Symfony\Component\HttpKernel\DependencyInjection\ConfigurableExtension;
10
11
/**
12
 * @author Kevin Bond <[email protected]>
13
 */
14
final class ZenstruckAssetManifestExtension extends ConfigurableExtension
15
{
16
    /**
17
     * {@inheritdoc}
18
     */
19 3
    protected function loadInternal(array $mergedConfig, ContainerBuilder $container)
20
    {
21 3
        $loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
22 3
        $loader->load('twig.xml');
23
24 3
        $manifest = [];
25 3
        $manifestFile = $mergedConfig['manifest_file'];
26
27 3
        if ($manifestFile !== null) {
28 2
            $manifest = $this->loadFromFile($manifestFile);
29 1
        }
30
31 2
        $container->getDefinition('zenstruck_asset_manifest.twig_extension')->replaceArgument(0, $manifest);
32 2
    }
33
34
    /**
35
     * @param string $file
36
     *
37
     * @return array
38
     */
39 2
    private function loadFromFile($file)
40
    {
41 2
        if (!file_exists($file)) {
42 1
            throw new InvalidConfigurationException(sprintf('Manifest file "%s" does not exist.', $file));
43
        }
44
45 1
        return json_decode(file_get_contents($file), true);
46
    }
47
}
48