1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
/** |
4
|
|
|
* /src/Command/User/UserHelper.php |
5
|
|
|
* |
6
|
|
|
* @author TLe, Tarmo Leppänen <[email protected]> |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace App\Command\User; |
10
|
|
|
|
11
|
|
|
use App\Entity\User; |
12
|
|
|
use App\Entity\UserGroup; |
13
|
|
|
use App\Resource\UserGroupResource; |
14
|
|
|
use App\Resource\UserResource; |
15
|
|
|
use Closure; |
16
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
17
|
|
|
use Throwable; |
18
|
|
|
use function array_map; |
19
|
|
|
use function sprintf; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class UserHelper |
23
|
|
|
* |
24
|
|
|
* @package App\Command\User |
25
|
|
|
* @author TLe, Tarmo Leppänen <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
class UserHelper |
28
|
|
|
{ |
29
|
|
|
public function __construct( |
30
|
|
|
private readonly UserResource $userResource, |
31
|
|
|
private readonly UserGroupResource $userGroupResource, |
32
|
|
|
) { |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Method to get user entity. Also note that this may return a null in |
37
|
|
|
* cases that user do not want to make any changes to users. |
38
|
|
|
* |
39
|
|
|
* @throws Throwable |
40
|
|
|
*/ |
41
|
|
|
public function getUser(SymfonyStyle $io, string $question): ?User |
42
|
|
|
{ |
43
|
|
|
$found = false; |
44
|
|
|
$user = null; |
45
|
|
|
|
46
|
|
|
while (!$found) { |
47
|
|
|
$user = $this->getUserEntity($io, $question); |
48
|
|
|
|
49
|
|
|
if (!$user instanceof User) { |
50
|
|
|
break; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$found = $this->isCorrectUser($io, $user); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return $user; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Method to get user group entity. Also note that this may return a null |
61
|
|
|
* in cases that user do not want to make any changes to user groups. |
62
|
|
|
* |
63
|
|
|
* @throws Throwable |
64
|
|
|
*/ |
65
|
|
|
public function getUserGroup(SymfonyStyle $io, string $question): ?UserGroup |
66
|
|
|
{ |
67
|
|
|
$found = false; |
68
|
|
|
$userGroup = null; |
69
|
|
|
|
70
|
|
|
while (!$found) { |
71
|
|
|
$userGroup = $this->getUserGroupEntity($io, $question); |
72
|
|
|
|
73
|
|
|
if (!$userGroup instanceof UserGroup) { |
74
|
|
|
break; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$found = $this->isCorrectUserGroup($io, $userGroup); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $userGroup; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Method to get User entity. Within this user will be asked which User |
85
|
|
|
* entity he/she wants to process with. |
86
|
|
|
* |
87
|
|
|
* @throws Throwable |
88
|
|
|
*/ |
89
|
|
|
private function getUserEntity(SymfonyStyle $io, string $question): ?User |
90
|
|
|
{ |
91
|
|
|
$choices = []; |
92
|
|
|
$iterator = $this->getUserIterator($choices); |
93
|
|
|
|
94
|
|
|
array_map( |
95
|
|
|
$iterator, |
96
|
|
|
$this->userResource->find(orderBy: [ |
97
|
|
|
'username' => 'asc', |
98
|
|
|
]) |
99
|
|
|
); |
100
|
|
|
|
101
|
|
|
$choices['Exit'] = 'Exit command'; |
102
|
|
|
|
103
|
|
|
return $this->userResource->findOne((string)$io->choice($question, $choices)); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Method to get UserGroup entity. Within this user will be asked which |
108
|
|
|
* UserGroup entity he/she wants to process with. |
109
|
|
|
* |
110
|
|
|
* @throws Throwable |
111
|
|
|
*/ |
112
|
|
|
private function getUserGroupEntity(SymfonyStyle $io, string $question): ?UserGroup |
113
|
|
|
{ |
114
|
|
|
$choices = []; |
115
|
|
|
$iterator = $this->getUserGroupIterator($choices); |
116
|
|
|
|
117
|
|
|
array_map( |
118
|
|
|
$iterator, |
119
|
|
|
$this->userGroupResource->find(orderBy: [ |
120
|
|
|
'name' => 'asc', |
121
|
|
|
]) |
122
|
|
|
); |
123
|
|
|
|
124
|
|
|
$choices['Exit'] = 'Exit command'; |
125
|
|
|
|
126
|
|
|
return $this->userGroupResource->findOne((string)$io->choice($question, $choices)); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Getter method for user formatter closure. This closure will format |
131
|
|
|
* single User entity for choice list. |
132
|
|
|
* |
133
|
|
|
* @param array<string, string> $choices |
134
|
|
|
*/ |
135
|
|
|
private function getUserIterator(array &$choices): Closure |
136
|
|
|
{ |
137
|
|
|
return static function (User $user) use (&$choices): void { |
138
|
|
|
$message = sprintf( |
139
|
|
|
'%s (%s %s <%s>)', |
140
|
|
|
$user->getUsername(), |
141
|
|
|
$user->getFirstName(), |
142
|
|
|
$user->getLastName(), |
143
|
|
|
$user->getEmail(), |
144
|
|
|
); |
145
|
|
|
|
146
|
|
|
$choices[$user->getId()] = $message; |
147
|
|
|
}; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Getter method for user group formatter closure. This closure will format |
152
|
|
|
* single UserGroup entity for choice list. |
153
|
|
|
* |
154
|
|
|
* @param array<string, string> $choices |
155
|
|
|
*/ |
156
|
|
|
private function getUserGroupIterator(array &$choices): Closure |
157
|
|
|
{ |
158
|
|
|
return static function (UserGroup $userGroup) use (&$choices): void { |
159
|
|
|
$choices[$userGroup->getId()] = sprintf('%s (%s)', $userGroup->getName(), $userGroup->getRole()->getId()); |
160
|
|
|
}; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Helper method to confirm user that he/she has chosen correct User |
165
|
|
|
* entity to process with. |
166
|
|
|
*/ |
167
|
|
|
private function isCorrectUser(SymfonyStyle $io, User $userEntity): bool |
168
|
|
|
{ |
169
|
|
|
$message = sprintf( |
170
|
|
|
'Is this the correct user [%s - %s (%s %s <%s>)]?', |
171
|
|
|
$userEntity->getId(), |
172
|
|
|
$userEntity->getUsername(), |
173
|
|
|
$userEntity->getFirstName(), |
174
|
|
|
$userEntity->getLastName(), |
175
|
|
|
$userEntity->getEmail(), |
176
|
|
|
); |
177
|
|
|
|
178
|
|
|
return $io->confirm($message, false); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Helper method to confirm user that he/she has chosen correct UserGroup |
183
|
|
|
* entity to process with. |
184
|
|
|
*/ |
185
|
|
|
private function isCorrectUserGroup(SymfonyStyle $io, UserGroup $userGroupEntity): bool |
186
|
|
|
{ |
187
|
|
|
$message = sprintf( |
188
|
|
|
'Is this the correct user group [%s - %s (%s)]?', |
189
|
|
|
$userGroupEntity->getId(), |
190
|
|
|
$userGroupEntity->getName(), |
191
|
|
|
$userGroupEntity->getRole()->getId(), |
192
|
|
|
); |
193
|
|
|
|
194
|
|
|
return $io->confirm($message, false); |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|