Completed
Push — master ( b53e51...1009f9 )
by Alejandro
04:13 queued 38s
created

AbstractConfigCustomizerPlugin::__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 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Shlinkio\Shlink\CLI\Install\Plugin;
3
4
use Symfony\Component\Console\Exception\RuntimeException;
5
use Symfony\Component\Console\Helper\QuestionHelper;
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\Console\Output\OutputInterface;
8
use Symfony\Component\Console\Question\Question;
9
10
abstract class AbstractConfigCustomizerPlugin implements ConfigCustomizerPluginInterface
11
{
12
    /**
13
     * @var QuestionHelper
14
     */
15
    protected $questionHelper;
16
17 14
    public function __construct(QuestionHelper $questionHelper)
18
    {
19 14
        $this->questionHelper = $questionHelper;
20 14
    }
21
22
    /**
23
     * @param InputInterface $input
24
     * @param OutputInterface $output
25
     * @param string $text
26
     * @param string|null $default
27
     * @param bool $allowEmpty
28
     * @return string
29
     * @throws RuntimeException
30
     */
31 6
    protected function ask(InputInterface $input, OutputInterface $output, $text, $default = null, $allowEmpty = false)
32
    {
33 6
        if ($default !== null) {
34 2
            $text .= ' (defaults to ' . $default . ')';
35 2
        }
36
        do {
37 6
            $value = $this->questionHelper->ask($input, $output, new Question(
38 6
                '<question>' . $text . ':</question> ',
39
                $default
40 6
            ));
41 6
            if (empty($value) && ! $allowEmpty) {
42
                $output->writeln('<error>Value can\'t be empty</error>');
43
            }
44 6
        } while (empty($value) && $default === null && ! $allowEmpty);
45
46 6
        return $value;
47
    }
48
49
    /**
50
     * @param OutputInterface $output
51
     * @param string $text
52
     */
53 13
    protected function printTitle(OutputInterface $output, $text)
54
    {
55 13
        $text = trim($text);
56 13
        $length = strlen($text) + 4;
57 13
        $header = str_repeat('*', $length);
58
59 13
        $output->writeln([
60 13
            '',
61 13
            '<info>' . $header . '</info>',
62 13
            '<info>* ' . strtoupper($text) . ' *</info>',
63 13
            '<info>' . $header . '</info>',
64 13
        ]);
65 13
    }
66
}
67