12
12
namespace AppBundle \Command ;
13
13
14
14
use AppBundle \Entity \User ;
15
+ use AppBundle \Utils \Validator ;
15
16
use Doctrine \ORM \EntityManagerInterface ;
16
17
use Symfony \Component \Console \Command \Command ;
17
18
use Symfony \Component \Console \Input \InputArgument ;
23
24
use Symfony \Component \Stopwatch \Stopwatch ;
24
25
25
26
/**
26
- * A command console that creates users and stores them in the database.
27
+ * A console command that creates users and stores them in the database.
27
28
*
28
29
* To use this command, open a terminal window, enter into your project
29
30
* directory and execute the following:
@@ -48,13 +49,15 @@ class AddUserCommand extends Command
48
49
private $ io ;
49
50
private $ entityManager ;
50
51
private $ passwordEncoder ;
52
+ private $ validator ;
51
53
52
- public function __construct (EntityManagerInterface $ em , UserPasswordEncoderInterface $ encoder )
54
+ public function __construct (EntityManagerInterface $ em , UserPasswordEncoderInterface $ encoder, Validator $ validator )
53
55
{
54
56
parent ::__construct ();
55
57
56
58
$ this ->entityManager = $ em ;
57
59
$ this ->passwordEncoder = $ encoder ;
60
+ $ this ->validator = $ validator ;
58
61
}
59
62
60
63
/**
@@ -120,14 +123,7 @@ protected function interact(InputInterface $input, OutputInterface $output)
120
123
if (null !== $ username ) {
121
124
$ this ->io ->text (' > <info>Username</info>: ' .$ username );
122
125
} else {
123
- $ username = $ this ->io ->ask ('Username ' , null , function ($ answer ) {
124
- if (empty ($ answer )) {
125
- throw new \RuntimeException ('The username cannot be empty ' );
126
- }
127
-
128
- return $ answer ;
129
- });
130
-
126
+ $ username = $ this ->io ->ask ('Username ' , null , [$ this ->validator , 'validateUsername ' ]);
131
127
$ input ->setArgument ('username ' , $ username );
132
128
}
133
129
@@ -136,7 +132,7 @@ protected function interact(InputInterface $input, OutputInterface $output)
136
132
if (null !== $ password ) {
137
133
$ this ->io ->text (' > <info>Password</info>: ' .str_repeat ('* ' , mb_strlen ($ password )));
138
134
} else {
139
- $ password = $ this ->io ->askHidden ('Password (your type will be hidden) ' , null , [$ this , 'passwordValidator ' ]);
135
+ $ password = $ this ->io ->askHidden ('Password (your type will be hidden) ' , null , [$ this -> validator , 'validatePassword ' ]);
140
136
$ input ->setArgument ('password ' , $ password );
141
137
}
142
138
@@ -145,7 +141,7 @@ protected function interact(InputInterface $input, OutputInterface $output)
145
141
if (null !== $ email ) {
146
142
$ this ->io ->text (' > <info>Email</info>: ' .$ email );
147
143
} else {
148
- $ email = $ this ->io ->ask ('Email ' , null , [$ this , 'emailValidator ' ]);
144
+ $ email = $ this ->io ->ask ('Email ' , null , [$ this -> validator , 'validateEmail ' ]);
149
145
$ input ->setArgument ('email ' , $ email );
150
146
}
151
147
@@ -154,7 +150,7 @@ protected function interact(InputInterface $input, OutputInterface $output)
154
150
if (null !== $ fullName ) {
155
151
$ this ->io ->text (' > <info>Full Name</info>: ' .$ fullName );
156
152
} else {
157
- $ fullName = $ this ->io ->ask ('Full Name ' , null , [$ this , 'fullNameValidator ' ]);
153
+ $ fullName = $ this ->io ->ask ('Full Name ' , null , [$ this -> validator , 'validateFullName ' ]);
158
154
$ input ->setArgument ('full-name ' , $ fullName );
159
155
}
160
156
}
@@ -199,50 +195,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
199
195
}
200
196
}
201
197
202
- /**
203
- * @internal
204
- */
205
- public function passwordValidator ($ plainPassword )
206
- {
207
- if (empty ($ plainPassword )) {
208
- throw new \Exception ('The password can not be empty. ' );
209
- }
210
-
211
- if (mb_strlen (trim ($ plainPassword )) < 6 ) {
212
- throw new \Exception ('The password must be at least 6 characters long. ' );
213
- }
214
-
215
- return $ plainPassword ;
216
- }
217
-
218
- /**
219
- * @internal
220
- */
221
- public function emailValidator ($ email )
222
- {
223
- if (empty ($ email )) {
224
- throw new \Exception ('The email can not be empty. ' );
225
- }
226
-
227
- if (false === mb_strpos ($ email , '@ ' )) {
228
- throw new \Exception ('The email should look like a real email. ' );
229
- }
230
-
231
- return $ email ;
232
- }
233
-
234
- /**
235
- * @internal
236
- */
237
- public function fullNameValidator ($ fullName )
238
- {
239
- if (empty ($ fullName )) {
240
- throw new \Exception ('The full name can not be empty. ' );
241
- }
242
-
243
- return $ fullName ;
244
- }
245
-
246
198
private function validateUserData ($ username , $ plainPassword , $ email , $ fullName )
247
199
{
248
200
$ userRepository = $ this ->entityManager ->getRepository (User::class);
@@ -255,9 +207,9 @@ private function validateUserData($username, $plainPassword, $email, $fullName)
255
207
}
256
208
257
209
// validate password and email if is not this input means interactive.
258
- $ this ->passwordValidator ($ plainPassword );
259
- $ this ->emailValidator ($ email );
260
- $ this ->fullNameValidator ($ fullName );
210
+ $ this ->validator -> validatePassword ($ plainPassword );
211
+ $ this ->validator -> validateEmail ($ email );
212
+ $ this ->validator -> validateFullName ($ fullName );
261
213
262
214
// check if a user with the same email already exists.
263
215
$ existingEmail = $ userRepository ->findOneBy (['email ' => $ email ]);
0 commit comments