Completed
Push — master ( b44b47...75271b )
by Tomasz
02:47
created

CommandProcessorPass::process()   C

Complexity

Conditions 8
Paths 7

Size

Total Lines 28
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 29.9519

Importance

Changes 0
Metric Value
dl 0
loc 28
ccs 6
cts 20
cp 0.3
rs 5.3846
c 0
b 0
f 0
cc 8
eloc 16
nc 7
nop 1
crap 29.9519
1
<?php
2
3
/*
4
 * This file is part of the Symfony package.
5
 *
6
 * (c) Fabien Potencier <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Gendoria\CommandQueueBundle\DependencyInjection\Pass;
13
14
use InvalidArgumentException;
15
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
16
use Symfony\Component\DependencyInjection\ContainerBuilder;
17
18
/**
19
 * Compiler pass to register tagged services for an event dispatcher.
20
 */
21
class CommandProcessorPass implements CompilerPassInterface
22
{
23
    /**
24
     * Command processor factory service ID.
25
     *
26
     * @var string
27
     */
28
    protected $processorFactoryService;
29
30
    /**
31
     * Processor listener tag name.
32
     *
33
     * @var string
34
     */
35
    protected $listenerTag;
36
37
    /**
38
     * Class constructor.
39
     *
40
     * @param string $serviceFactoryService Service name of the event dispatcher in processed container.
41
     * @param string $listenerTag           Tag name used for listener.
42
     */
43 1
    public function __construct($serviceFactoryService = 'gendoria_command_queue.processor_factory', $listenerTag = 'gendoria_command_queue.processor')
44
    {
45 1
        $this->processorFactoryService = $serviceFactoryService;
46 1
        $this->listenerTag = $listenerTag;
47 1
    }
48
49
    /**
50
     * Process command processor tagged services and add to command processor factory.
51
     *
52
     * @param ContainerBuilder $container
53
     *
54
     * @throws InvalidArgumentException
55
     */
56 1
    public function process(ContainerBuilder $container)
57
    {
58 1
        if (!$container->hasDefinition($this->processorFactoryService) && !$container->hasAlias($this->processorFactoryService)) {
59
            return;
60
        }
61
62 1
        $dispatcher = $container->findDefinition($this->processorFactoryService);
63
64 1
        foreach ($container->findTaggedServiceIds($this->listenerTag) as $id => $tags) {
65
            $def = $container->getDefinition($id);
66
            if (!$def->isPublic()) {
67
                throw new InvalidArgumentException(sprintf('The service "%s" must be public as services are lazy-loaded.', $id));
68
            }
69
70
            if ($def->isAbstract()) {
71
                throw new InvalidArgumentException(sprintf('The service "%s" must not be abstract as services are lazy-loaded.', $id));
72
            }
73
            foreach ($tags as $attributes) {
74
                if (empty($attributes['command'])) {
75
                    throw new InvalidArgumentException(sprintf('The service "%s" is tagged as processor without specifying "command" attribute', $id));
76
                }
77
                $dispatcher->addMethodCall('registerProcessorIdForCommand', array(
78
                    $attributes['command'],
79
                    $id,
80
                ));
81
            }
82 1
        }
83 1
    }
84
}
85