Completed
Push — master ( 4deee6...b383ba )
by Zach
05:43 queued 03:44
created

Command   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 153
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 5
dl 0
loc 153
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 10 2
A execute() 0 10 2
A getOutput() 0 4 1
A getOutputInterface() 0 4 1
A argument() 0 8 2
A hasArgument() 0 4 1
A option() 0 8 2
A hasOption() 0 4 1
A addInput() 0 10 2
1
<?php
2
3
namespace Yarak\Console;
4
5
use Yarak\Console\Input\Input;
6
use Yarak\Console\Output\SymfonyOutput;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Output\OutputInterface;
9
use Symfony\Component\Console\Command\Command as SymfonyCommand;
10
11
abstract class Command extends SymfonyCommand
12
{
13
    /**
14
     * Symfony console output.
15
     *
16
     * @var SymfonyOutput
17
     */
18
    protected $output;
19
20
    /**
21
     * Symfony input implementation.
22
     *
23
     * @var InputInterface
24
     */
25
    protected $input;
26
27
    /**
28
     * The command description.
29
     *
30
     * @var string
31
     */
32
    protected $description;
33
34
    /**
35
     * Command signature.
36
     *
37
     * @var null|string
38
     */
39
    protected $signature = null;
40
41
    /**
42
     * Configure the command if signature is set.
43
     */
44
    protected function configure()
45
    {
46
        if (!is_null($this->signature)) {
47
            $parser = new SignatureParser($this);
48
49
            $parser->parse($this->signature);
50
51
            $this->setDescription($this->description);
52
        }
53
    }
54
55
    /**
56
     * Execute the command.
57
     *
58
     * @param InputInterface  $input
59
     * @param OutputInterface $output
60
     */
61
    protected function execute(InputInterface $input, OutputInterface $output)
62
    {
63
        $this->output = new SymfonyOutput($output);
64
65
        $this->input = $input;
66
67
        if (method_exists($this, 'handle')) {
68
            $this->handle();
69
        }
70
    }
71
72
    /**
73
     * Return the output implementation.
74
     *
75
     * @return SymfonyOutput
76
     */
77
    protected function getOutput()
78
    {
79
        return $this->output;
80
    }
81
82
    /**
83
     * Get root symfony output implementation.
84
     *
85
     * @return OutputInterface
86
     */
87
    protected function getOutputInterface()
88
    {
89
        return $this->getOutput()->getOutput();
90
    }
91
92
    /**
93
     * Get the value of a command argument.
94
     *
95
     * @param string $key
96
     *
97
     * @return string|array
98
     */
99
    protected function argument($key = null)
100
    {
101
        if (is_null($key)) {
102
            return $this->input->getArguments();
103
        }
104
105
        return $this->input->getArgument($key);
106
    }
107
108
    /**
109
     * Determine if the given argument is present.
110
     *
111
     * @param string|int $name
112
     *
113
     * @return bool
114
     */
115
    protected function hasArgument($name)
116
    {
117
        return $this->input->hasArgument($name);
118
    }
119
120
    /**
121
     * Get the value of a command option.
122
     *
123
     * @param string $key
124
     *
125
     * @return string|array
126
     */
127
    protected function option($key = null)
128
    {
129
        if (is_null($key)) {
130
            return $this->input->getOptions();
131
        }
132
133
        return $this->input->getOption($key);
134
    }
135
136
    /**
137
     * Determine if the given option is present.
138
     *
139
     * @param string $name
140
     *
141
     * @return bool
142
     */
143
    protected function hasOption($name)
144
    {
145
        return $this->input->hasOption($name);
146
    }
147
148
    /**
149
     * Add input to the command.
150
     *
151
     * @param Input $input
152
     */
153
    public function addInput(Input $input)
154
    {
155
        $reflection = new \ReflectionClass($input);
156
157
        $method = 'add'.$reflection->getShortName();
158
159
        if (method_exists($this, $method)) {
160
            $this->$method(...array_values($input->getAttributes()));
161
        }
162
    }
163
}
164