1+ <?php
2+ /**
3+ * Created by PhpStorm.
4+ * User: Alexandre
5+ * Date: 01/05/2018
6+ * Time: 15:21
7+ */
8+
9+ namespace App \Command ;
10+
11+
12+ use App \Security \User ;
13+ use Doctrine \ORM \EntityManagerInterface ;
14+ use Symfony \Component \Console \Command \Command ;
15+ use Symfony \Component \Console \Input \InputArgument ;
16+ use Symfony \Component \Console \Input \InputInterface ;
17+ use Symfony \Component \Console \Output \OutputInterface ;
18+ use Symfony \Component \Security \Core \Encoder \UserPasswordEncoderInterface ;
19+ use Symfony \Component \Validator \Validator \ValidatorInterface ;
20+
21+ class CreateUserCommand extends Command
22+ {
23+ private $ em ;
24+ /**
25+ * @var UserPasswordEncoderInterface
26+ */
27+ private $ passwordEncoder ;
28+ /**
29+ * @var ValidatorInterface
30+ */
31+ private $ validator ;
32+
33+ public function __construct (EntityManagerInterface $ em ,
34+ UserPasswordEncoderInterface $ passwordEncoder ,
35+ ValidatorInterface $ validator )
36+ {
37+ parent ::__construct ();
38+ $ this ->em = $ em ;
39+ $ this ->passwordEncoder = $ passwordEncoder ;
40+ $ this ->validator = $ validator ;
41+ }
42+
43+ protected function configure ()
44+ {
45+ $ this
46+ // the name of the command (the part after "bin/console")
47+ ->setName ('app:create-user ' )
48+
49+ // the short description shown while running "php bin/console list"
50+ ->setDescription ('Creates a new user. ' )
51+
52+ // the full command description shown when running the command with
53+ // the "--help" option
54+ ->setHelp ('This command allows you to create a user... ' )
55+ ->addArgument ('username ' , InputArgument::REQUIRED , 'The username of the user. ' )
56+ ->addArgument ('password ' , InputArgument::REQUIRED , 'The password of the user. ' )
57+ ;
58+ }
59+
60+ protected function execute (InputInterface $ input , OutputInterface $ output )
61+ {
62+ // outputs multiple lines to the console (adding "\n" at the end of each line)
63+ $ output ->writeln ([
64+ 'User Creator ' ,
65+ '============ ' ,
66+ '' ,
67+ ]);
68+
69+ // outputs a message followed by a "\n"
70+ $ output ->writeln ('Whoa! ' );
71+
72+ // outputs a message without adding a "\n" at the end of the line
73+ $ output ->write ('You are about to ' );
74+ $ output ->write ('create a user. ' );
75+
76+ do {
77+ $ output ->writeln ('Username: ' .$ input ->getArgument ('username ' ));
78+ $ output ->writeln ('Password: ' .$ input ->getArgument ('password ' ));
79+
80+ $ user = new User ();
81+ $ user ->setUsername ($ input ->getArgument ('username ' ));
82+ $ user ->setPlainPassword ($ input ->getArgument ('password ' ));
83+
84+ $ password = $ this ->passwordEncoder ->encodePassword ($ user , $ user ->getPlainPassword ());
85+ $ user ->setPassword ($ password );
86+
87+ $ errors = $ this ->validator ->validate ($ user );
88+ if (count ($ errors ) > 0 ) {
89+ /*
90+ * Uses a __toString method on the $errors variable which is a
91+ * ConstraintViolationList object. This gives us a nice string
92+ * for debugging.
93+ */
94+ $ errorsString = (string )$ errors ;
95+ $ output ->writeln ('Errors: ' .$ errorsString );
96+ }
97+
98+ } while (count ($ errors ));
99+
100+ $ this ->em ->persist ($ user );
101+ $ this ->em ->flush ();
102+ }
103+ }
0 commit comments