Completed
Push — master ( 6d71ae...f255f3 )
by Greg
01:49
created

ParameterInjection::injectIntoCommandData()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
namespace Consolidation\AnnotatedCommand;
3
4
/**
5
 * Prepare parameter list for execurion. Handle injection of any
6
 * special values (e.g. $input and $output) into the parameter list.
7
 */
8
class ParameterInjection implements ParameterInjector
9
{
10
    public function __construct()
11
    {
12
        $this->register('Symfony\Component\Console\Input\InputInterface', $this);
13
        $this->register('Symfony\Component\Console\Output\OutputInterface', $this);
14
    }
15
16
    public function register($interfaceName, ParameterInjector $injector)
17
    {
18
        $this->injectors[$interfaceName] = $injector;
0 ignored issues
show
Bug introduced by
The property injectors 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...
19
    }
20
21
    public function args($commandData)
22
    {
23
        return array_merge(
24
            $commandData->injectedInstances(),
25
            $commandData->getArgsAndOptions()
26
        );
27
    }
28
29
    public function injectIntoCommandData($commandData, $injectedClasses)
30
    {
31
        foreach ($injectedClasses as $injectedClass) {
32
            $injectedInstance = $this->getInstanceToInject($commandData, $injectedClass);
33
            $commandData->injectInstance($injectedInstance);
34
        }
35
    }
36
37
    protected function getInstanceToInject(CommandData $commandData, $interfaceName)
38
    {
39
        if (!isset($this->injectors[$interfaceName])) {
40
            return null;
41
        }
42
43
        return $this->injectors[$interfaceName]->get($commandData, $interfaceName);
44
    }
45
46
    public function get(CommandData $commandData, $interfaceName)
47
    {
48
        switch ($interfaceName) {
49
            case 'Symfony\Component\Console\Input\InputInterface':
50
                return $commandData->input();
51
            case 'Symfony\Component\Console\Output\OutputInterface':
52
                return $commandData->output();
53
        }
54
55
        return null;
56
    }
57
}
58