Completed
Push — master ( 199635...738ba0 )
by Dorian
01:26
created

CommandLineInterface::writeln()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
1
<?php declare(strict_types=1);
2
3
namespace App\Cli;
4
5
use App\UI\UserInterface;
6
use Symfony\Component\Console\Command\Command;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Output\OutputInterface;
9
use Symfony\Component\Console\Question\ConfirmationQuestion;
10
11
final class CommandLineInterface implements UserInterface
12
{
13
    /** @var Command */
14
    private $command;
15
16
    /** @var InputInterface */
17
    private $input;
18
19
    /** @var OutputInterface */
20
    private $output;
21
22
    /**
23
     * @param Command $command
24
     * @param InputInterface $input
25
     * @param OutputInterface $output
26
     */
27
    public function __construct(Command $command, InputInterface $input, OutputInterface $output)
28
    {
29
        $this->command = $command;
30
        $this->input = $input;
31
        $this->output = $output;
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function askConfirmation(string $message, bool $default = true): bool
38
    {
39
        /** @var \Symfony\Component\Console\Helper\QuestionHelper $questionHelper */
40
        /** @noinspection ExceptionsAnnotatingAndHandlingInspection */
41
        $questionHelper = $this->command->getHelper('question');
42
43
        /** @noinspection ExceptionsAnnotatingAndHandlingInspection */
44
        $answer = $questionHelper->ask($this->input, $this->output, new ConfirmationQuestion($message, $default));
45
46
        if ($answer && $this->input->isInteractive()) {
47
            $this->writeln('');
48
        }
49
50
        return $answer;
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function write($messages, bool $newLine = false): void
57
    {
58
        $this->output->write($messages, $newLine);
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public function writeln($messages, int $options = 0): void
65
    {
66
        $this->output->writeln($messages, $options);
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function listing(array $messages, int $indentation = 0): void
73
    {
74
        $messages = array_map(function ($message) use ($indentation) {
75
            return sprintf(str_repeat(' ', $indentation).' * %s', $message);
76
        }, $messages);
77
78
        $this->write(PHP_EOL);
79
        $this->writeln($messages);
80
        $this->write(PHP_EOL);
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86
    public function forceOutput(callable $callable): void
87
    {
88
        $verbosity = $this->output->getVerbosity();
89
        $this->output->setVerbosity(OutputInterface::VERBOSITY_NORMAL);
90
91
        $callable();
92
93
        $this->output->setVerbosity($verbosity);
94
    }
95
96
    /**
97
     * {@inheritdoc}
98
     */
99
    public function indent(int $indentation = 1, int $characters = 2, string $character = ' '): string
100
    {
101
        return str_repeat(str_repeat($character, $characters), $indentation);
102
    }
103
104
    /**
105
     * {@inheritdoc}
106
     */
107
    public function confirm(bool $confirmationDefault = true): bool
108
    {
109
        $confirmationQuestion = '<question>Continue?</question> ('.($confirmationDefault ? 'Y/n' : 'y/N').') ';
110
        if (!$this->askConfirmation($confirmationQuestion, $confirmationDefault)) {
111
            $this->writeln(PHP_EOL.$this->indent().'Not doing anything...');
112
113
            return false;
114
        }
115
116
        return true;
117
    }
118
119
    /**
120
     * {@inheritdoc}
121
     */
122
    public function logError(string $error, array &$errors): void
123
    {
124
        $this->writeln('<error>An error occurred.</error>');
125
        $errors[] = $error;
126
    }
127
128
    /**
129
     * {@inheritdoc}
130
     */
131
    public function displayErrors(
132
        array $errors,
133
        string $process,
134
        string $type = 'error',
135
        int $indentation = 0
136
    ): void {
137
        $nbErrors = \count($errors);
138
        if ($nbErrors > 0) {
139
            $this->forceOutput(function () use ($nbErrors, $errors, $process, $type, $indentation) {
140
                $this->writeln(
141
                    PHP_EOL.PHP_EOL.'<'.$type.'>There were '.$nbErrors.' errors during the '.$process.' :</'.$type.'>'
142
                );
143
144
                $this->listing($errors, $indentation);
145
            });
146
        }
147
    }
148
}
149