1 | <?php |
||
2 | |||
3 | namespace Luilliarcec\UserCommands\Commands; |
||
4 | |||
5 | use Illuminate\Contracts\Auth\MustVerifyEmail; |
||
6 | use Illuminate\Support\Facades\DB; |
||
7 | use Illuminate\Support\Facades\Hash; |
||
8 | use Illuminate\Support\Facades\Route; |
||
9 | use Illuminate\Support\Facades\Validator; |
||
10 | use Illuminate\Validation\Rule; |
||
11 | |||
12 | class CreateNewUserCommand extends UserCommand |
||
13 | { |
||
14 | /** |
||
15 | * The name and signature of the console command. |
||
16 | * |
||
17 | * @var string |
||
18 | */ |
||
19 | protected $signature = 'user:create |
||
20 | {--verified : Mark the user`s email as verified} |
||
21 | {--a|attributes=* : Add additional attributes to the user} |
||
22 | {--p|permissions=* : Assign permissions to the user.} |
||
23 | {--r|roles=* : Assign roles to the user.}'; |
||
24 | |||
25 | /** |
||
26 | * The console command description. |
||
27 | * |
||
28 | * @var string |
||
29 | */ |
||
30 | protected $description = 'Create a new user in your app'; |
||
31 | |||
32 | /** |
||
33 | * Execute the console command. |
||
34 | */ |
||
35 | public function handle() |
||
36 | { |
||
37 | $this->user = $this->newUserInstance(); |
||
38 | |||
39 | $name = $this->ask('name:'); |
||
40 | $email = $this->ask('email:'); |
||
41 | $password = $this->secret('password:'); |
||
42 | $password_confirmation = $this->secret('password confirmation:'); |
||
43 | |||
44 | $passes = $this->validate([ |
||
45 | 'name' => $name, |
||
46 | 'email' => $email, |
||
47 | 'password' => $password, |
||
48 | 'password_confirmation' => $password_confirmation |
||
49 | ]); |
||
50 | |||
51 | if (!$passes) return 1; |
||
52 | |||
53 | DB::transaction(function () use ($name, $email, $password) { |
||
54 | $this->save( |
||
55 | array_merge($this->attributes(), [ |
||
56 | 'name' => $name, |
||
57 | 'email' => $email, |
||
58 | 'password' => Hash::make($password), |
||
59 | ]) |
||
60 | ); |
||
61 | |||
62 | $this->verified(); |
||
63 | $this->permissions(); |
||
64 | $this->roles(); |
||
65 | |||
66 | $this->info('The user was created successfully!'); |
||
67 | }); |
||
68 | |||
69 | return 0; |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * Validate data and show errors |
||
74 | * |
||
75 | * @param array $data |
||
76 | * @return bool |
||
77 | */ |
||
78 | protected function validate(array $data): bool |
||
79 | { |
||
80 | $validator = Validator::make($data, $this->rules()); |
||
81 | |||
82 | if ($validator->fails()) { |
||
83 | $this->info('User not created. See error messages below:'); |
||
84 | |||
85 | foreach ($validator->errors()->all() as $error) { |
||
86 | $this->error($error); |
||
87 | } |
||
88 | |||
89 | return false; |
||
90 | } |
||
91 | |||
92 | return true; |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * Validation rules |
||
97 | * |
||
98 | * @return array |
||
99 | */ |
||
100 | protected function rules(): array |
||
101 | { |
||
102 | return [ |
||
103 | 'name' => ['required', 'string', 'max:255'], |
||
104 | 'email' => ['required', 'string', 'email', 'max:255', Rule::unique($this->user->getTable())], |
||
0 ignored issues
–
show
|
|||
105 | 'password' => ['required', 'string', 'confirmed'], |
||
106 | ]; |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * The record in the database persists |
||
111 | * |
||
112 | * @param array $data |
||
113 | */ |
||
114 | protected function save(array $data) |
||
115 | { |
||
116 | $this->user->fill($data)->save(); |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * Mark the email as verified or send a verification email |
||
121 | */ |
||
122 | protected function verified() |
||
123 | { |
||
124 | if ($this->option('verified')) { |
||
125 | return $this->user->markEmailAsVerified(); |
||
126 | } |
||
127 | |||
128 | if (Route::has('verification.verify') && $this->user instanceof MustVerifyEmail) { |
||
129 | $this->user->sendEmailVerificationNotification(); |
||
130 | } |
||
131 | |||
132 | return null; |
||
133 | } |
||
134 | |||
135 | /** |
||
136 | * Assign permissions to the user. |
||
137 | */ |
||
138 | protected function permissions() |
||
139 | { |
||
140 | if (is_null($this->permission)) return; |
||
141 | |||
142 | $relation = config('user-commands.permission.relation'); |
||
143 | |||
144 | $options = $this->option('permissions'); |
||
145 | |||
146 | $permissions = $this->permission::query() |
||
147 | ->whereIn('name', $options) |
||
148 | ->get('id') |
||
149 | ->modelKeys(); |
||
150 | |||
151 | $this->user->$relation()->attach($permissions); |
||
152 | } |
||
153 | |||
154 | /** |
||
155 | * Assign roles to the user. |
||
156 | */ |
||
157 | protected function roles() |
||
158 | { |
||
159 | if (is_null($this->role)) return; |
||
160 | |||
161 | $relation = config('user-commands.role.relation'); |
||
162 | |||
163 | $options = $this->option('roles'); |
||
164 | |||
165 | $permissions = $this->role::query() |
||
166 | ->whereIn('name', $options) |
||
167 | ->get('id') |
||
168 | ->modelKeys(); |
||
169 | |||
170 | $this->user->$relation()->attach($permissions); |
||
171 | } |
||
172 | |||
173 | /** |
||
174 | * Gets the additional attributes |
||
175 | * |
||
176 | * @return array |
||
177 | */ |
||
178 | protected function attributes(): array |
||
179 | { |
||
180 | $attributes = []; |
||
181 | |||
182 | $option = $this->option('attributes'); |
||
183 | |||
184 | foreach ($option as $attribute) { |
||
185 | [$key, $value] = explode(':', $attribute); |
||
186 | |||
187 | $attributes[$key] = $value; |
||
188 | } |
||
189 | |||
190 | return $attributes; |
||
191 | } |
||
192 | } |
||
193 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.