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; |
25
|
|
|
|
26
|
|
|
use byrokrat\giroapp\DependencyInjection\CommandBusProperty; |
27
|
|
|
use byrokrat\giroapp\CommandBus; |
28
|
|
|
use byrokrat\giroapp\Domain\PostalAddress; |
29
|
|
|
use byrokrat\giroapp\Validator; |
30
|
|
|
use Symfony\Component\Console\Command\Command; |
31
|
|
|
use Symfony\Component\Console\Input\InputOption; |
32
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
33
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
34
|
|
|
use Symfony\Component\Console\Helper\QuestionHelper; |
35
|
|
|
|
36
|
|
|
final class EditConsole implements ConsoleInterface |
37
|
|
|
{ |
38
|
|
|
use CommandBusProperty; |
39
|
|
|
use Helper\DonorArgument; |
40
|
|
|
use Helper\DryRun; |
41
|
|
|
|
42
|
|
|
private const OPTIONS = [ |
43
|
|
|
self::OPTION_NAME, |
44
|
|
|
self::OPTION_ADDRESS1, |
45
|
|
|
self::OPTION_ADDRESS2, |
46
|
|
|
self::OPTION_ADDRESS3, |
47
|
|
|
self::OPTION_POSTAL_CODE, |
48
|
|
|
self::OPTION_POSTAL_CITY, |
49
|
|
|
self::OPTION_EMAIL, |
50
|
|
|
self::OPTION_PHONE, |
51
|
|
|
self::OPTION_COMMENT, |
52
|
|
|
]; |
53
|
|
|
|
54
|
|
|
public function configure(Command $command): void |
55
|
|
|
{ |
56
|
|
|
$this->configureDonorArgument($command); |
57
|
|
|
$command->setName('edit'); |
58
|
|
|
$command->setDescription('Edit an existing donor'); |
59
|
|
|
$command->setHelp('Edit a donor in the database.'); |
60
|
|
|
|
61
|
|
|
foreach (self::OPTIONS as $option) { |
62
|
|
|
$command->addOption($option, null, InputOption::VALUE_REQUIRED, self::OPTION_DESCS[$option]); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$command->addOption( |
66
|
|
|
self::OPTION_ATTR_KEY, |
67
|
|
|
null, |
68
|
|
|
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, |
69
|
|
|
self::OPTION_DESCS[self::OPTION_ATTR_KEY] |
70
|
|
|
); |
71
|
|
|
|
72
|
|
|
$command->addOption( |
73
|
|
|
self::OPTION_ATTR_VALUE, |
74
|
|
|
null, |
75
|
|
|
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, |
76
|
|
|
self::OPTION_DESCS[self::OPTION_ATTR_VALUE] |
77
|
|
|
); |
78
|
|
|
|
79
|
|
|
$this->configureDryRun($command); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function execute(InputInterface $input, OutputInterface $output): void |
83
|
|
|
{ |
84
|
|
|
$commandQueue = []; |
85
|
|
|
|
86
|
|
|
$donor = $this->readDonor($input); |
87
|
|
|
|
88
|
|
|
$inputReader = new Helper\InputReader($input, $output, new QuestionHelper()); |
89
|
|
|
|
90
|
|
|
$commandQueue[] = new CommandBus\UpdateName( |
91
|
|
|
$donor, |
92
|
|
|
$inputReader->readOptionalInput( |
93
|
|
|
self::OPTION_NAME, |
94
|
|
|
$donor->getName(), |
95
|
|
|
new Validator\ValidatorCollection( |
96
|
|
|
new Validator\StringValidator(), |
97
|
|
|
new Validator\NotEmptyValidator() |
98
|
|
|
) |
99
|
|
|
) |
100
|
|
|
); |
101
|
|
|
|
102
|
|
|
$commandQueue[] = new CommandBus\UpdatePostalAddress( |
103
|
|
|
$donor, |
104
|
|
|
new PostalAddress( |
105
|
|
|
$inputReader->readOptionalInput( |
106
|
|
|
self::OPTION_ADDRESS1, |
107
|
|
|
$donor->getPostalAddress()->getLine1(), |
108
|
|
|
new Validator\StringValidator() |
109
|
|
|
), |
110
|
|
|
$inputReader->readOptionalInput( |
111
|
|
|
self::OPTION_ADDRESS2, |
112
|
|
|
$donor->getPostalAddress()->getLine2(), |
113
|
|
|
new Validator\StringValidator() |
114
|
|
|
), |
115
|
|
|
$inputReader->readOptionalInput( |
116
|
|
|
self::OPTION_ADDRESS3, |
117
|
|
|
$donor->getPostalAddress()->getLine3(), |
118
|
|
|
new Validator\StringValidator() |
119
|
|
|
), |
120
|
|
|
$inputReader->readOptionalInput( |
121
|
|
|
self::OPTION_POSTAL_CODE, |
122
|
|
|
$donor->getPostalAddress()->getPostalCode(), |
123
|
|
|
new Validator\PostalCodeValidator() |
124
|
|
|
), |
125
|
|
|
$inputReader->readOptionalInput( |
126
|
|
|
self::OPTION_POSTAL_CITY, |
127
|
|
|
$donor->getPostalAddress()->getPostalCity(), |
128
|
|
|
new Validator\StringValidator() |
129
|
|
|
) |
130
|
|
|
) |
131
|
|
|
); |
132
|
|
|
|
133
|
|
|
$commandQueue[] = new CommandBus\UpdateEmail( |
134
|
|
|
$donor, |
135
|
|
|
$inputReader->readOptionalInput(self::OPTION_EMAIL, $donor->getEmail(), new Validator\EmailValidator()) |
136
|
|
|
); |
137
|
|
|
|
138
|
|
|
$commandQueue[] = new CommandBus\UpdatePhone( |
139
|
|
|
$donor, |
140
|
|
|
$inputReader->readOptionalInput(self::OPTION_PHONE, $donor->getPhone(), new Validator\PhoneValidator()) |
141
|
|
|
); |
142
|
|
|
|
143
|
|
|
$commandQueue[] = new CommandBus\UpdateComment( |
144
|
|
|
$donor, |
145
|
|
|
$inputReader->readOptionalInput( |
146
|
|
|
self::OPTION_COMMENT, |
147
|
|
|
$donor->getComment(), |
148
|
|
|
new Validator\StringValidator() |
149
|
|
|
) |
150
|
|
|
); |
151
|
|
|
|
152
|
|
|
foreach ($donor->getAttributes() as $attrKey => $attrValue) { |
153
|
|
|
$commandQueue[] = new CommandBus\UpdateAttribute( |
154
|
|
|
$donor, |
155
|
|
|
$attrKey, |
156
|
|
|
$inputReader->readOptionalInput("attribute.$attrKey", $attrValue, new Validator\StringValidator()) |
157
|
|
|
); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** @var array<string> */ |
161
|
|
|
$attrKeys = $input->getOption(self::OPTION_ATTR_KEY); |
162
|
|
|
|
163
|
|
|
/** @var array<string> */ |
164
|
|
|
$attrValues = $input->getOption(self::OPTION_ATTR_VALUE); |
165
|
|
|
|
166
|
|
|
for ($count = 0;; $count++) { |
167
|
|
|
$attrKey = $inputReader->readInput( |
168
|
|
|
'', |
169
|
|
|
Helper\QuestionFactory::createQuestion('Add an attribute (empty to skip)', $attrKeys[$count] ?? ''), |
170
|
|
|
new Validator\StringValidator() |
171
|
|
|
); |
172
|
|
|
|
173
|
|
|
if (!$attrKey) { |
174
|
|
|
break; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
$commandQueue[] = new CommandBus\UpdateAttribute( |
178
|
|
|
$donor, |
179
|
|
|
$attrKey, |
180
|
|
|
$inputReader->readInput( |
181
|
|
|
'', |
182
|
|
|
Helper\QuestionFactory::createQuestion('Value', $attrValues[$count] ?? ''), |
183
|
|
|
new Validator\StringValidator() |
184
|
|
|
) |
185
|
|
|
); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
foreach ($commandQueue as $command) { |
189
|
|
|
$this->commandBus->handle($command); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
$this->evaluateDryRun($input); |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|