Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 90
0.00% covered (danger)
0.00%
0 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
UserCreate
0.00% covered (danger)
0.00%
0 / 90
0.00% covered (danger)
0.00%
0 / 6
380
0.00% covered (danger)
0.00%
0 / 1
 handle
0.00% covered (danger)
0.00%
0 / 22
0.00% covered (danger)
0.00%
0 / 1
2
 addFirstName
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
12
 addLastName
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
12
 addEmail
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
12
 addPassword
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
12
 addRole
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 1
42
1<?php
2
3namespace App\Console\Commands;
4
5use App\Enums\Users\RoleEnum;
6use App\Models\User;
7use Carbon\Carbon;
8use Illuminate\Console\Command;
9use Illuminate\Support\Facades\Validator;
10use Illuminate\Validation\ValidationException;
11
12class UserCreate extends Command
13{
14    /**
15     * The name and signature of the console command.
16     *
17     * @var string
18     */
19    protected $signature = 'uwil:user:create';
20
21    /**
22     * The console command description.
23     *
24     * @var string
25     */
26    protected $description = 'Create a backend admin user';
27
28    /**
29     * The first name of the new user.
30     *
31     * @var string
32     */
33    protected $first_name;
34
35    /**
36     * The last name of the new user.
37     *
38     * @var string
39     */
40    protected $last_name;
41
42    /**
43     * The email of the new user.
44     *
45     * @var string
46     */
47    protected $email;
48
49    /**
50     * The password of the new user.
51     *
52     * @var string
53     */
54    protected $password;
55
56    /**
57     * The role of the new user.
58     *
59     * @var string
60     */
61    protected $role;
62
63    /**
64     * Execute the console command.
65     *
66     * @return integer
67     */
68    public function handle(): int
69    {
70        $this->first_name = null;
71        $this->addFirstName();
72
73        $this->last_name = null;
74        $this->addLastName();
75
76        $this->email = null;
77        $this->addEmail();
78
79        $this->password = null;
80        $this->addPassword();
81
82        $this->role = null;
83        $this->addRole();
84
85        $user = new User([
86            'first_name' => $this->first_name,
87            'last_name' => $this->last_name,
88            'email' => $this->email,
89            'password' => $this->password,
90            'role' => $this->role,
91            'published' => true,
92            'published_at' => Carbon::now(),
93        ]);
94        $user->saveOrFail();
95
96        $this->info('User created ðŸ‘Œ');
97
98        return 0;
99    }
100
101    /**
102     * Add a first name to the user.
103     *
104     * @return void
105     */
106    private function addFirstName(): void
107    {
108        while (is_null($this->first_name)) {
109            try {
110                $tmp       = $this->ask('Type the wanted user first name', 'Admin');
111                $validator = Validator::make(['first_name' => $tmp], [
112                    'first_name' => 'required|min:3|max:255'
113                ]);
114                $validator->validate();
115                $this->first_name = $tmp;
116            } catch (ValidationException $e) {
117                $this->error(sprintf(
118                    'Please provide a valid first name %s',
119                    \implode(',', collect($e->errors())->flatten()->all())
120                ));
121                continue;
122            } //end try
123        }; //end while
124    }
125
126    /**
127     * Add a last name to the user.
128     *
129     * @return void
130     */
131    private function addLastName(): void
132    {
133        while (is_null($this->last_name)) {
134            try {
135                $tmp       = $this->ask('Type the wanted user last name', 'Saïtama');
136                $validator = Validator::make(['last_name' => $tmp], [
137                    'last_name' => 'required|min:3|max:255'
138                ]);
139                $validator->validate();
140                $this->last_name = $tmp;
141            } catch (ValidationException $e) {
142                $this->error(sprintf(
143                    'Please provide a valid last_name %s',
144                    \implode(',', collect($e->errors())->flatten()->all())
145                ));
146                continue;
147            } //end try
148        }; //end while
149    }
150
151    /**
152     * Add an email to the user.
153     *
154     * @return void
155     */
156    private function addEmail(): void
157    {
158        while (is_null($this->email)) {
159            try {
160                $tmp       = $this->ask('Type the wanted user email', 'equipe.web@ldc.fr');
161                $validator = Validator::make(['email' => $tmp], [
162                    'email' => 'required|unique:users,email|email:rfc,dns'
163                ]);
164                $validator->validate();
165                $this->email = $tmp;
166            } catch (ValidationException $e) {
167                $this->error(sprintf(
168                    'Please provide a valid email %s',
169                    \implode(',', collect($e->errors())->flatten()->all())
170                ));
171                continue;
172            } //end try
173        }; //end while
174    }
175
176    /**
177     * Add a password to the user.
178     *
179     * @return void
180     */
181    private function addPassword(): void
182    {
183        while (
184            ($this->password = $this->secret(
185                (!\is_null($this->password) ? 'Failed to confirm, ' : '') . 'Type the wanted user password'
186            )) !== $this->secret('Confirm the password')
187        ) {
188            continue;
189        }; //end while
190    }
191
192    /**
193     * Add a role to the user.
194     *
195     * @return void
196     */
197    private function addRole(): void
198    {
199        while (is_null($this->role)) {
200            try {
201                $tmp                         = $this->choice(
202                    'Select his role',
203                    [RoleEnum::conceptor->name, RoleEnum::admin->name, RoleEnum::visitor->name],
204                    RoleEnum::visitor->name,
205                    $maxAttempts             = null,
206                    $allowMultipleSelections = false
207                );
208                if ($tmp === RoleEnum::conceptor->name) {
209                    $tmp = RoleEnum::conceptor;
210                }
211                if ($tmp === RoleEnum::admin->name) {
212                    $tmp = RoleEnum::admin;
213                }
214                if ($tmp === RoleEnum::visitor->name) {
215                    $tmp = RoleEnum::visitor;
216                }
217                $validator = Validator::make(['role' => $tmp], [
218                    'role' => 'required'
219                ]);
220                $validator->validate();
221                $this->role = $tmp;
222            } catch (ValidationException $e) {
223                $this->error(sprintf(
224                    'Please choose from the selection',
225                    \implode(',', collect($e->errors())->flatten()->all())
226                ));
227                continue;
228            } //end try
229        }; //end while
230    }
231}