Prompt::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Copyright © 2017 Toan Nguyen. All rights reserved.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace Gojira\Provider\Console;
10
11
use DateTimeZone;
12
use Gojira\Framework\ObjectManager\ObjectManager;
13
use Symfony\Component\Console\Exception\RuntimeException;
14
use Symfony\Component\Console\Helper\QuestionHelper;
15
use Symfony\Component\Console\Input\InputInterface;
16
use Symfony\Component\Console\Output\OutputInterface;
17
use Symfony\Component\Console\Question\ChoiceQuestion;
18
use Symfony\Component\Console\Question\ConfirmationQuestion;
19
use Symfony\Component\Console\Question\Question;
20
21
/**
22
 * Base class for Gojira console prompt questions.
23
 *
24
 * @package Gojira\Provider\Console
25
 * @author  Toan Nguyen <[email protected]>
26
 */
27
class Prompt
28
{
29
    /**
30
     * @var QuestionHelper
31
     */
32
    protected $prompt;
33
34
    /**
35
     * Prompt constructor.
36
     */
37
    public function __construct()
38
    {
39
        $this->prompt = ObjectManager::create(QuestionHelper::class);
40
    }
41
42
    /**
43
     * Asks a question to the user.
44
     *
45
     * @param InputInterface  $input    An InputInterface instance
46
     * @param OutputInterface $output   An OutputInterface instance
47
     * @param Question        $question The question to ask
48
     *
49
     * @return mixed The user answer
50
     *
51
     * @throws RuntimeException If there is no data to read in the input stream
52
     */
53
    public function ask(InputInterface $input, OutputInterface $output, Question $question)
54
    {
55
        return $this->prompt->ask($input, $output, $question);
56
    }
57
58
    /**
59
     * Get JIRA URL question
60
     *
61
     * @return Question
62
     */
63 View Code Duplication
    public function getJiraUrlQuestion()
64
    {
65
        $question = new Question('<question>Please enter your Jira URL:</question> ');
66
        $question->setValidator(function ($value) {
67
            if (trim($value) === '') {
68
                throw new \Exception('The URL cannot be empty');
69
            }
70
71
            return $value;
72
        });
73
74
        return $question;
75
    }
76
77
    /**
78
     * Get JIRA username question
79
     *
80
     * @return Question
81
     */
82 View Code Duplication
    public function getJiraUsernameQuestion()
83
    {
84
        $question = new Question('<question>Please enter your Jira username:</question> ');
85
        $question->setValidator(function ($value) {
86
            if (trim($value) === '') {
87
                throw new \Exception('The username cannot be empty');
88
            }
89
90
            return $value;
91
        });
92
93
        return $question;
94
    }
95
96
    /**
97
     * Get JIRA password question
98
     *
99
     * @return Question
100
     */
101
    public function getJiraPasswordQuestion()
102
    {
103
        $question = new Question('<question>Please enter your Jira password:</question> ');
104
        $question->setValidator(function ($value) {
105
            if (trim($value) === '') {
106
                throw new \Exception('The password cannot be empty');
107
            }
108
109
            return $value;
110
        });
111
        $question->setHidden(true);
112
        $question->setMaxAttempts(20);
113
114
        return $question;
115
    }
116
117
    /**
118
     * Choose server timezone question
119
     *
120
     * @return ChoiceQuestion
121
     */
122
    public function chooseTimezoneQuestion()
123
    {
124
        $timezones = DateTimeZone::listIdentifiers();
125
        $question = new ChoiceQuestion(
126
            '<question>Please choose your server timezone (defaults to Australia/Sydney):</question> ',
127
            $timezones,
128
            313
129
        );
130
        $question->setErrorMessage('Timezone %s is invalid');
131
132
        return $question;
133
    }
134
135
    /**
136
     * Select cache mode question
137
     *
138
     * @return ConfirmationQuestion
139
     */
140
    public function chooseCacheModeQuestion()
141
    {
142
        $question = new ConfirmationQuestion('<question>Please select HttpClient cache mode:</question> ', false);
143
144
        return $question;
145
    }
146
}
147