Completed
Push — master ( f513d8...2c6f37 )
by Pierre
04:55
created

AbstractBaseCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Ptondereau\PackMe\Commands;
4
5
use Symfony\Component\Console\Helper\HelperSet;
6
use Symfony\Component\Console\Helper\QuestionHelper;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Output\ConsoleOutputInterface;
9
use Symfony\Component\Console\Output\OutputInterface;
10
use Symfony\Component\Console\Question\Question;
11
12
/**
13
 * Class BaseCommand.
14
 */
15
abstract class AbstractBaseCommand
16
{
17
    /**
18
     * @var InputInterface
19
     */
20
    protected $input;
21
22
    /**
23
     * @var OutputInterface
24
     */
25
    protected $output;
26
27
    /**
28
     * @var HelperSet
29
     */
30
    protected $helperSet;
31
32
    /**
33
     * BaseCommand constructor.
34
     *
35
     * @param HelperSet $helperSet
36
     */
37 9
    public function __construct(HelperSet $helperSet)
38
    {
39 9
        $this->helperSet = $helperSet;
40 9
    }
41
42
    /**
43
     * @param string   $question
44
     * @param \Closure $validator
45
     * @param null     $attempts
46
     * @param null     $default
47
     *
48
     * @return string
49
     */
50
    protected function askAndValidate($question, $validator, $attempts = null, $default = null)
51
    {
52
        /** @var QuestionHelper $helper */
53
        $helper = $this->helperSet->get('question');
54
        $question = new Question($question, $default);
55
        $question->setValidator($validator);
56
        $question->setMaxAttempts($attempts);
57
58
        return $helper->ask($this->input, $this->getErrorOutput(), $question);
59
    }
60
61
    /**
62
     * @param string $question
63
     * @param null   $default
64
     *
65
     * @return string
66
     */
67
    protected function ask($question, $default = null)
68
    {
69
        /** @var \Symfony\Component\Console\Helper\QuestionHelper $helper */
70
        $helper = $this->helperSet->get('question');
71
        $question = new Question($question, $default);
72
73
        return $helper->ask($this->input, $this->getErrorOutput(), $question);
74
    }
75
76
    /**
77
     * @return OutputInterface
78
     */
79
    private function getErrorOutput()
80
    {
81
        if ($this->output instanceof ConsoleOutputInterface) {
82
            return $this->output->getErrorOutput();
83
        }
84
85
        return $this->output;
86
    }
87
}
88