Completed
Push — master ( 222be8...ad9fc4 )
by Nikola
03:54
created

TwigLoaderCompilerPass::process()   D

Complexity

Conditions 9
Paths 15

Size

Total Lines 38
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 9

Importance

Changes 0
Metric Value
dl 0
loc 38
ccs 20
cts 20
cp 1
rs 4.909
c 0
b 0
f 0
cc 9
eloc 20
nc 15
nop 1
crap 9
1
<?php
2
/*
3
 * This file is part of the QueryResourcesLoaderBundle, an RunOpenCode project.
4
 *
5
 * (c) 2017 RunOpenCode.
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace RunOpenCode\Bundle\QueryResourcesLoader\DependencyInjection\CompilerPass;
11
12
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
13
use Symfony\Component\DependencyInjection\ContainerBuilder;
14
use Symfony\Component\DependencyInjection\Exception\LogicException;
15
use Symfony\Component\DependencyInjection\Reference;
16
17
/**
18
 * Class TwigLoaderCompilerPass
19
 *
20
 * Process Twig loaders.
21
 *
22
 * @package RunOpenCode\Bundle\QueryResourcesLoader\DependencyInjection\CompilerPass
23
 */
24
class TwigLoaderCompilerPass implements CompilerPassInterface
25
{
26 4
    public function process(ContainerBuilder $container)
27
    {
28 4
        if (false === $container->hasDefinition('run_open_code.query_resources_loader.twig')) {
29 1
            return;
30
        }
31
32
        // register additional Query loaders
33 3
        $loaderIds = $container->findTaggedServiceIds('run_open_code.query_resources_loader.twig.loader');
34
35 3
        if (count($loaderIds) === 0) {
36 1
            throw new LogicException('No twig loaders found. You need to tag at least one loader with "run_open_code.query_resources_loader.twig.loader"');
37
        }
38
39 2
        if (count($loaderIds) === 1) {
40 1
            $container->setAlias('run_open_code.query_resources_loader.twig.loader', key($loaderIds));
41
        } else {
42 1
            $chainLoader = $container->getDefinition('run_open_code.query_resources_loader.twig.loader.chain');
43
44 1
            $prioritizedLoaders = array();
45
46 1
            foreach ($loaderIds as $id => $tags) {
47 1
                foreach ($tags as $tag) {
48 1
                    $priority = isset($tag['priority']) ? $tag['priority'] : 0;
49 1
                    $prioritizedLoaders[$priority][] = $id;
50
                }
51
            }
52
53 1
            krsort($prioritizedLoaders);
54
55 1
            foreach ($prioritizedLoaders as $loaders) {
56 1
                foreach ($loaders as $loader) {
57 1
                    $chainLoader->addMethodCall('addLoader', array(new Reference($loader)));
58
                }
59
            }
60
61 1
            $container->setAlias('run_open_code.query_resources_loader.twig.loader', 'run_open_code.query_resources_loader.twig.loader.chain');
62
        }
63 2
    }
64
}
65