QueryFactoryPass::process()   B
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 25
ccs 14
cts 14
cp 1
rs 8.439
c 0
b 0
f 0
cc 5
eloc 13
nc 5
nop 1
crap 5
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace GBProd\AlgoliaSpecificationBundle\DependencyInjection\Compiler;
6
7
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
8
use Symfony\Component\DependencyInjection\ContainerBuilder;
9
use Symfony\Component\DependencyInjection\Reference;
10
11
/**
12
 * Register algolia query factories
13
 *
14
 * @author GBProd <[email protected]>
15
 */
16
class QueryFactoryPass implements CompilerPassInterface
17
{
18
    /**
19
     * {inheritdoc}
20
     */
21 4
    public function process(ContainerBuilder $container)
22
    {
23 4
        if (!$container->has('gbprod.algolia_specification_handler')) {
24 1
            throw new \Exception('Missing gbprod.algolia_specification_handler definition');
25
        }
26
27 3
        $handler = $container->findDefinition('gbprod.algolia_specification_handler');
28
29 3
        $factories = $container->findTaggedServiceIds('algolia.query_factory');
30
31 3
        foreach ($factories as $id => $tags) {
32 2
            foreach ($tags as $attributes) {
33 2
                if (!isset($attributes['specification'])) {
34 1
                    throw new \Exception(
35 1
                        'The algolia.query_factory tag must always have a "specification" attribute'
36
                    );
37
                }
38
39 1
                $handler->addMethodCall(
40 1
                    'registerFactory',
41 1
                    [$attributes['specification'], new Reference($id)]
42
                );
43
            }
44
        }
45 2
    }
46
}
47