ModelPass   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 10
lcom 0
cbo 3
dl 0
loc 60
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A process() 0 6 1
B addTagged() 0 50 9
1
<?php
2
/*
3
 * This file is part of the PommProject/PommBundle package.
4
 *
5
 * (c) 2014 - 2016 Grégoire HUBERT <[email protected]>
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 PommProject\PommBundle\DependencyInjection\Compiler;
11
12
use Symfony\Component\DependencyInjection as DI;
13
14
/**
15
 * Class PoolerPass
16
 * @package PommProject\PommBundle\DependencyInjection\Compiler
17
 * @author  Miha Vrhovnik
18
 */
19
class ModelPass implements DI\Compiler\CompilerPassInterface
20
{
21
    public function process(DI\ContainerBuilder $container)
22
    {
23
        $this->addTagged($container, 'pomm.model', 'pomm.pooler.model', 'getModel');
24
25
        $this->addTagged($container, 'pomm.model_layer', 'pomm.pooler.model_layer', 'getModelLayer');
26
    }
27
28
    private function addTagged(DI\ContainerBuilder $container, $tag, $defaultServiceId, $method)
29
    {
30
        /** @var DI\Definition[] $definitions */
31
        $definitions = [];
32
33
        // find all service IDs with the appropriate tag
34
        $taggedServices = $container->findTaggedServiceIds($tag);
35
36
        foreach ($taggedServices as $id => $tags) {
37
            $class = $container->getDefinition($id)
38
                ->getClass()
39
            ;
40
41
            $serviceId = isset($tags[0]['pooler']) ? $tags[0]['pooler'] : $defaultServiceId;
42
            $sessionId = isset($tags[0]['session']) ? $tags[0]['session'] : 'pomm.default_session';
43
44
            if (!array_key_exists($serviceId, $definitions)) {
45
                if ($container->hasDefinition($serviceId)) {
46
                    $definitions[$serviceId] = $container->getDefinition($serviceId);
47
48
                    $interface = 'PommProject\PommBundle\Model\ServiceMapInterface';
49
                    if (!in_array($interface, class_implements($definitions[$serviceId]->getClass()), true)) {
50
                        throw new \RuntimeException(sprintf('Your pooler should implement %s.', $interface));
51
                    }
52
                } else {
53
                    throw new \RuntimeException(sprintf('There is no pooler service with id %s.', $serviceId));
54
                }
55
            }
56
57
            $definitions[$serviceId]->addMethodCall('addModelToServiceMapping', [$class, $id . '.pomm.inner']);
58
59
            $old = $container->getDefinition($id);
60
            $old->setPublic(true);
61
            $container->removeDefinition($id);
62
            $container->addDefinitions([$id . '.pomm.inner' => $old]);
63
64
            $service = $container->register($id, $old->getClass())
65
                ->setFactory([new DI\Reference($sessionId), $method])
66
                ->addArgument($old->getClass())
67
            ;
68
69
            if (version_compare(\Symfony\Component\HttpKernel\Kernel::VERSION, '3.3', '<')) {
70
                $service->addAutowiringType($old->getClass());
0 ignored issues
show
Bug introduced by
The method addAutowiringType() does not seem to exist on object<Symfony\Component...cyInjection\Definition>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
71
            }
72
73
            if ($class !== $id) {
74
                $container->setAlias($class, $id);
75
            }
76
        }
77
    }
78
}
79