Completed
Pull Request — master (#105)
by Mikołaj
04:26 queued 01:11
created

AbstractConfigCustomizerPlugin   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 95.65%

Importance

Changes 0
Metric Value
dl 0
loc 57
ccs 22
cts 23
cp 0.9565
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B ask() 0 17 7
A printTitle() 0 13 1
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
        }
36
        do {
37 6
            $value = $this->questionHelper->ask($input, $output, new Question(
38 6
                '<question>' . $text . ':</question> ',
39 6
                $default
40
            ));
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
        ]);
65 13
    }
66
}
67