1
|
|
|
<?php |
2
|
|
|
namespace rtens\domin\delivery\cli; |
3
|
|
|
|
4
|
|
|
use rtens\domin\delivery\FieldRegistry; |
5
|
|
|
use rtens\domin\delivery\ParameterReader; |
6
|
|
|
use rtens\domin\Parameter; |
7
|
|
|
|
8
|
|
|
class InteractiveCliParameterReader implements ParameterReader { |
9
|
|
|
|
10
|
|
|
private $fields; |
11
|
|
|
private $console; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @param FieldRegistry $fields <- |
15
|
|
|
* @param Console $console |
16
|
|
|
*/ |
17
|
|
|
public function __construct(FieldRegistry $fields, Console $console) { |
18
|
|
|
$this->fields = $fields; |
19
|
|
|
$this->console = $console; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param Parameter $parameter |
24
|
|
|
* @return boolean |
25
|
|
|
*/ |
26
|
|
|
public function has(Parameter $parameter) { |
27
|
|
|
return true; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param Parameter $parameter |
32
|
|
|
* @return string |
33
|
|
|
* @throws \Exception |
34
|
|
|
*/ |
35
|
|
|
public function read(Parameter $parameter) { |
36
|
|
|
$prompt = $parameter->getName(); |
37
|
|
|
if ($parameter->isRequired()) { |
38
|
|
|
$prompt = $prompt . '*'; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
$field = $this->getField($parameter); |
42
|
|
|
$description = $field->getDescription($parameter); |
43
|
|
|
if ($description !== null) { |
44
|
|
|
$prompt .= ' ' . $description; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$value = $this->console->read($prompt . ': '); |
48
|
|
|
while ($parameter->isRequired() && !$value) { |
49
|
|
|
$value = $this->console->read($prompt . ': '); |
50
|
|
|
} |
51
|
|
|
return $value; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param Parameter $parameter |
56
|
|
|
* @return \rtens\domin\delivery\cli\CliField |
57
|
|
|
* @throws \Exception |
58
|
|
|
*/ |
59
|
|
View Code Duplication |
private function getField(Parameter $parameter) { |
|
|
|
|
60
|
|
|
$field = $this->fields->getField($parameter); |
61
|
|
|
if (!($field instanceof CliField)) { |
62
|
|
|
$fieldClass = get_class($field); |
63
|
|
|
throw new \Exception("Not a CliField [$fieldClass]"); |
64
|
|
|
} |
65
|
|
|
return $field; |
66
|
|
|
} |
67
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.