Completed
Push — dev ( a8234e...9312cf )
by Zach
04:40
created

Command::addInput()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 10
Ratio 100 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 10
loc 10
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Yarak\Console;
4
5
use Yarak\Console\Input\Input;
6
use Yarak\Console\SignatureParser;
7
use Yarak\Console\Output\SymfonyOutput;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Output\OutputInterface;
10
use Symfony\Component\Console\Command\Command as SymfonyCommand;
11
12
abstract class Command extends SymfonyCommand
13
{
14
    /**
15
     * Symfony console output.
16
     *
17
     * @var SymfonyOutput
18
     */
19
    protected $output;
20
21
    /**
22
     * Symfony input implementation.
23
     *
24
     * @var InputInterface
25
     */
26
    protected $input;
27
28
    /**
29
     * The command description.
30
     *
31
     * @var string
32
     */
33
    protected $description;
34
35
    /**
36
     * Command signature.
37
     *
38
     * @var null|string
39
     */
40
    protected $signature = null;
41
42
    /**
43
     * Configure the command if signature is set.
44
     */
45
    protected function configure()
46
    {
47
        if (!is_null($this->signature)) {
48
            $parser = new SignatureParser($this);
49
50
            $parser->parse($this->signature);
51
52
            $this->setDescription($this->description);
53
        }
54
    }
55
56
    /**
57
     * Execute the command.
58
     *
59
     * @param InputInterface  $input
60
     * @param OutputInterface $output
61
     */
62
    protected function execute(InputInterface $input, OutputInterface $output)
63
    {
64
        $this->output = new SymfonyOutput($output);
65
66
        $this->input = $input;
67
68
        if (method_exists($this, 'handle')) {
69
            $this->handle();
70
        }
71
    }
72
73
    /**
74
     * Return the output implementation.
75
     *
76
     * @return SymfonyOutput
77
     */
78
    protected function getOutput()
79
    {
80
        return $this->output;
81
    }
82
83
    /**
84
     * Get the value of a command argument.
85
     *
86
     * @param string $key
87
     *
88
     * @return string|array
89
     */
90
    protected function argument($key = null)
91
    {
92
        if (is_null($key)) {
93
            return $this->input->getArguments();
94
        }
95
96
        return $this->input->getArgument($key);
97
    }
98
99
    /**
100
     * Determine if the given argument is present.
101
     *
102
     * @param string|int $name
103
     *
104
     * @return bool
105
     */
106
    protected function hasArgument($name)
107
    {
108
        return $this->input->hasArgument($name);
109
    }
110
111
    /**
112
     * Get the value of a command option.
113
     *
114
     * @param string $key
115
     *
116
     * @return string|array
117
     */
118
    protected function option($key = null)
119
    {
120
        if (is_null($key)) {
121
            return $this->input->getOptions();
122
        }
123
124
        return $this->input->getOption($key);
125
    }
126
127
    /**
128
     * Determine if the given option is present.
129
     *
130
     * @param string $name
131
     *
132
     * @return bool
133
     */
134
    protected function hasOption($name)
135
    {
136
        return $this->input->hasOption($name);
137
    }
138
139
    /**
140
     * Add input to the command.
141
     *
142
     * @param Input $input
143
     */
144 View Code Duplication
    public function addInput(Input $input)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
145
    {
146
        $reflection = new \ReflectionClass($input);
147
148
        $method = 'add'.$reflection->getShortName();
149
150
        if (method_exists($this, $method)) {
151
            $this->$method(...array_values($input->getAttributes()));
152
        }
153
    }
154
}
155