These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | /* |
||
4 | * NOTICE OF LICENSE |
||
5 | * |
||
6 | * Part of the Rinvex Fort Package. |
||
7 | * |
||
8 | * This source file is subject to The MIT License (MIT) |
||
9 | * that is bundled with this package in the LICENSE file. |
||
10 | * |
||
11 | * Package: Rinvex Fort Package |
||
12 | * License: The MIT License (MIT) |
||
13 | * Link: https://rinvex.com |
||
14 | */ |
||
15 | |||
16 | namespace Rinvex\Fort\Console\Commands; |
||
17 | |||
18 | use Illuminate\Console\Command; |
||
19 | use Illuminate\Support\Facades\Lang; |
||
20 | use Illuminate\Contracts\Validation\Factory; |
||
21 | |||
22 | class UserCreateCommand extends Command |
||
23 | { |
||
24 | /** |
||
25 | * The name and signature of the console command. |
||
26 | * |
||
27 | * @var string |
||
28 | */ |
||
29 | protected $signature = 'fort:user:create |
||
30 | {email? : The email address of the user} |
||
31 | {username? : The username of the user} |
||
32 | {password? : The password of the user} |
||
33 | {firstName? : The first name of the user} |
||
34 | {middleName? : The middle name of the user} |
||
35 | {lastName? : The last name of the user} |
||
36 | {--I|inactive : Set the user as inactive} |
||
37 | {--U|unverified : Set the user as unverified}'; |
||
38 | |||
39 | /** |
||
40 | * The console command description. |
||
41 | * |
||
42 | * @var string |
||
43 | */ |
||
44 | protected $description = 'Create a new user.'; |
||
45 | |||
46 | /** |
||
47 | * Execute the console command. |
||
48 | * |
||
49 | * @return void |
||
50 | */ |
||
51 | public function handle() |
||
52 | { |
||
53 | $data = array_filter([ |
||
54 | |||
55 | // Required user attributes |
||
56 | 'email' => $this->argument('email') ?: $this->ask(Lang::get('rinvex.fort::artisan.user.email')), |
||
57 | 'username' => $this->argument('username') ?: $this->ask(Lang::get('rinvex.fort::artisan.user.username')), |
||
58 | 'password' => bcrypt($this->argument('password') ?: $this->secret(Lang::get('rinvex.fort::artisan.user.password'))), |
||
0 ignored issues
–
show
|
|||
59 | |||
60 | // Optional user attributes |
||
61 | 'first_name' => $this->argument('firstName'), |
||
62 | 'middle_name' => $this->argument('middleName'), |
||
63 | 'last_name' => $this->argument('lastName'), |
||
64 | 'active' => ! $this->option('inactive'), |
||
65 | 'email_verified' => ! $this->option('unverified'), |
||
66 | |||
67 | ], [ |
||
68 | $this, |
||
69 | 'filter' |
||
70 | ]); |
||
71 | |||
72 | $rules = [ |
||
73 | 'email' => 'required|email|max:255|unique:'.config('rinvex.fort.tables.users').',email', |
||
74 | 'username' => 'required|max:255|unique:'.config('rinvex.fort.tables.users').',username', |
||
75 | ]; |
||
76 | |||
77 | $validator = app(Factory::class)->make($data, $rules); |
||
78 | |||
79 | if ($validator->fails()) { |
||
80 | $this->error('Errors:'); |
||
81 | |||
82 | foreach ($validator->errors()->getMessages() as $key => $messages) { |
||
83 | $this->error('- '.$key.': '.$messages[0]); |
||
84 | } |
||
85 | } else { |
||
86 | $user = $this->laravel['rinvex.fort.user']->create($data); |
||
87 | |||
88 | $this->info(Lang::get('rinvex.fort::artisan.user.created').' ['.Lang::get('rinvex.fort::artisan.user.id').': '.$user->id.', '.Lang::get('rinvex.fort::artisan.user.email').': '.$user->email.', '.Lang::get('rinvex.fort::artisan.user.username').': '.$user->username.']'); |
||
89 | } |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * Filter null and empty values. |
||
94 | * |
||
95 | * @param $value |
||
96 | * |
||
97 | * @return bool |
||
98 | */ |
||
99 | protected function filter($value) |
||
100 | { |
||
101 | return ($value !== null && $value !== ''); |
||
102 | } |
||
103 | } |
||
104 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.