ConsoleCommandsPass::process()   B
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 25
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 25
ccs 0
cts 18
cp 0
rs 8.5806
cc 4
eloc 17
nc 4
nop 1
crap 20
1
<?php
2
3
/**
4
 * Moodle component manager.
5
 *
6
 * @author Luke Carrier <[email protected]>
7
 * @copyright 2016 Luke Carrier
8
 * @license GPL-3.0+
9
 */
10
11
namespace ComponentManager\DependencyInjection;
12
13
use InvalidArgumentException;
14
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
15
use Symfony\Component\DependencyInjection\ContainerBuilder;
16
use Symfony\Component\DependencyInjection\Reference;
17
18
/**
19
 * Add tagged console commands.
20
 */
21
class ConsoleCommandsPass implements CompilerPassInterface {
22
    /**
23
     * Console command class.
24
     *
25
     * @var string
26
     */
27
    const CONSOLE_COMMAND = 'Symfony\\Component\\Console\\Command\\Command';
28
29
    /**
30
     * Method to call on console application to add commands.
31
     *
32
     * @var string
33
     */
34
    const ADD_METHOD = 'add';
35
36
    /**
37
     * Console application ID.
38
     *
39
     * @var string
40
     */
41
    protected $appId;
42
43
    /**
44
     * Tag name.
45
     *
46
     * @var string
47
     */
48
    protected $tagName;
49
50
    /**
51
     * Initialiser.
52
     *
53
     * @param string|null $tagName
54
     * @param string|null $appId
55
     */
56
    public function __construct($tagName=null, $appId=null) {
57
        $this->tagName = ($tagName === null) ? 'console.command' : $tagName;
58
        $this->appId = ($appId === null) ? 'console.application' : $appId;
59
    }
60
61
    /**
62
     * @inheritdoc CompilerPassInterface
63
     */
64
    public function process(ContainerBuilder $container) {
65
        $services      = $container->findTaggedServiceIds($this->tagName);
66
        $appDefinition = $container->getDefinition($this->appId);
67
68
        foreach ($services as $id => $tags) {
69
            $definition = $container->getDefinition($id);
70
71
            if ($definition->isAbstract()) {
72
                throw new InvalidArgumentException(sprintf(
73
                        'The service "%s" tagged "%s" must not be abstract.',
74
                        $id, $this->tagName));
75
            }
76
77
            $class = $container->getParameterBag()->resolveValue(
78
                    $definition->getClass());
79
            if (!is_subclass_of($class, static::CONSOLE_COMMAND)) {
80
                throw new InvalidArgumentException(sprintf(
81
                        'The service "%s" tagged "%s" must be a subclass of "%s".',
82
                        $id, $this->tagName, static::CONSOLE_COMMAND));
83
            }
84
85
            $appDefinition->addMethodCall(
86
                    static::ADD_METHOD, [new Reference($id)]);
87
        }
88
    }
89
}
90