Completed
Pull Request — master (#206)
by
unknown
05:26
created

AbstractSubCommand::hasFlagOrOptionalBoolOption()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 13
rs 9.4285
cc 3
eloc 7
nc 3
nop 2
1
<?php
2
3
namespace N98\Magento\Command\SubCommand;
4
5
use N98\Magento\Command\AbstractMagentoCommand;
6
use Symfony\Component\Console\Helper\DialogHelper;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Output\OutputInterface;
9
10
abstract class AbstractSubCommand implements SubCommandInterface
11
{
12
    /**
13
     * @var ConfigBag
14
     */
15
    protected $config;
16
17
    /**
18
     * @var array
19
     */
20
    protected $commandConfig;
21
22
    /**
23
     * @var InputInterface
24
     */
25
    protected $input;
26
27
    /**
28
     * @var OutputInterface
29
     */
30
    protected $output;
31
32
    /**
33
     * @var AbstractMagentoCommand
34
     */
35
    protected $command;
36
37
    /**
38
     * @param ConfigBag $config
39
     */
40
    public function setConfig(ConfigBag $config)
41
    {
42
        $this->config = $config;
43
    }
44
45
    /**
46
     * @param array $commandConfig
47
     */
48
    public function setCommandConfig(array $commandConfig)
49
    {
50
        $this->commandConfig = $commandConfig;
51
    }
52
53
    /**
54
     * @param InputInterface $input
55
     */
56
    public function setInput(InputInterface $input)
57
    {
58
        $this->input = $input;
59
    }
60
61
    /**
62
     * @param OutputInterface $output
63
     */
64
    public function setOutput(OutputInterface $output)
65
    {
66
        $this->output = $output;
67
    }
68
69
    /**
70
     * @return AbstractMagentoCommand
71
     */
72
    public function getCommand()
73
    {
74
        return $this->command;
75
    }
76
77
    /**
78
     * @param AbstractMagentoCommand $command
79
     */
80
    public function setCommand(AbstractMagentoCommand $command)
81
    {
82
        $this->command = $command;
83
    }
84
85
    /**
86
     * @return void
87
     */
88
    abstract public function execute();
89
90
    /**
91
     * @param string $name of the optional option
92
     * @param string $question to ask in case the option is not available
93
     * @param bool $default value (true means yes, false no), optional, defaults to true
94
     * @return bool
95
     */
96
    final protected function getOptionalBooleanOption($name, $question, $default = true)
97
    {
98
        if ($this->input->getOption($name) !== null) {
99
            $flag = $this->getCommand()->parseBoolOption($this->input->getOption($name));
100
101
            return $flag;
102
        } else {
103
            /** @var $dialog DialogHelper */
104
            $dialog = $this->getCommand()->getHelper('dialog');
105
106
            $flag = $dialog->askConfirmation(
107
                $this->output,
108
                sprintf('<question>%s</question> <comment>[%s]</comment>: ', $question, $default ? 'y' : 'n'),
109
                $default
110
            );
111
112
            return $flag;
113
        }
114
    }
115
116
    /**
117
     * @param string $name of flag/option
118
     * @param bool $default value for flag/option if set but with no value
119
     * @return bool
120
     */
121
    final protected function hasFlagOrOptionalBoolOption($name, $default = true)
122
    {
123
        if (!$this->input->hasOption($name)) {
124
            return false;
125
        }
126
127
        $value = $this->input->getOption($name);
128
        if (null === $value) {
129
            return (bool) $default;
130
        }
131
132
        return (bool) $this->getCommand()->parseBoolOption($value);
133
    }
134
}
135