Total Complexity | 81 |
Total Lines | 559 |
Duplicated Lines | 0 % |
Changes | 8 | ||
Bugs | 4 | Features | 0 |
Complex classes like User often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use User, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
151 | class User extends Authenticatable |
||
152 | { |
||
153 | use HasRoles, InviteTrait, Notifiable, UserVerification; |
||
154 | |||
155 | public const ERR_SIGNUP_BADUNAME = -1; |
||
156 | |||
157 | public const ERR_SIGNUP_BADPASS = -2; |
||
158 | |||
159 | public const ERR_SIGNUP_BADEMAIL = -3; |
||
160 | |||
161 | public const ERR_SIGNUP_UNAMEINUSE = -4; |
||
162 | |||
163 | public const ERR_SIGNUP_EMAILINUSE = -5; |
||
164 | |||
165 | public const ERR_SIGNUP_BADINVITECODE = -6; |
||
166 | |||
167 | public const SUCCESS = 1; |
||
168 | |||
169 | public const ROLE_USER = 1; |
||
170 | |||
171 | public const ROLE_ADMIN = 2; |
||
172 | |||
173 | /** |
||
174 | * Users SELECT queue type. |
||
175 | */ |
||
176 | public const QUEUE_NONE = 0; |
||
177 | |||
178 | public const QUEUE_SABNZBD = 1; |
||
179 | |||
180 | public const QUEUE_NZBGET = 2; |
||
181 | |||
182 | /** |
||
183 | * @var string |
||
184 | */ |
||
185 | |||
186 | /** |
||
187 | * @var bool |
||
188 | */ |
||
189 | protected $dateFormat = false; |
||
190 | |||
191 | /** |
||
192 | * @var array |
||
193 | */ |
||
194 | protected $hidden = ['remember_token', 'password']; |
||
195 | |||
196 | /** |
||
197 | * @var array |
||
198 | */ |
||
199 | protected $guarded = []; |
||
200 | |||
201 | protected function getDefaultGuardName(): string |
||
202 | { |
||
203 | return 'web'; |
||
204 | } |
||
205 | |||
206 | public function role(): BelongsTo |
||
207 | { |
||
208 | return $this->belongsTo(Role::class, 'roles_id'); |
||
209 | } |
||
210 | |||
211 | public function request(): HasMany |
||
212 | { |
||
213 | return $this->hasMany(UserRequest::class, 'users_id'); |
||
214 | } |
||
215 | |||
216 | public function download(): HasMany |
||
217 | { |
||
218 | return $this->hasMany(UserDownload::class, 'users_id'); |
||
219 | } |
||
220 | |||
221 | public function release(): HasMany |
||
222 | { |
||
223 | return $this->hasMany(UsersRelease::class, 'users_id'); |
||
224 | } |
||
225 | |||
226 | public function series(): HasMany |
||
227 | { |
||
228 | return $this->hasMany(UserSerie::class, 'users_id'); |
||
229 | } |
||
230 | |||
231 | public function invitation(): HasMany |
||
232 | { |
||
233 | return $this->hasMany(Invitation::class, 'users_id'); |
||
234 | } |
||
235 | |||
236 | public function failedRelease(): HasMany |
||
237 | { |
||
238 | return $this->hasMany(DnzbFailure::class, 'users_id'); |
||
239 | } |
||
240 | |||
241 | public function comment(): HasMany |
||
242 | { |
||
243 | return $this->hasMany(ReleaseComment::class, 'users_id'); |
||
244 | } |
||
245 | |||
246 | /** |
||
247 | * @throws \Exception |
||
248 | */ |
||
249 | public static function deleteUser($id): void |
||
250 | { |
||
251 | self::find($id)->delete(); |
||
252 | } |
||
253 | |||
254 | public static function getCount(?string $role = null, ?string $username = '', ?string $host = '', ?string $email = ''): int |
||
255 | { |
||
256 | $res = self::query()->where('email', '<>', '[email protected]'); |
||
257 | |||
258 | if (! empty($role)) { |
||
259 | $res->where('roles_id', $role); |
||
260 | } |
||
261 | |||
262 | if ($username !== '') { |
||
263 | $res->where('username', 'like', '%'.$username.'%'); |
||
264 | } |
||
265 | |||
266 | if ($host !== '') { |
||
267 | $res->where('host', 'like', '%'.$host.'%'); |
||
268 | } |
||
269 | |||
270 | if ($email !== '') { |
||
271 | $res->where('email', 'like', '%'.$email.'%'); |
||
272 | } |
||
273 | |||
274 | return $res->count(['id']); |
||
275 | } |
||
276 | |||
277 | public static function updateUser(int $id, string $userName, ?string $email, int $grabs, int $role, ?string $notes, int $invites, int $movieview, int $musicview, int $gameview, int $xxxview, int $consoleview, int $bookview, string $style = 'None'): int |
||
278 | { |
||
279 | $userName = trim($userName); |
||
280 | |||
281 | $rateLimit = Role::query()->where('id', $role)->first(); |
||
282 | |||
283 | $sql = [ |
||
284 | 'username' => $userName, |
||
285 | 'grabs' => $grabs, |
||
286 | 'roles_id' => $role, |
||
287 | 'notes' => substr($notes, 0, 255), |
||
288 | 'invites' => $invites, |
||
289 | 'movieview' => $movieview, |
||
290 | 'musicview' => $musicview, |
||
291 | 'gameview' => $gameview, |
||
292 | 'xxxview' => $xxxview, |
||
293 | 'consoleview' => $consoleview, |
||
294 | 'bookview' => $bookview, |
||
295 | 'style' => $style, |
||
296 | 'rate_limit' => $rateLimit ? $rateLimit['rate_limit'] : 60, |
||
297 | ]; |
||
298 | |||
299 | if (! empty($email)) { |
||
300 | $email = trim($email); |
||
301 | $sql += ['email' => $email]; |
||
302 | } |
||
303 | |||
304 | $user = self::find($id); |
||
305 | $user->update($sql); |
||
306 | $user->syncRoles([$rateLimit['name']]); |
||
307 | |||
308 | return self::SUCCESS; |
||
309 | } |
||
310 | |||
311 | /** |
||
312 | * @return User|Builder|Model|object|null |
||
313 | */ |
||
314 | public static function getByUsername(string $userName) |
||
315 | { |
||
316 | return self::whereUsername($userName)->first(); |
||
317 | } |
||
318 | |||
319 | /** |
||
320 | * @return Model|static |
||
321 | * |
||
322 | * @throws ModelNotFoundException |
||
323 | */ |
||
324 | public static function getByEmail(string $email) |
||
325 | { |
||
326 | return self::whereEmail($email)->first(); |
||
327 | } |
||
328 | |||
329 | public static function updateUserRole(int $uid, int|string $role): bool |
||
330 | { |
||
331 | if (is_int($role)) { |
||
332 | $roleQuery = Role::query()->where('id', $role)->first(); |
||
333 | } else { |
||
334 | $roleQuery = Role::query()->where('name', $role)->first(); |
||
335 | } |
||
336 | $roleName = $roleQuery->name; |
||
337 | |||
338 | $user = self::find($uid); |
||
339 | $user->syncRoles([$roleName]); |
||
340 | |||
341 | return self::find($uid)->update(['roles_id' => $roleQuery->id]); |
||
342 | } |
||
343 | |||
344 | public static function updateUserRoleChangeDate(int $uid, $date = '', int $addYear = 0): void |
||
345 | { |
||
346 | $user = self::find($uid); |
||
347 | $currRoleExp = $user->rolechangedate ?? now()->toDateTimeString(); |
||
348 | if (! empty($date)) { |
||
349 | $user->update(['rolechangedate' => $date]); |
||
350 | } |
||
351 | if (empty($date) && ! empty($addYear)) { |
||
352 | $user->update(['rolechangedate' => Carbon::createFromDate($currRoleExp)->addYears($addYear)]); |
||
353 | } |
||
354 | } |
||
355 | |||
356 | public static function updateExpiredRoles(): void |
||
357 | { |
||
358 | $now = CarbonImmutable::now(); |
||
359 | $period = [ |
||
360 | 'day' => $now->addDay(), |
||
361 | 'week' => $now->addWeek(), |
||
362 | 'month' => $now->addMonth(), |
||
363 | ]; |
||
364 | |||
365 | foreach ($period as $value) { |
||
366 | $users = self::query()->whereDate('rolechangedate', '=', $value)->get(); |
||
367 | $days = $now->diffInDays($value); |
||
368 | foreach ($users as $user) { |
||
369 | SendAccountWillExpireEmail::dispatch($user, $days)->onQueue('emails'); |
||
370 | } |
||
371 | } |
||
372 | foreach (self::query()->whereDate('rolechangedate', '<', $now)->get() as $expired) { |
||
373 | $expired->update(['roles_id' => self::ROLE_USER, 'rolechangedate' => null]); |
||
374 | $expired->syncRoles(['User']); |
||
375 | SendAccountExpiredEmail::dispatch($expired)->onQueue('emails'); |
||
376 | } |
||
377 | } |
||
378 | |||
379 | /** |
||
380 | * @throws \Throwable |
||
381 | */ |
||
382 | public static function getRange($start, $offset, $orderBy, ?string $userName = '', ?string $email = '', ?string $host = '', ?string $role = '', bool $apiRequests = false): Collection |
||
383 | { |
||
384 | if ($apiRequests) { |
||
385 | UserRequest::clearApiRequests(false); |
||
386 | $query = " |
||
387 | SELECT users.*, roles.name AS rolename, COUNT(user_requests.id) AS apirequests |
||
388 | FROM users |
||
389 | INNER JOIN roles ON roles.id = users.roles_id |
||
390 | LEFT JOIN user_requests ON user_requests.users_id = users.id |
||
391 | WHERE users.id != 0 %s %s %s %s |
||
392 | AND email != '[email protected]' |
||
393 | GROUP BY users.id |
||
394 | ORDER BY %s %s %s "; |
||
395 | } else { |
||
396 | $query = ' |
||
397 | SELECT users.*, roles.name AS rolename |
||
398 | FROM users |
||
399 | INNER JOIN roles ON roles.id = users.roles_id |
||
400 | WHERE 1=1 %s %s %s %s |
||
401 | ORDER BY %s %s %s'; |
||
402 | } |
||
403 | $order = self::getBrowseOrder($orderBy); |
||
404 | |||
405 | return self::fromQuery( |
||
406 | sprintf( |
||
407 | $query, |
||
408 | ! empty($userName) ? 'AND users.username '.'LIKE '.escapeString('%'.$userName.'%') : '', |
||
409 | ! empty($email) ? 'AND users.email '.'LIKE '.escapeString('%'.$email.'%') : '', |
||
410 | ! empty($host) ? 'AND users.host '.'LIKE '.escapeString('%'.$host.'%') : '', |
||
411 | (! empty($role) ? ('AND users.roles_id = '.$role) : ''), |
||
412 | $order[0], |
||
413 | $order[1], |
||
414 | ($start === false ? '' : ('LIMIT '.$offset.' OFFSET '.$start)) |
||
415 | ) |
||
416 | ); |
||
417 | } |
||
418 | |||
419 | /** |
||
420 | * Get sort types for sorting users on the web page user list. |
||
421 | * |
||
422 | * @return string[] |
||
423 | */ |
||
424 | public static function getBrowseOrder($orderBy): array |
||
425 | { |
||
426 | $order = (empty($orderBy) ? 'username_desc' : $orderBy); |
||
427 | $orderArr = explode('_', $order); |
||
428 | $orderField = match ($orderArr[0]) { |
||
429 | 'email' => 'email', |
||
430 | 'host' => 'host', |
||
431 | 'createdat' => 'created_at', |
||
432 | 'lastlogin' => 'lastlogin', |
||
433 | 'apiaccess' => 'apiaccess', |
||
434 | 'grabs' => 'grabs', |
||
435 | 'role' => 'rolename', |
||
436 | 'rolechangedate' => 'rolechangedate', |
||
437 | 'verification' => 'verified', |
||
438 | default => 'username', |
||
439 | }; |
||
440 | $orderSort = (isset($orderArr[1]) && preg_match('/^asc|desc$/i', $orderArr[1])) ? $orderArr[1] : 'desc'; |
||
441 | |||
442 | return [$orderField, $orderSort]; |
||
443 | } |
||
444 | |||
445 | /** |
||
446 | * Verify a password against a hash. |
||
447 | * |
||
448 | * Automatically update the hash if it needs to be. |
||
449 | * |
||
450 | * @param string $password Password to check against hash. |
||
451 | * @param bool|string $hash Hash to check against password. |
||
452 | * @param int $userID ID of the user. |
||
453 | */ |
||
454 | public static function checkPassword(string $password, bool|string $hash, int $userID = -1): bool |
||
455 | { |
||
456 | if (Hash::check($password, $hash) === false) { |
||
457 | return false; |
||
458 | } |
||
459 | |||
460 | // Update the hash if it needs to be. |
||
461 | if (is_numeric($userID) && $userID > 0 && Hash::needsRehash($hash)) { |
||
462 | $hash = self::hashPassword($password); |
||
463 | |||
464 | if ($hash !== false) { |
||
465 | self::find($userID)->update(['password' => $hash]); |
||
466 | } |
||
467 | } |
||
468 | |||
469 | return true; |
||
470 | } |
||
471 | |||
472 | public static function updateRssKey($uid): int |
||
473 | { |
||
474 | self::find($uid)->update(['api_token' => md5(Password::getRepository()->createNewToken())]); |
||
475 | |||
476 | return self::SUCCESS; |
||
477 | } |
||
478 | |||
479 | public static function updatePassResetGuid($id, $guid): int |
||
480 | { |
||
481 | self::find($id)->update(['resetguid' => $guid]); |
||
482 | |||
483 | return self::SUCCESS; |
||
484 | } |
||
485 | |||
486 | public static function updatePassword(int $id, string $password): int |
||
487 | { |
||
488 | self::find($id)->update(['password' => self::hashPassword($password), 'userseed' => md5(Str::uuid()->toString())]); |
||
489 | |||
490 | return self::SUCCESS; |
||
491 | } |
||
492 | |||
493 | public static function hashPassword($password): string |
||
494 | { |
||
495 | return Hash::make($password); |
||
496 | } |
||
497 | |||
498 | /** |
||
499 | * @return Model|static |
||
500 | * |
||
501 | * @throws ModelNotFoundException |
||
502 | */ |
||
503 | public static function getByPassResetGuid(string $guid) |
||
506 | } |
||
507 | |||
508 | public static function incrementGrabs(int $id, int $num = 1): void |
||
509 | { |
||
510 | self::find($id)->increment('grabs', $num); |
||
511 | } |
||
512 | |||
513 | /** |
||
514 | * @return Model|null|static |
||
515 | */ |
||
516 | public static function getByRssToken(string $rssToken) |
||
517 | { |
||
518 | return self::whereApiToken($rssToken)->first(); |
||
519 | } |
||
520 | |||
521 | public static function isValidUrl($url): bool |
||
522 | { |
||
523 | return (! preg_match('/^(http|https|ftp):\/\/([A-Z0-9][A-Z0-9_-]*(?:\.[A-Z0-9][A-Z0-9_-]*)+):?(\d+)?\/?/i', $url)) ? false : true; |
||
524 | } |
||
525 | |||
526 | /** |
||
527 | * @throws \Exception |
||
528 | */ |
||
529 | public static function generatePassword(int $length = 15): string |
||
530 | { |
||
531 | return Token::random($length, true); |
||
532 | } |
||
533 | |||
534 | /** |
||
535 | * @throws \Exception |
||
536 | */ |
||
537 | public static function signUp($userName, $password, $email, $host, $notes, int $invites = Invitation::DEFAULT_INVITES, string $inviteCode = '', bool $forceInviteMode = false, int $role = self::ROLE_USER, bool $validate = true): bool|int|string |
||
538 | { |
||
539 | $user = [ |
||
540 | 'username' => trim($userName), |
||
541 | 'password' => trim($password), |
||
542 | 'email' => trim($email), |
||
543 | ]; |
||
544 | |||
545 | if ($validate) { |
||
546 | $validator = Validator::make($user, [ |
||
547 | 'username' => ['required', 'string', 'min:5', 'max:255', 'unique:users'], |
||
548 | 'email' => ['required', 'string', 'email', 'max:255', 'unique:users', 'indisposable'], |
||
549 | 'password' => ['required', 'string', 'min:8', 'confirmed', 'regex:/^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&*-]).{8,}$/'], |
||
550 | ]); |
||
551 | |||
552 | if ($validator->fails()) { |
||
553 | return implode('', Arr::collapse($validator->errors()->toArray())); |
||
554 | } |
||
555 | } |
||
556 | |||
557 | // Make sure this is the last check, as if a further validation check failed, the invite would still have been used up. |
||
558 | $invitedBy = 0; |
||
559 | if (! $forceInviteMode && (int) Settings::settingValue('..registerstatus') === Settings::REGISTER_STATUS_INVITE) { |
||
560 | if ($inviteCode === '') { |
||
561 | return self::ERR_SIGNUP_BADINVITECODE; |
||
562 | } |
||
563 | |||
564 | $invitedBy = self::checkAndUseInvite($inviteCode); |
||
565 | if ($invitedBy < 0) { |
||
566 | return self::ERR_SIGNUP_BADINVITECODE; |
||
567 | } |
||
568 | } |
||
569 | |||
570 | return self::add($user['username'], $user['password'], $user['email'], $role, $notes, $host, $invites, $invitedBy); |
||
571 | } |
||
572 | |||
573 | /** |
||
574 | * If a invite is used, decrement the person who invited's invite count. |
||
575 | */ |
||
576 | public static function checkAndUseInvite(string $inviteCode): int |
||
577 | { |
||
578 | $invite = Invitation::getInvite($inviteCode); |
||
579 | if (! $invite) { |
||
580 | return -1; |
||
581 | } |
||
582 | |||
583 | self::query()->where('id', $invite['users_id'])->decrement('invites'); |
||
584 | Invitation::deleteInvite($inviteCode); |
||
585 | |||
586 | return $invite['users_id']; |
||
587 | } |
||
588 | |||
589 | /** |
||
590 | * @return false|int|mixed |
||
591 | */ |
||
592 | public static function add(string $userName, string $password, string $email, int $role, ?string $notes = '', string $host = '', int $invites = Invitation::DEFAULT_INVITES, int $invitedBy = 0) |
||
593 | { |
||
594 | $password = self::hashPassword($password); |
||
595 | if (! $password) { |
||
596 | return false; |
||
597 | } |
||
598 | |||
599 | $storeips = (int) Settings::settingValue('..storeuserips') === 1 ? $host : ''; |
||
600 | |||
601 | $user = self::create( |
||
602 | [ |
||
603 | 'username' => $userName, |
||
604 | 'password' => $password, |
||
605 | 'email' => $email, |
||
606 | 'host' => $storeips, |
||
607 | 'roles_id' => $role, |
||
608 | 'invites' => $invites, |
||
609 | 'invitedby' => (int) $invitedBy === 0 ? null : $invitedBy, |
||
610 | 'notes' => $notes, |
||
611 | ] |
||
612 | ); |
||
613 | |||
614 | return $user->id; |
||
615 | } |
||
616 | |||
617 | /** |
||
618 | * Get the list of categories the user has excluded. |
||
619 | * |
||
620 | * @param int $userID ID of the user. |
||
621 | * |
||
622 | * @throws \Exception |
||
623 | */ |
||
624 | public static function getCategoryExclusionById(int $userID): array |
||
625 | { |
||
626 | $ret = []; |
||
627 | |||
628 | $user = self::find($userID); |
||
629 | |||
630 | $userAllowed = $user->getDirectPermissions()->pluck('name')->toArray(); |
||
631 | $roleAllowed = $user->getAllPermissions()->pluck('name')->toArray(); |
||
632 | |||
633 | $allowed = array_intersect($roleAllowed, $userAllowed); |
||
634 | |||
635 | $cats = ['view console', 'view movies', 'view audio', 'view tv', 'view pc', 'view adult', 'view books', 'view other']; |
||
636 | |||
637 | if (! empty($allowed)) { |
||
638 | foreach ($cats as $cat) { |
||
639 | if (! \in_array($cat, $allowed, false)) { |
||
640 | $ret[] = match ($cat) { |
||
641 | 'view console' => 1000, |
||
642 | 'view movies' => 2000, |
||
643 | 'view audio' => 3000, |
||
644 | 'view pc' => 4000, |
||
645 | 'view tv' => 5000, |
||
646 | 'view adult' => 6000, |
||
647 | 'view books' => 7000, |
||
648 | 'view other' => 1, |
||
649 | }; |
||
650 | } |
||
651 | } |
||
652 | } |
||
653 | |||
654 | return Category::query()->whereIn('root_categories_id', $ret)->pluck('id')->toArray(); |
||
655 | } |
||
656 | |||
657 | /** |
||
658 | * @throws \Exception |
||
659 | */ |
||
660 | public static function getCategoryExclusionForApi(Request $request): array |
||
666 | } |
||
667 | |||
668 | /** |
||
669 | * @return Collection|\Illuminate\Support\Collection|static[] |
||
670 | */ |
||
671 | public static function getTopGrabbers() |
||
672 | { |
||
673 | return self::query()->selectRaw('id, username, SUM(grabs) as grabs')->groupBy('id', 'username')->having('grabs', '>', 0)->orderByDesc('grabs')->limit(10)->get(); |
||
674 | } |
||
675 | |||
676 | /** |
||
677 | * @return Collection|\Illuminate\Support\Collection|static[] |
||
678 | */ |
||
679 | public static function getUsersByMonth() |
||
680 | { |
||
681 | return self::query()->whereNotNull('created_at')->where('created_at', '<>', '0000-00-00 00:00:00')->selectRaw("DATE_FORMAT(created_at, '%M %Y') as mth, COUNT(id) as num")->groupBy(['mth'])->orderByDesc('created_at')->get(); |
||
682 | } |
||
683 | |||
684 | /** |
||
685 | * @throws \Exception |
||
686 | */ |
||
687 | public static function sendInvite($serverUrl, $uid, $emailTo): string |
||
688 | { |
||
689 | $user = self::find($uid); |
||
690 | $token = Invite::invite($emailTo, $user->id); |
||
691 | $url = $serverUrl.'/register?invitecode='.$token; |
||
692 | |||
693 | Invitation::addInvite($uid, $token); |
||
694 | SendInviteEmail::dispatch($emailTo, $user, $url)->onQueue('emails'); |
||
695 | |||
696 | return $url; |
||
697 | } |
||
698 | |||
699 | /** |
||
700 | * Deletes users that have not verified their accounts for 3 or more days. |
||
701 | */ |
||
702 | public static function deleteUnVerified(): void |
||
703 | { |
||
704 | static::whereVerified(0)->where('created_at', '<', now()->subDays(3))->delete(); |
||
705 | } |
||
706 | |||
707 | public function passwordSecurity(): HasOne |
||
710 | } |
||
711 | } |
||
712 |
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: