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