DataCollectorCompilerPass::process()   B
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 37
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 4.7691

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 37
ccs 14
cts 22
cp 0.6364
rs 8.5806
cc 4
eloc 22
nc 4
nop 1
crap 4.7691
1
<?php
2
3
/*
4
 * This file is part of php-cache\cache-bundle package.
5
 *
6
 * (c) 2015 Aaron Scherer <[email protected]>, Tobias Nyholm <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Cache\CacheBundle\DependencyInjection\Compiler;
13
14
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
15
use Symfony\Component\DependencyInjection\ContainerBuilder;
16
use Symfony\Component\DependencyInjection\Definition;
17
use Symfony\Component\DependencyInjection\Reference;
18
19
/**
20
 * Inject a data collector to all the cache services to be able to get detailed statistics.
21
 *
22
 * @author Aaron Scherer <[email protected]>
23
 * @author Tobias Nyholm <[email protected]>
24
 */
25
class DataCollectorCompilerPass implements CompilerPassInterface
26
{
27
    /**
28
     * {@inheritdoc}
29
     */
30 1
    public function process(ContainerBuilder $container)
31
    {
32 1
        if (!$container->hasDefinition('cache.data_collector')) {
33
            return;
34
        }
35
36 1
        $proxyFactory        = $container->get('cache.proxy_factory');
37 1
        $collectorDefinition = $container->getDefinition('cache.data_collector');
38 1
        $serviceIds          = $container->findTaggedServiceIds('cache.provider');
39
40 1
        foreach (array_keys($serviceIds) as $id) {
41
42
            // Get the pool definition and rename it.
43 1
            $poolDefinition = $container->getDefinition($id);
44 1
            if (null === $poolDefinition->getFactory()) {
45
                // Just replace the class
46 1
                $proxyClass = $proxyFactory->createProxy($poolDefinition->getClass(), $file);
0 ignored issues
show
Bug introduced by
The variable $file does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
47 1
                $poolDefinition->setClass($proxyClass);
48 1
                $poolDefinition->setFile($file);
49 1
                $poolDefinition->addMethodCall('__setName', [$id]);
50
            } else {
51
                // Create a new ID for the original service
52
                $innerId = $id.'.inner';
53
                $container->setDefinition($innerId, $poolDefinition);
54
55
                // Create a new definition.
56
                $decoratedPool = new Definition($poolDefinition->getClass());
57
                $decoratedPool->setFactory([new Reference('cache.decorating_factory'), 'create']);
58
                $decoratedPool->setArguments([new Reference($innerId)]);
59
                $container->setDefinition($id, $decoratedPool);
60
                $decoratedPool->addMethodCall('__setName', [$id]);
61
            }
62
63
            // Tell the collector to add the new instance
64 1
            $collectorDefinition->addMethodCall('addInstance', [$id, new Reference($id)]);
65
        }
66 1
    }
67
}
68