AddProviderCompilerPass::process()   A
last analyzed

Complexity

Conditions 4
Paths 5

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 8
c 1
b 0
f 0
nc 5
nop 1
dl 0
loc 15
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace DH\AuditorBundle\DependencyInjection\Compiler;
6
7
use DH\Auditor\Auditor;
8
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
9
use Symfony\Component\DependencyInjection\ContainerBuilder;
10
use Symfony\Component\DependencyInjection\Reference;
11
12
class AddProviderCompilerPass implements CompilerPassInterface
13
{
14
    /**
15
     * Get all providers based on their tag (`dh_auditor.provider`) and register them.
16
     */
17
    public function process(ContainerBuilder $container): void
18
    {
19
        if (!$container->hasDefinition(Auditor::class)) {
20
            return;
21
        }
22
23
        $auditorDefinition = $container->getDefinition(Auditor::class);
24
25
        $providers = [];
26
        foreach (array_keys($container->findTaggedServiceIds('dh_auditor.provider')) as $providerId) {
27
            $providers[] = new Reference($providerId);
28
        }
29
30
        foreach ($providers as $provider) {
31
            $auditorDefinition->addMethodCall('registerProvider', [$provider]);
32
        }
33
    }
34
}
35