MetricsCollectorPass::process()   B
last analyzed

Complexity

Conditions 7
Paths 9

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 7.0119

Importance

Changes 0
Metric Value
dl 0
loc 27
ccs 15
cts 16
cp 0.9375
rs 8.5546
c 0
b 0
f 0
cc 7
nc 9
nop 1
crap 7.0119
1
<?php
2
3
namespace Flagbit\Bundle\MetricsBundle\DependencyInjection\Compiler;
4
5
use Flagbit\Bundle\MetricsBundle\Provider\ProviderInvoker;
6
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
7
use Symfony\Component\DependencyInjection\ContainerBuilder;
8
use Symfony\Component\DependencyInjection\Reference;
9
10
class MetricsCollectorPass implements CompilerPassInterface
11
{
12
    /**
13
     * Add tagged metrics.provider services to flagbit_metrics.collector service
14
     *
15
     * @param ContainerBuilder $container
16
     */
17 2
    public function process(ContainerBuilder $container)
18
    {
19 2
        if (false === $container->hasDefinition(ProviderInvoker::class)) {
20
            return;
21
        }
22
23 2
        $definition = $container->getDefinition(ProviderInvoker::class);
24
25 2
        foreach ($container->findTaggedServiceIds('metrics.provider') as $id => $tags) {
26 2
            $collectors = [];
27 2
            foreach($tags as $attributes) {
28 2
                if (!isset($attributes['collector'])) {
29 1
                    throw new \InvalidArgumentException(sprintf(
30 1
                            'Metrics provider service "%s" must have an collector attribute in oder to specify a collector',
31 1
                            $id
32
                        ));
33
                }
34 1
                if ($container->hasDefinition('beberlei_metrics.collector.' . $attributes['collector'])) {
35 1
                    $collectors[] = new Reference('beberlei_metrics.collector.' . $attributes['collector']);
36
                }
37
            }
38
39 1
            if (!empty($collectors)) {
40 1
                $definition->addMethodCall('addMetricsProvider', [new Reference($id), $collectors]);
41
            }
42
        }
43 1
    }
44
}
45