|
1
|
|
|
<?php |
|
2
|
|
|
namespace Samurai\Project\Question; |
|
3
|
|
|
|
|
4
|
|
|
use Samurai\Project\Author; |
|
5
|
|
|
use Samurai\Task\ITask; |
|
6
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
7
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
8
|
|
|
use Symfony\Component\Console\Question\ConfirmationQuestion; |
|
9
|
|
|
use Symfony\Component\Console\Question\Question as SimpleQuestion; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class AuthorQuestion |
|
13
|
|
|
* @package Samurai\Project\Question |
|
14
|
|
|
* @author Raphaël Lefebvre <[email protected]> |
|
15
|
|
|
*/ |
|
16
|
|
|
class AuthorQuestion extends Question |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @param InputInterface $input |
|
20
|
|
|
* @param OutputInterface $output |
|
21
|
|
|
* @return int |
|
22
|
|
|
* @throws \Exception |
|
23
|
|
|
*/ |
|
24
|
5 |
|
public function execute(InputInterface $input, OutputInterface $output) |
|
25
|
|
|
{ |
|
26
|
5 |
|
$this->setInitialAuthor($input, $output); |
|
27
|
5 |
|
$this->setAdditionalAuthors($input, $output); |
|
28
|
5 |
|
return $this->getProject()->getAuthors()->count() ? ITask::NO_ERROR_CODE : ITask::BLOCKING_ERROR_CODE; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param InputInterface $input |
|
33
|
|
|
* @param OutputInterface $output |
|
34
|
|
|
*/ |
|
35
|
5 |
|
private function setInitialAuthor(InputInterface $input, OutputInterface $output) |
|
36
|
5 |
|
{ |
|
37
|
|
|
try { |
|
38
|
5 |
|
$this->setAuthorFromGit($input, $output); |
|
39
|
5 |
|
} catch (\Exception $e) { |
|
40
|
1 |
|
$this->setAuthorFromQuestion($input, $output); |
|
41
|
|
|
} |
|
42
|
5 |
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param InputInterface $input |
|
46
|
|
|
* @param OutputInterface $output |
|
47
|
|
|
*/ |
|
48
|
5 |
|
private function setAuthorFromGit(InputInterface $input, OutputInterface $output) |
|
49
|
|
|
{ |
|
50
|
5 |
|
$author = new Author($this->retrieveGitUser()); |
|
51
|
4 |
|
if ($this->confirmAuthor($input, $output, $author)) { |
|
52
|
4 |
|
$this->getProject()->addAuthor($author); |
|
53
|
4 |
|
} else { |
|
54
|
|
|
$this->setAuthorFromQuestion($input, $output); |
|
55
|
|
|
} |
|
56
|
4 |
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param InputInterface $input |
|
60
|
|
|
* @param OutputInterface $output |
|
61
|
|
|
*/ |
|
62
|
3 |
|
private function setAuthorFromQuestion(InputInterface $input, OutputInterface $output) |
|
63
|
|
|
{ |
|
64
|
3 |
|
$this->getProject()->addAuthor( |
|
65
|
3 |
|
$this->ask( |
|
66
|
3 |
|
$input, |
|
67
|
3 |
|
$output, |
|
68
|
3 |
|
$this->buildAuthorQuestion() |
|
69
|
3 |
|
) |
|
70
|
3 |
|
); |
|
71
|
3 |
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param InputInterface $input |
|
75
|
|
|
* @param OutputInterface $output |
|
76
|
|
|
*/ |
|
77
|
5 |
|
private function setAdditionalAuthors(InputInterface $input, OutputInterface $output) |
|
78
|
|
|
{ |
|
79
|
5 |
|
while ($this->confirmAdditionalAuthor($input, $output)) { |
|
80
|
3 |
|
$this->setAuthorFromQuestion($input, $output); |
|
81
|
3 |
|
} |
|
82
|
5 |
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @return SimpleQuestion |
|
86
|
|
|
*/ |
|
87
|
3 |
|
private function buildAuthorQuestion() |
|
88
|
|
|
{ |
|
89
|
3 |
|
$question = new SimpleQuestion('<question>Enter the author (name <[email protected]>):</question>'); |
|
90
|
3 |
|
$question->setValidator($this->buildValidator()); |
|
91
|
3 |
|
$question->setMaxAttempts(3); |
|
92
|
|
|
|
|
93
|
3 |
|
return $question; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @return callable |
|
98
|
|
|
*/ |
|
99
|
|
|
private function buildValidator() |
|
100
|
|
|
{ |
|
101
|
3 |
|
return function ($answer) { |
|
102
|
|
|
return new Author($answer); |
|
103
|
3 |
|
}; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @param InputInterface $input |
|
108
|
|
|
* @param OutputInterface $output |
|
109
|
|
|
* @return mixed |
|
110
|
|
|
*/ |
|
111
|
5 |
|
private function confirmAdditionalAuthor(InputInterface $input, OutputInterface $output) |
|
112
|
|
|
{ |
|
113
|
5 |
|
return $this->ask( |
|
114
|
5 |
|
$input, |
|
115
|
5 |
|
$output, |
|
116
|
5 |
|
new ConfirmationQuestion('<question>Do you want to add another author?[n]</question>', false) |
|
117
|
5 |
|
); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @param InputInterface $input |
|
122
|
|
|
* @param OutputInterface $output |
|
123
|
|
|
* @param Author $author |
|
124
|
|
|
* @return mixed |
|
125
|
|
|
*/ |
|
126
|
4 |
|
private function confirmAuthor(InputInterface $input, OutputInterface $output, Author $author) |
|
127
|
|
|
{ |
|
128
|
4 |
|
return $this->ask( |
|
129
|
4 |
|
$input, |
|
130
|
4 |
|
$output, |
|
131
|
4 |
|
new ConfirmationQuestion('<question>Do you confirm this author "'.$author.'"?[y]</question>') |
|
132
|
4 |
|
); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @return string |
|
137
|
|
|
*/ |
|
138
|
5 |
|
private function retrieveGitUser() |
|
139
|
|
|
{ |
|
140
|
5 |
|
$config = $this->getService('git')->config(); |
|
141
|
5 |
|
if(isset($config['user.name']) && isset($config['user.email'])){ |
|
142
|
4 |
|
return $config['user.name'] . ' <' . $config['user.email'] . '>'; |
|
143
|
|
|
} |
|
144
|
1 |
|
return ''; |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
|