Completed
Push — master ( 102563...f3da36 )
by Mihail
03:16
created

Command::run()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 4
nc 1
nop 2
1
<?php
2
3
namespace Ffcms\Console;
4
5
use Ffcms\Core\Helper\Type\Str;
6
use Symfony\Component\Console\Command\Command as SymfonyCommand;
7
use Symfony\Component\Console\Helper\SymfonyQuestionHelper;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Output\OutputInterface;
10
use Symfony\Component\Console\Question\ConfirmationQuestion;
11
use Symfony\Component\Console\Question\Question;
12
13
/**
14
 * Class Command. Extend symfony command to simplify usage
15
 * @package Ffcms\Console
16
 */
17
class Command extends SymfonyCommand
18
{
19
    /** @var InputInterface */
20
    private $input;
21
    /** @var OutputInterface */
22
    private $output;
23
    /** @var string|null */
24
    public $dbConnection = null;
25
26
    /**
27
     * Set database connection name to use not with only default connection
28
     * @param string|null $name
29
     */
30
    public function setDbConnection($name = null)
31
    {
32
        $this->dbConnection = $name;
33
    }
34
35
    /**
36
     * Ask string param from stdin php input
37
     * @param string $question
38
     * @param string|null $default
39
     * @return string
40
     */
41
    public function ask($question, $default = null)
42
    {
43
        $que = new Question($question, $default);
44
        $helper = new SymfonyQuestionHelper();
45
        return $helper->ask($this->input, $this->output, $que);
46
    }
47
48
    /**
49
     * Ask confirmation for question (yes/no)
50
     * @param string $question
51
     * @param bool $default
52
     * @return string
53
     */
54
    public function confirm($question, $default = false)
55
    {
56
        $que = new ConfirmationQuestion($question, $default);
57
        $helper = new SymfonyQuestionHelper();
58
        return $helper->ask($this->input, $this->output, $que);
59
    }
60
61
    /**
62
     * Catch input & output instances inside class
63
     * @param InputInterface $input
64
     * @param OutputInterface $output
65
     * @return int
66
     */
67
    public function run(InputInterface $input, OutputInterface $output)
68
    {
69
        $this->input = $input;
70
        $this->output = $output;
71
        return parent::run($input, $output);
72
    }
73
74
    /**
75
     * Get input option value or ask it if empty
76
     * @param string $option
77
     * @param string $question
78
     * @param string|null $default
79
     * @return string
80
     */
81
    public function optionOrAsk($option, $question, $default = null)
82
    {
83
        $value = $this->input->getOption($option);
84
        if ($value === null || Str::likeEmpty($value)) {
85
            $value = $this->ask($question, $default);
86
        }
87
88
        return $value;
89
    }
90
91
    /**
92
     * Get input option value
93
     * @param string $name
94
     * @return string|null
95
     */
96
    public function option($name) {
97
        return $this->input->getOption($name);
98
    }
99
}