InputReader::readInput()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 11
c 1
b 0
f 0
nc 6
nop 3
dl 0
loc 21
rs 9.9
1
<?php
2
3
/**
4
 * This file is part of byrokrat\giroapp.
5
 *
6
 * byrokrat\giroapp is free software: you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License as published
8
 * by the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * byrokrat\giroapp is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with byrokrat\giroapp. If not, see <http://www.gnu.org/licenses/>.
18
 *
19
 * Copyright 2016-21 Hannes Forsgård
20
 */
21
22
declare(strict_types=1);
23
24
namespace byrokrat\giroapp\Console\Helper;
25
26
use byrokrat\giroapp\Console\ConsoleInterface;
27
use byrokrat\giroapp\Validator\ValidatorInterface;
28
use Symfony\Component\Console\Input\InputInterface;
29
use Symfony\Component\Console\Output\OutputInterface;
30
use Symfony\Component\Console\Helper\QuestionHelper;
31
use Symfony\Component\Console\Question\ConfirmationQuestion;
32
use Symfony\Component\Console\Question\Question;
33
34
/**
35
 * Read option or fallback to interactive question
36
 */
37
class InputReader
38
{
39
    /**
40
     * @var InputInterface
41
     */
42
    private $input;
43
44
    /**
45
     * @var OutputInterface
46
     */
47
    private $output;
48
49
    /**
50
     * @var QuestionHelper
51
     */
52
    private $questionHelper;
53
54
    public function __construct(InputInterface $input, OutputInterface $output, QuestionHelper $questionHelper)
55
    {
56
        $this->input = $input;
57
        $this->output = $output;
58
        $this->questionHelper = $questionHelper;
59
    }
60
61
    public function confirm(string $question, bool $default = true): bool
62
    {
63
        return !!$this->questionHelper->ask(
64
            $this->input,
65
            $this->output,
66
            new ConfirmationQuestion($question, $default)
67
        );
68
    }
69
70
    public function readInput(string $key, Question $question, ValidatorInterface $validator): string
71
    {
72
        $value = null;
73
74
        if ($this->input->hasOption($key)) {
75
            $value = $this->input->getOption($key);
76
        }
77
78
        if (is_string($value)) {
79
            return $validator->validate($key, $value);
80
        }
81
82
        $value = (string)$this->questionHelper->ask(
83
            $this->input,
84
            $this->output,
85
            $question->setValidator(function ($answer) use ($key, $validator) {
86
                return $validator->validate($key, (string)$answer);
87
            })
88
        );
89
90
        return $this->input->isInteractive() ? $value : $validator->validate($key, $value);
91
    }
92
93
    public function readOptionalInput(string $key, string $default, ValidatorInterface $validator): string
94
    {
95
        $value = null;
96
97
        if ($this->input->hasOption($key)) {
98
            $value = $this->input->getOption($key);
99
        }
100
101
        if (is_string($value)) {
102
            return $validator->validate($key, $value);
103
        }
104
105
        $desc = ConsoleInterface::OPTION_DESCS[$key] ?? $key;
106
107
        // Confirm if user wants to edit, return default if not
108
        if (!$this->confirm("$desc: <comment>$default</comment>\nEdit [<info>y/N</info>]? ", false)) {
109
            return $default;
110
        }
111
112
        $value = (string)$this->questionHelper->ask(
113
            $this->input,
114
            $this->output,
115
            (new Question("New $desc: "))->setValidator(function ($answer) use ($key, $validator) {
116
                return $validator->validate($key, (string)$answer);
117
            })
118
        );
119
120
        return $this->input->isInteractive() ? $value : $validator->validate($key, $value);
121
    }
122
}
123