Completed
Push — master ( ea4e28...f4a6ba )
by Eric
08:48
created

ConvertRegistryPass   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
c 1
b 0
f 0
lcom 0
cbo 3
dl 0
loc 56
ccs 28
cts 28
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A process() 0 15 3
A createLazyDefinition() 0 16 3
A clearMethodCalls() 0 8 2
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\RegistryBundle\DependencyInjection\Compiler;
13
14
use Lug\Bundle\RegistryBundle\Model\LazyRegistry;
15
use Lug\Bundle\RegistryBundle\Model\LazyRegistryInterface;
16
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
17
use Symfony\Component\DependencyInjection\ContainerBuilder;
18
use Symfony\Component\DependencyInjection\Definition;
19
use Symfony\Component\DependencyInjection\Reference;
20
21
/**
22
 * @author GeLo <[email protected]>
23
 */
24
class ConvertRegistryPass implements CompilerPassInterface
25
{
26
    /**
27
     * {@inheritdoc}
28
     */
29 2
    public function process(ContainerBuilder $container)
30
    {
31 2
        foreach (array_keys($container->findTaggedServiceIds($tag = 'lug.registry')) as $registry) {
32 2
            $definition = $container->getDefinition($registry);
33
34 2
            if (is_subclass_of($definition->getClass(), LazyRegistryInterface::class)) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if \Lug\Bundle\RegistryBund...egistryInterface::class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
35 1
                continue;
36
            }
37
38 1
            $container->setDefinition($internal = $registry.'.internal', $definition->clearTag($tag));
39 1
            $container->setDefinition($registry, $this->createLazyDefinition($internal, $definition));
40
41 1
            $this->clearMethodCalls($definition);
42 2
        }
43 2
    }
44
45
    /**
46
     * @param string     $registry
47
     * @param Definition $definition
48
     *
49
     * @return Definition
50
     */
51 1
    private function createLazyDefinition($registry, Definition $definition)
52
    {
53 1
        $lazy = new Definition(LazyRegistry::class, [
54 1
            new Reference('service_container'),
55 1
            new Reference($registry),
56 1
        ]);
57
58 1
        foreach ($definition->getMethodCalls() as $methodCall) {
59 1
            if ($methodCall[0] === 'offsetSet') {
60 1
                $methodCall[1][1] = (string) $methodCall[1][1];
61 1
                $lazy->addMethodCall('setLazy', $methodCall[1]);
62 1
            }
63 1
        }
64
65 1
        return $lazy;
66
    }
67
68
    /**
69
     * @param Definition $definition
70
     */
71 1
    private function clearMethodCalls(Definition $definition)
72
    {
73 1
        $methodCall = 'offsetSet';
74
75 1
        while ($definition->hasMethodCall($methodCall)) {
76 1
            $definition->removeMethodCall($methodCall);
77 1
        }
78 1
    }
79
}
80