AbstractRegistryPass::process()   B
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 28
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 28
rs 8.439
cc 5
eloc 16
nc 5
nop 1
1
<?php
2
3
namespace Psi\Bundle\ContentType\DependencyInjection\Compiler;
4
5
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
6
use Symfony\Component\DependencyInjection\ContainerBuilder;
7
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
8
use Symfony\Component\DependencyInjection\Reference;
9
10
abstract class AbstractRegistryPass implements CompilerPassInterface
11
{
12
    private $registryId;
13
    private $serviceTag;
14
    private $registerByAlias;
15
16
    public function __construct(
17
        $registryId,
18
        $serviceTag,
19
        $registerByAlias = true
20
    ) {
21
        $this->registryId = $registryId;
22
        $this->serviceTag = $serviceTag;
23
        $this->registerByAlias = $registerByAlias;
24
    }
25
26
    public function process(ContainerBuilder $container)
27
    {
28
        if (!$container->has($this->registryId)) {
29
            return;
30
        }
31
32
        $taggedIds = $container->findTaggedServiceIds($this->serviceTag);
33
        $registryDef = $container->getDefinition($this->registryId);
34
35
        foreach ($taggedIds as $serviceId => $attributes) {
36
            $attributes = $attributes[0];
37
38
            if ($this->registerByAlias) {
39
                if (!isset($attributes['alias'])) {
40
                    throw new InvalidArgumentException(sprintf(
41
                        $this->context . ' "%s" has no "alias" attribute in its tag',
0 ignored issues
show
Bug introduced by
The property context does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
42
                        $serviceId
43
                    ));
44
                }
45
46
                $alias = $attributes['alias'];
47
            } else {
48
                $alias = $container->getDefinition($serviceId)->getClass();
49
            }
50
51
            $registryDef->addMethodCall('register', [$alias, new Reference($serviceId)]);
52
        }
53
    }
54
}
55