Completed
Pull Request — master (#3)
by Tomáš
06:46
created

LoadCommandsToConsoleCompilerPass   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 8
c 0
b 0
f 0
lcom 0
cbo 3
dl 0
loc 50
ccs 0
cts 27
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A process() 0 10 2
A getDefinitionByType() 0 10 3
A findDefinitionsByType() 0 11 3
1
<?php
2
3
/*
4
 * This file is a part of Sculpin.
5
 *
6
 * (c) Dragonfly Development Inc.
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 Sculpin\Bundle\SculpinBundle\DependencyInjection\Compiler;
13
14
use Symfony\Component\Console\Application;
15
use Symfony\Component\Console\Command\Command;
16
use Symfony\Component\DependencyInjection\ContainerBuilder;
17
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
18
use Symfony\Component\DependencyInjection\Definition;
19
use Symfony\Component\DependencyInjection\Reference;
20
21
final class LoadCommandsToConsoleCompilerPass implements CompilerPassInterface
22
{
23
    /**
24
     * @var ContainerBuilder
25
     */
26
    private $containerBuilder;
27
28
    /**
29
     * {@inheritdoc}
30
     */
31
    public function process(ContainerBuilder $containerBuilder)
32
    {
33
        $this->containerBuilder = $containerBuilder;
34
35
        $consoleApplication = $this->getDefinitionByType(Application::class);
36
37
        foreach ($this->findDefinitionsByType(Command::class) as $name => $definition) {
38
            $consoleApplication->addMethodCall('add', [new Reference($name)]);
39
        }
40
    }
41
42
    /**
43
     * @return bool|Definition
44
     */
45
    private function getDefinitionByType(string $type)
46
    {
47
        foreach ($this->containerBuilder->getDefinitions() as $definition) {
48
            if (is_subclass_of($definition->getClass(), $type)) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if $type can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
49
                return $definition;
50
            }
51
        }
52
53
        return false;
54
    }
55
56
    /**
57
     * @return Definition[]
58
     */
59
    private function findDefinitionsByType(string $type) : array
60
    {
61
        $commands = [];
62
        foreach ($this->containerBuilder->getDefinitions() as $name => $definition) {
63
            if (is_subclass_of($definition->getClass(), $type)) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if $type can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
64
                $commands[$name] = $definition;
65
            }
66
        }
67
68
        return $commands;
69
    }
70
}
71