Completed
Push — master ( 02360f...237513 )
by Beñat
01:45
created

SetNotFoundLocaleResolverPass::loadDependency()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 3
nop 3
1
<?php
2
3
/*
4
 * This file is part of the I18n Routing Bundle.
5
 *
6
 * (c) Beñat Espiña <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace BenatEspina\I18nRoutingBundle\DependencyInjection\Compiler;
13
14
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
15
use Symfony\Component\DependencyInjection\ContainerBuilder;
16
17
/**
18
 * @author Beñat Espiña <[email protected]>
19
 */
20
class SetNotFoundLocaleResolverPass implements CompilerPassInterface
21
{
22
    const TWIG_EXTENSION_ID = 'benat_espina_i18n_routing.twig.current_path_translation_extension';
23
    const LISTENER_ID = 'benat_espina_i18n_routing.event_listener.not_found_locale';
24
25
    public function process(ContainerBuilder $container)
26
    {
27
        $this->loadDependency($container, self::LISTENER_ID, 0);
28
        $this->loadDependency($container, self::TWIG_EXTENSION_ID, 3);
29
    }
30
31
    private function loadDependency(ContainerBuilder $container, $serviceId, $position)
32
    {
33
        if (!$container->hasDefinition($serviceId)) {
34
            return;
35
        }
36
        $services = $container->findTaggedServiceIds('benat_espina_i18n_routing.not_found_locale_resolver');
37
38
        if (count($services) > 1) {
39
            throw new \LogicException(
40
                'Only one service can have the "benat_espina_i18n_routing.not_found_locale_resolver" tag.'
41
            );
42
        }
43
44
        $container->findDefinition($serviceId)->replaceArgument($position, $container->getDefinition(key($services)));
45
    }
46
}
47