StopwatchCompilerPass   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 24
ccs 10
cts 10
cp 1
rs 10
c 0
b 0
f 0
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A process() 0 14 4
1
<?php
2
declare(strict_types = 1);
3
/**
4
 * /src/Compiler/StopwatchCompilerPass.php
5
 *
6
 * @author TLe, Tarmo Leppänen <[email protected]>
7
 */
8
9
namespace App\Compiler;
10
11
use App\Decorator\StopwatchDecorator;
12
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
13
use Symfony\Component\DependencyInjection\ContainerBuilder;
14
use Symfony\Component\DependencyInjection\Definition;
15
use Symfony\Component\DependencyInjection\Reference;
16
use function str_starts_with;
17
18
/**
19
 * Class StopwatchCompilerPass
20
 *
21
 * @package App\Compiler
22
 * @author TLe, Tarmo Leppänen <[email protected]>
23
 */
24
class StopwatchCompilerPass implements CompilerPassInterface
25
{
26
    private const SERVICE_TAGS = [
27
        'security.voter',
28
        'kernel.event_subscriber',
29
        'validator.constraint_validator',
30
        'validator.initializer',
31
        'app.stopwatch',
32
    ];
33
34 3
    public function process(ContainerBuilder $container): void
35
    {
36 3
        foreach (self::SERVICE_TAGS as $tag) {
37 3
            foreach ($container->findTaggedServiceIds($tag) as $serviceId => $tags) {
38 2
                if (!str_starts_with($serviceId, 'App')) {
39 1
                    continue;
40
                }
41
42 1
                $definition = new Definition($container->getDefinition($serviceId)->getClass());
43 1
                $definition->setDecoratedService($serviceId);
44 1
                $definition->setFactory([new Reference(StopwatchDecorator::class), 'decorate']);
45 1
                $definition->setArguments([new Reference($serviceId . '.stopwatch.inner')]);
46
47 1
                $container->setDefinition($serviceId . '.stopwatch', $definition);
48
            }
49
        }
50
    }
51
}
52