Completed
Push — master ( 879c21...1c67ad )
by Tomasz
03:09
created

CommandProcessorPass::process()   D

Complexity

Conditions 9
Paths 8

Size

Total Lines 32
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 9

Importance

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