From 31e073d6207c2378a0ae275e19730661db6e46f4 Mon Sep 17 00:00:00 2001 From: Mohammad Hafijul Islam Date: Thu, 5 Sep 2024 23:55:21 +0600 Subject: [PATCH] code optimization added --- config/auth.php | 27 ++++++++++++------- src/Interfaces/LoginAttemptRepository.php | 3 ++- src/Models/LoginAttempt.php | 3 ++- src/Notifications/OTPNotification.php | 3 ++- src/Providers/RepositoryServiceProvider.php | 5 ++-- .../Eloquent/LoginAttemptRepository.php | 3 ++- .../Mongodb/LoginAttemptRepository.php | 3 ++- src/Services/PasswordResetService.php | 3 ++- src/Services/PinResetService.php | 3 ++- src/Services/ProfileService.php | 3 ++- src/Services/UserService.php | 10 ++++--- src/Services/Vendors/GeoIp/IpApi.php | 3 ++- tests/Feature/LoginTest.php | 1 - tests/Feature/PermissionTest.php | 1 - tests/Feature/RoleTest.php | 1 - 15 files changed, 45 insertions(+), 27 deletions(-) diff --git a/config/auth.php b/config/auth.php index 25b9284..22d0c20 100644 --- a/config/auth.php +++ b/config/auth.php @@ -3,6 +3,7 @@ // config for fintech/auth use Fintech\Auth\Models\Audit; use Fintech\Auth\Models\Favourite; +use Fintech\Auth\Models\LoginAttempt; use Fintech\Auth\Models\OneTimePin; use Fintech\Auth\Models\Permission; use Fintech\Auth\Models\Profile; @@ -18,6 +19,14 @@ use Fintech\Auth\Repositories\Eloquent\RoleRepository; use Fintech\Auth\Repositories\Eloquent\TeamRepository; use Fintech\Auth\Repositories\Eloquent\UserRepository; +use Fintech\Auth\Services\Vendors\GeoIp\Cloudflare; +use Fintech\Auth\Services\Vendors\GeoIp\Ip2Location; +use Fintech\Auth\Services\Vendors\GeoIp\IpApi; +use Fintech\Auth\Services\Vendors\GeoIp\IpData; +use Fintech\Auth\Services\Vendors\GeoIp\IpInfo; +use Fintech\Auth\Services\Vendors\GeoIp\Kloudend; +use Fintech\Auth\Services\Vendors\GeoIp\Local; +use Fintech\Auth\Services\Vendors\GeoIp\MaxMind; return [ @@ -147,7 +156,7 @@ | | This value will be used to across system where model is needed */ - 'login_attempt_model' => \Fintech\Auth\Models\LoginAttempt::class, + 'login_attempt_model' => LoginAttempt::class, 'record_login_attempt' => env('PACKAGE_AUTH_RECORD_LOGIN_ATTEMPT', false), /* |-------------------------------------------------------------------------- @@ -160,35 +169,35 @@ 'default' => env('PACKAGE_AUTH_GEOIP_DRIVER', null), 'drivers' => [ 'local' => [ - 'class' => \Fintech\Auth\Services\Vendors\GeoIp\Local::class, + 'class' => Local::class, 'type' => 'city', 'path' => 'maxmind/GeoLite2-City.mmdb', ], 'ipapi' => [ - 'class' => \Fintech\Auth\Services\Vendors\GeoIp\IpApi::class, + 'class' => IpApi::class, 'token' => env('PACKAGE_AUTH_IPAPI_TOKEN'), ], 'ipinfo' => [ - 'class' => \Fintech\Auth\Services\Vendors\GeoIp\IpInfo::class, + 'class' => IpInfo::class, 'token' => env('PACKAGE_AUTH_IPINFO_TOKEN'), ], 'ipdata' => [ - 'class' => \Fintech\Auth\Services\Vendors\GeoIp\IpData::class, + 'class' => IpData::class, 'token' => env('PACKAGE_AUTH_IPDATA_TOKEN'), ], 'ip2location' => [ - 'class' => \Fintech\Auth\Services\Vendors\GeoIp\Ip2Location::class, + 'class' => Ip2Location::class, 'token' => env('PACKAGE_AUTH_IP2LOCATION_TOKEN'), ], 'cloudflare' => [ - 'class' => \Fintech\Auth\Services\Vendors\GeoIp\Cloudflare::class, + 'class' => Cloudflare::class, ], 'kloudend' => [ - 'class' => \Fintech\Auth\Services\Vendors\GeoIp\Kloudend::class, + 'class' => Kloudend::class, 'token' => env('PACKAGE_AUTH_KLOUDEND_TOKEN'), ], 'maxmind' => [ - 'class' => \Fintech\Auth\Services\Vendors\GeoIp\MaxMind::class, + 'class' => MaxMind::class, 'user_id' => env('PACKAGE_AUTH_MAXMIND_USER_ID'), 'license_key' => env('PACKAGE_AUTH_MAXMIND_LICENSE_KEY'), 'options' => ['host' => 'geoip.maxmind.com'], diff --git a/src/Interfaces/LoginAttemptRepository.php b/src/Interfaces/LoginAttemptRepository.php index 090a6c0..f14360c 100644 --- a/src/Interfaces/LoginAttemptRepository.php +++ b/src/Interfaces/LoginAttemptRepository.php @@ -5,6 +5,7 @@ use Fintech\Core\Abstracts\BaseModel; use Illuminate\Contracts\Pagination\Paginator; use Illuminate\Support\Collection; +use InvalidArgumentException; /** * Interface LoginAttemptRepository @@ -48,7 +49,7 @@ public function delete(int|string $id); * find and restore a entry from records * * @param int|string $id - * @throws \InvalidArgumentException + * @throws InvalidArgumentException */ public function restore(int|string $id); } diff --git a/src/Models/LoginAttempt.php b/src/Models/LoginAttempt.php index a813937..531e6b5 100644 --- a/src/Models/LoginAttempt.php +++ b/src/Models/LoginAttempt.php @@ -4,6 +4,7 @@ use Fintech\Core\Abstracts\BaseModel; use Fintech\Core\Traits\AuditableTrait; +use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\SoftDeletes; class LoginAttempt extends BaseModel @@ -37,7 +38,7 @@ class LoginAttempt extends BaseModel | RELATIONS |-------------------------------------------------------------------------- */ - public function user(): \Illuminate\Database\Eloquent\Relations\BelongsTo + public function user(): BelongsTo { return $this->belongsTo(config('fintech.auth.user_model', User::class), 'user_id'); } diff --git a/src/Notifications/OTPNotification.php b/src/Notifications/OTPNotification.php index 1ac3af9..b4d03af 100644 --- a/src/Notifications/OTPNotification.php +++ b/src/Notifications/OTPNotification.php @@ -2,6 +2,7 @@ namespace Fintech\Auth\Notifications; +use Fintech\Auth\Models\User; use Fintech\Core\Enums\Auth\OTPOption; use Illuminate\Bus\Queueable; use Illuminate\Notifications\Messages\MailMessage; @@ -40,7 +41,7 @@ public function via(object $notifiable): array } if (request()->filled('user')) { - $userModel = config('fintech.auth.user_model', \Fintech\Auth\Models\User::class); + $userModel = config('fintech.auth.user_model', User::class); return ($notifiable instanceof $userModel) ? $notifiable->prefer ? [$notifiable->prefer] : ['mail'] : ['database']; diff --git a/src/Providers/RepositoryServiceProvider.php b/src/Providers/RepositoryServiceProvider.php index a937ca5..7f10bbc 100644 --- a/src/Providers/RepositoryServiceProvider.php +++ b/src/Providers/RepositoryServiceProvider.php @@ -6,6 +6,7 @@ use Illuminate\Contracts\Support\DeferrableProvider; use Illuminate\Support\Facades\Config; use Illuminate\Support\ServiceProvider; +use InvalidArgumentException; class RepositoryServiceProvider extends ServiceProvider implements DeferrableProvider { @@ -25,7 +26,7 @@ public function register(): void if ($current = config('fintech.auth.geoip.default')) { if (!config("fintech.auth.geoip.drivers.{$current}")) { - throw new \InvalidArgumentException("No driver configuration found named `{$current}`."); + throw new InvalidArgumentException("No driver configuration found named `{$current}`."); } $config = config("fintech.auth.geoip.drivers.{$current}"); @@ -37,7 +38,7 @@ public function register(): void return new $class($config); } else { - throw new \InvalidArgumentException("No driver is assigned for GeoIP Service."); + throw new InvalidArgumentException("No driver is assigned for GeoIP Service."); } }); } diff --git a/src/Repositories/Eloquent/LoginAttemptRepository.php b/src/Repositories/Eloquent/LoginAttemptRepository.php index 6c783ce..c93b1ed 100644 --- a/src/Repositories/Eloquent/LoginAttemptRepository.php +++ b/src/Repositories/Eloquent/LoginAttemptRepository.php @@ -3,6 +3,7 @@ namespace Fintech\Auth\Repositories\Eloquent; use Fintech\Auth\Interfaces\LoginAttemptRepository as InterfacesLoginAttemptRepository; +use Fintech\Auth\Models\LoginAttempt; use Fintech\Core\Repositories\EloquentRepository; use Illuminate\Contracts\Pagination\Paginator; use Illuminate\Database\Eloquent\Collection; @@ -15,7 +16,7 @@ class LoginAttemptRepository extends EloquentRepository implements InterfacesLog { public function __construct() { - parent::__construct(config('fintech.auth.login_attempt_model', \Fintech\Auth\Models\LoginAttempt::class)); + parent::__construct(config('fintech.auth.login_attempt_model', LoginAttempt::class)); } /** diff --git a/src/Repositories/Mongodb/LoginAttemptRepository.php b/src/Repositories/Mongodb/LoginAttemptRepository.php index 74eb3de..d6a70ce 100644 --- a/src/Repositories/Mongodb/LoginAttemptRepository.php +++ b/src/Repositories/Mongodb/LoginAttemptRepository.php @@ -3,6 +3,7 @@ namespace Fintech\Auth\Repositories\Mongodb; use Fintech\Auth\Interfaces\LoginAttemptRepository as InterfacesLoginAttemptRepository; +use Fintech\Auth\Models\LoginAttempt; use Fintech\Core\Repositories\MongodbRepository; use Illuminate\Contracts\Pagination\Paginator; use Illuminate\Database\Eloquent\Collection; @@ -15,7 +16,7 @@ class LoginAttemptRepository extends MongodbRepository implements InterfacesLogi { public function __construct() { - parent::__construct(config('fintech.auth.login_attempt_model', \Fintech\Auth\Models\LoginAttempt::class)); + parent::__construct(config('fintech.auth.login_attempt_model', LoginAttempt::class)); } /** diff --git a/src/Services/PasswordResetService.php b/src/Services/PasswordResetService.php index 9bfa897..efa1918 100644 --- a/src/Services/PasswordResetService.php +++ b/src/Services/PasswordResetService.php @@ -39,7 +39,8 @@ class PasswordResetService public function __construct( private readonly OneTimePinRepository $oneTimePinRepository, private readonly UserRepository $userRepository - ) { + ) + { $this->passwordField = config('fintech.auth.password_field', 'password'); $this->resetMethod = config('fintech.auth.password_reset_method', PasswordResetOption::ResetLink->value); diff --git a/src/Services/PinResetService.php b/src/Services/PinResetService.php index 30b0475..a0e1ee9 100644 --- a/src/Services/PinResetService.php +++ b/src/Services/PinResetService.php @@ -37,7 +37,8 @@ class PinResetService public function __construct( private readonly OneTimePinRepository $oneTimePinRepository, private readonly UserRepository $userRepository - ) { + ) + { $this->pinField = config('fintech.auth.pin_field', 'pin'); $this->resetMethod = config('fintech.auth.password_reset_method', PasswordResetOption::ResetLink->value); diff --git a/src/Services/ProfileService.php b/src/Services/ProfileService.php index cad8cea..25cb6e6 100644 --- a/src/Services/ProfileService.php +++ b/src/Services/ProfileService.php @@ -26,7 +26,8 @@ class ProfileService */ public function __construct( private readonly ProfileRepository $profileRepository - ) { + ) + { } public function create(string|int $user_id, array $inputs = []) diff --git a/src/Services/UserService.php b/src/Services/UserService.php index ba0691d..a7e439f 100644 --- a/src/Services/UserService.php +++ b/src/Services/UserService.php @@ -24,6 +24,7 @@ use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Hash; use PDOException; +use stdClass; /** * Class UserService @@ -41,7 +42,8 @@ class UserService public function __construct( private readonly UserRepository $userRepository, private readonly ProfileRepository $profileRepository - ) { + ) + { $this->loginAttempt = []; } @@ -298,7 +300,7 @@ public function login(array $inputs, string $guard = 'web') if ($attemptUser->tokens->isNotEmpty()) { - $attemptUser->tokens->each(fn ($token) => $token->delete()); + $attemptUser->tokens->each(fn($token) => $token->delete()); event(new OtherDeviceLogout($guard, $attemptUser)); } @@ -363,8 +365,8 @@ private function loginAttemptData($user_id, $status, $note): array $ipAddress['continent_name']]); $country = MetaData::country()->list(['iso2' => $ipAddress['country_code']])->first(); - $state = new \stdClass(); - $city = new \stdClass(); + $state = new stdClass(); + $city = new stdClass(); if ($country) { $state = MetaData::state()->list(['country_id' => $country->id, 'search' => $ipAddress['region_name']])->first(); diff --git a/src/Services/Vendors/GeoIp/IpApi.php b/src/Services/Vendors/GeoIp/IpApi.php index 172e5ea..49be84e 100644 --- a/src/Services/Vendors/GeoIp/IpApi.php +++ b/src/Services/Vendors/GeoIp/IpApi.php @@ -4,6 +4,7 @@ use Fintech\Auth\Interfaces\GeoIp; use Illuminate\Support\Facades\Http; +use InvalidArgumentException; class IpApi implements GeoIp { @@ -17,7 +18,7 @@ class IpApi implements GeoIp public function __construct(array $config = []) { if (!$config['token']) { - throw new \InvalidArgumentException("IP API Access Key is missing."); + throw new InvalidArgumentException("IP API Access Key is missing."); } $this->token = $config['token']; diff --git a/tests/Feature/LoginTest.php b/tests/Feature/LoginTest.php index dd68b62..1438b1e 100644 --- a/tests/Feature/LoginTest.php +++ b/tests/Feature/LoginTest.php @@ -1,7 +1,6 @@