1212namespace  AppBundle \Command ;
1313
1414use  AppBundle \Entity \User ;
15+ use  AppBundle \Utils \Validator ;
1516use  Doctrine \ORM \EntityManagerInterface ;
1617use  Symfony \Component \Console \Command \Command ;
1718use  Symfony \Component \Console \Input \InputArgument ;
2324use  Symfony \Component \Stopwatch \Stopwatch ;
2425
2526/** 
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. 
2728 * 
2829 * To use this command, open a terminal window, enter into your project 
2930 * directory and execute the following: 
@@ -48,13 +49,15 @@ class AddUserCommand extends Command
4849    private  $ io
4950    private  $ entityManager
5051    private  $ passwordEncoder
52+     private  $ validator
5153
52-     public  function  __construct (EntityManagerInterface $ emUserPasswordEncoderInterface $ encoder
54+     public  function  __construct (EntityManagerInterface $ emUserPasswordEncoderInterface $ encoder,  Validator   $ validator 
5355    {
5456        parent ::__construct ();
5557
5658        $ this entityManager  = $ em
5759        $ this passwordEncoder  = $ encoder
60+         $ this validator  = $ validator
5861    }
5962
6063    /** 
@@ -120,14 +123,7 @@ protected function interact(InputInterface $input, OutputInterface $output)
120123        if  (null  !== $ username
121124            $ this io ->text (' > <info>Username</info>:  ' .$ username
122125        } 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 ' ]);
131127            $ inputsetArgument ('username ' , $ username
132128        }
133129
@@ -136,7 +132,7 @@ protected function interact(InputInterface $input, OutputInterface $output)
136132        if  (null  !== $ password
137133            $ this io ->text (' > <info>Password</info>:  ' .str_repeat ('* ' , mb_strlen ($ password
138134        } 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 ' ]);
140136            $ inputsetArgument ('password ' , $ password
141137        }
142138
@@ -145,7 +141,7 @@ protected function interact(InputInterface $input, OutputInterface $output)
145141        if  (null  !== $ email
146142            $ this io ->text (' > <info>Email</info>:  ' .$ email
147143        } else  {
148-             $ email$ this io ->ask ('Email ' , null , [$ this 'emailValidator ' ]);
144+             $ email$ this io ->ask ('Email ' , null , [$ this -> validator , 'validateEmail ' ]);
149145            $ inputsetArgument ('email ' , $ email
150146        }
151147
@@ -154,7 +150,7 @@ protected function interact(InputInterface $input, OutputInterface $output)
154150        if  (null  !== $ fullName
155151            $ this io ->text (' > <info>Full Name</info>:  ' .$ fullName
156152        } else  {
157-             $ fullName$ this io ->ask ('Full Name ' , null , [$ this 'fullNameValidator ' ]);
153+             $ fullName$ this io ->ask ('Full Name ' , null , [$ this -> validator , 'validateFullName ' ]);
158154            $ inputsetArgument ('full-name ' , $ fullName
159155        }
160156    }
@@ -199,50 +195,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
199195        }
200196    }
201197
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 ($ plainPassword6 ) {
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- 
246198    private  function  validateUserData ($ username$ plainPassword$ email$ fullName
247199    {
248200        $ userRepository$ this entityManager ->getRepository (User::class);
@@ -255,9 +207,9 @@ private function validateUserData($username, $plainPassword, $email, $fullName)
255207        }
256208
257209        // 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
261213
262214        // check if a user with the same email already exists. 
263215        $ existingEmail$ userRepositoryfindOneBy (['email '  => $ email
0 commit comments