Completed
Pull Request — master (#59)
by Eric
33:48
created

ConfigureFactoryPass::getTranslationFactory()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
ccs 0
cts 0
cp 0
rs 9.2
cc 4
eloc 6
nc 3
nop 2
crap 20
1
<?php
2
3
/*
4
 * This file is part of the Lug package.
5
 *
6
 * (c) Eric GELOEN <[email protected]>
7
 *
8
 * For the full copyright and license information, please read the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Lug\Bundle\TranslationBundle\DependencyInjection\Compiler;
13
14
use Lug\Component\Translation\Factory\TranslatableFactory;
15
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
16
use Symfony\Component\DependencyInjection\ContainerBuilder;
17
use Symfony\Component\DependencyInjection\Definition;
18
use Symfony\Component\DependencyInjection\Reference;
19
20
/**
21
 * @author GeLo <[email protected]>
22
 */
23
class ConfigureFactoryPass implements CompilerPassInterface
24
{
25
    /**
26
     * {@inheritdoc}
27 2
     */
28
    public function process(ContainerBuilder $container)
29 2
    {
30 2
        foreach ($container->findTaggedServiceIds('lug.factory') as $service => $attributes) {
31
            $factory = $container->getDefinition($service);
32 2
33 1
            if (!is_a($factory->getClass(), TranslatableFactory::class, true)) {
34 1
                continue;
35 2
            }
36 2
37
            $factory
38
                ->addArgument(new Reference('lug.translation.context.locale'))
39
                ->addArgument($this->getTranslationFactory($attributes[0]['resource'], $container));
40
        }
41
    }
42
43
    /**
44
     * @param string           $resource
45
     * @param ContainerBuilder $container
46
     *
47
     * @return Reference
48
     */
49
    private function getTranslationFactory($resource, ContainerBuilder $container)
50
    {
51
        $translation = null;
52
53
        foreach ($container->getDefinition('lug.resource.'.$resource)->getMethodCalls() as $methodCall) {
54
            if ($methodCall[0] === 'addRelation' && $methodCall[1][0] === 'translation') {
55
                $translation = $container->getDefinition((string) $methodCall[1][1])->getArgument(0);
56
            }
57
        }
58
59
        return new Reference('lug.factory.'.$translation);
60
    }
61
}
62