From b29b8f517b0286f2f47ec63cc25fa966451ad9d8 Mon Sep 17 00:00:00 2001 From: Sajjad Esmaeeli Date: Sat, 13 Jan 2024 16:57:23 +0100 Subject: [PATCH 1/7] fix docblocks - add type hint for exceptions - fix return types --- src/Exceptions/InsufficientBalanceException.php | 9 ++------- src/Exceptions/InvalidDepositException.php | 9 ++------- src/Exceptions/InvalidValueException.php | 9 ++------- src/Exceptions/InvalidWalletTypeException.php | 3 ++- src/Exceptions/WalletNotFoundException.php | 3 ++- src/Facades/LaravelPayPocket.php | 11 ++++++----- src/Interfaces/WalletOperations.php | 10 ++++------ src/Services/PocketServices.php | 17 +++++++---------- src/Traits/HandlesDeposit.php | 11 +++-------- src/Traits/HandlesPayment.php | 4 +--- src/Traits/HasWallet.php | 8 +++++--- 11 files changed, 36 insertions(+), 58 deletions(-) diff --git a/src/Exceptions/InsufficientBalanceException.php b/src/Exceptions/InsufficientBalanceException.php index 1e87b6f..d77402c 100644 --- a/src/Exceptions/InsufficientBalanceException.php +++ b/src/Exceptions/InsufficientBalanceException.php @@ -3,16 +3,11 @@ namespace HPWebdeveloper\LaravelPayPocket\Exceptions; use Exception; +use Throwable; class InsufficientBalanceException extends Exception { - /** - * Construct the exception. - * - * @param string $message - * @param int $code - */ - public function __construct($message = 'Insufficient balance to cover the order', $code = 0, ?\Throwable $previous = null) + public function __construct(string $message = 'Insufficient balance to cover the order', int $code = 0, ?Throwable $previous = null) { parent::__construct($message, $code, $previous); } diff --git a/src/Exceptions/InvalidDepositException.php b/src/Exceptions/InvalidDepositException.php index 937011e..4f5f1a6 100644 --- a/src/Exceptions/InvalidDepositException.php +++ b/src/Exceptions/InvalidDepositException.php @@ -3,16 +3,11 @@ namespace HPWebdeveloper\LaravelPayPocket\Exceptions; use Exception; +use Throwable; class InvalidDepositException extends Exception { - /** - * Construct the exception. - * - * @param string $message - * @param int $code - */ - public function __construct($message = 'Invalid deposit operation', $code = 0, ?\Throwable $previous = null) + public function __construct(string $message = 'Invalid deposit operation', int $code = 0, ?Throwable $previous = null) { parent::__construct($message, $code, $previous); } diff --git a/src/Exceptions/InvalidValueException.php b/src/Exceptions/InvalidValueException.php index 7c351db..f5b48ca 100644 --- a/src/Exceptions/InvalidValueException.php +++ b/src/Exceptions/InvalidValueException.php @@ -3,16 +3,11 @@ namespace HPWebdeveloper\LaravelPayPocket\Exceptions; use Exception; +use Throwable; class InvalidValueException extends Exception { - /** - * Construct the exception. - * - * @param string $message - * @param int $code - */ - public function __construct($message = 'Invalie value to deposit', $code = 0, ?\Throwable $previous = null) + public function __construct(string $message = 'Invalie value to deposit', int $code = 0, ?Throwable $previous = null) { parent::__construct($message, $code, $previous); } diff --git a/src/Exceptions/InvalidWalletTypeException.php b/src/Exceptions/InvalidWalletTypeException.php index 5f35ef9..be6cd5d 100644 --- a/src/Exceptions/InvalidWalletTypeException.php +++ b/src/Exceptions/InvalidWalletTypeException.php @@ -3,10 +3,11 @@ namespace HPWebdeveloper\LaravelPayPocket\Exceptions; use Exception; +use Throwable; class InvalidWalletTypeException extends Exception { - public function __construct($message = 'Invalid wallet type', $code = 0, ?\Throwable $previous = null) + public function __construct(string $message = 'Invalid wallet type', int $code = 0, ?Throwable $previous = null) { parent::__construct($message, $code, $previous); } diff --git a/src/Exceptions/WalletNotFoundException.php b/src/Exceptions/WalletNotFoundException.php index 4b314a6..56139d7 100644 --- a/src/Exceptions/WalletNotFoundException.php +++ b/src/Exceptions/WalletNotFoundException.php @@ -3,10 +3,11 @@ namespace HPWebdeveloper\LaravelPayPocket\Exceptions; use Exception; +use Throwable; class WalletNotFoundException extends Exception { - public function __construct($message = 'Wallet not found', $code = 0, ?\Throwable $previous = null) + public function __construct(string $message = 'Wallet not found', int $code = 0, ?Throwable $previous = null) { parent::__construct($message, $code, $previous); } diff --git a/src/Facades/LaravelPayPocket.php b/src/Facades/LaravelPayPocket.php index 48e55f6..8d7092a 100644 --- a/src/Facades/LaravelPayPocket.php +++ b/src/Facades/LaravelPayPocket.php @@ -2,19 +2,20 @@ namespace HPWebdeveloper\LaravelPayPocket\Facades; +use HPWebdeveloper\LaravelPayPocket\Interfaces\WalletOperations; use Illuminate\Support\Facades\Facade; /** * @see \HPWebdeveloper\LaravelPayPocket\Services\PocketServices * - * @method static void pay(\Illuminate\Database\Eloquent\Model $user, int|float $orderValue, ?string $notes = null) - * @method static bool deposit(\Illuminate\Database\Eloquent\Model $user, string $type, int|float $amount, ?string $notes = null) - * @method static int|float checkBalance(\Illuminate\Database\Eloquent\Model $user) - * @method static int|float walletBalanceByType(\Illuminate\Database\Eloquent\Model $user, string $type) + * @method static void pay(WalletOperations $user, int|float $orderValue, ?string $notes = null) + * @method static bool deposit(WalletOperations $user, string $type, int|float $amount, ?string $notes = null) + * @method static int|float checkBalance(WalletOperations $user) + * @method static int|float walletBalanceByType(WalletOperations $user, string $type) */ class LaravelPayPocket extends Facade { - protected static function getFacadeAccessor() + protected static function getFacadeAccessor(): string { return \HPWebdeveloper\LaravelPayPocket\Services\PocketServices::class; } diff --git a/src/Interfaces/WalletOperations.php b/src/Interfaces/WalletOperations.php index cdffed0..9ec3a7b 100644 --- a/src/Interfaces/WalletOperations.php +++ b/src/Interfaces/WalletOperations.php @@ -2,6 +2,8 @@ namespace HPWebdeveloper\LaravelPayPocket\Interfaces; +use HPWebdeveloper\LaravelPayPocket\Exceptions\InsufficientBalanceException; + interface WalletOperations { /** @@ -12,7 +14,7 @@ public function getWalletBalanceAttribute(): int|float; /** * Get the balance of a specific wallet type. */ - public function getWalletBalanceByType(string $walletType): float|int; + public function getWalletBalanceByType(string $walletType): int|float; /** * Check if User's wallet balance is more than given value @@ -22,16 +24,12 @@ public function hasSufficientBalance(int|float $value): bool; /** * Pay the order value from the user's wallets. * - * @param ?string $notes - * - * @throws \HPWebdeveloper\LaravelPayPocket\Exceptions\InsufficientBalanceException + * @throws InsufficientBalanceException */ public function pay(int|float $orderValue, ?string $notes = null): void; /** * Deposit an amount to the user's wallet of a specific type. - * - * @param ?string $notes */ public function deposit(string $type, int|float $amount, ?string $notes = null): bool; } diff --git a/src/Services/PocketServices.php b/src/Services/PocketServices.php index 1ee674d..b9b337b 100644 --- a/src/Services/PocketServices.php +++ b/src/Services/PocketServices.php @@ -2,16 +2,15 @@ namespace HPWebdeveloper\LaravelPayPocket\Services; -use Illuminate\Database\Eloquent\Model; +use HPWebdeveloper\LaravelPayPocket\Exceptions\InsufficientBalanceException; +use HPWebdeveloper\LaravelPayPocket\Interfaces\WalletOperations; class PocketServices { /** * Deposit an amount to the user's wallet of a specific type. - * - * @param ?string $notes */ - public function deposit(Model $user, string $type, int|float $amount, ?string $notes = null): bool + public function deposit(WalletOperations $user, string $type, int|float $amount, ?string $notes = null): bool { return $user->deposit($type, $amount, $notes); } @@ -19,11 +18,9 @@ public function deposit(Model $user, string $type, int|float $amount, ?string $n /** * Pay the order value from the user's wallets. * - * @param ?string $notes - * - * @throws \HPWebdeveloper\LaravelPayPocket\Exceptions\InsufficientBalanceException + * @throws InsufficientBalanceException */ - public function pay(Model $user, int|float $orderValue, ?string $notes = null): void + public function pay(WalletOperations $user, int|float $orderValue, ?string $notes = null): void { $user->pay($orderValue, $notes); } @@ -31,7 +28,7 @@ public function pay(Model $user, int|float $orderValue, ?string $notes = null): /** * Get the balance of the user. */ - public function checkBalance(Model $user): int|float + public function checkBalance(WalletOperations $user): int|float { return $user->walletBalance; } @@ -39,7 +36,7 @@ public function checkBalance(Model $user): int|float /** * Get the balance of a specific wallet type. */ - public function walletBalanceByType(Model $user, string $type): float|int + public function walletBalanceByType(WalletOperations $user, string $type): int|float { return $user->getWalletBalanceByType($type); } diff --git a/src/Traits/HandlesDeposit.php b/src/Traits/HandlesDeposit.php index 2d8318a..87c817a 100644 --- a/src/Traits/HandlesDeposit.php +++ b/src/Traits/HandlesDeposit.php @@ -8,16 +8,14 @@ use HPWebdeveloper\LaravelPayPocket\Exceptions\InvalidWalletTypeException; use Illuminate\Support\Facades\DB; -// Use your defined exception - trait HandlesDeposit { /** * Deposit an amount to the user's wallet of a specific type. * - * @param ?string $notes - * - * @throws InvalidDepositException|InvalidValueException|InvalidWalletTypeException + * @throws InvalidDepositException + * @throws InvalidValueException + * @throws InvalidWalletTypeException */ public function deposit(string $type, int|float $amount, ?string $notes = null): bool { @@ -56,9 +54,6 @@ private function getDepositableTypes(): array /** * Check if the given type is valid. * - * @var string - * @var array - * * @throws InvalidWalletTypeException */ private function isRequestValid($type, array $depositable): bool diff --git a/src/Traits/HandlesPayment.php b/src/Traits/HandlesPayment.php index e40ec19..2ea0a44 100644 --- a/src/Traits/HandlesPayment.php +++ b/src/Traits/HandlesPayment.php @@ -10,9 +10,7 @@ trait HandlesPayment /** * Pay the order value from the user's wallets. * - * @param ?string $notes - * - * @throws \HPWebdeveloper\LaravelPayPocket\Exceptions\InsufficientBalanceException + * @throws InsufficientBalanceException */ public function pay(int|float $orderValue, ?string $notes = null): void { diff --git a/src/Traits/HasWallet.php b/src/Traits/HasWallet.php index 6748dfa..1191ca1 100644 --- a/src/Traits/HasWallet.php +++ b/src/Traits/HasWallet.php @@ -6,6 +6,7 @@ use HPWebdeveloper\LaravelPayPocket\Exceptions\InvalidWalletTypeException; use HPWebdeveloper\LaravelPayPocket\Exceptions\WalletNotFoundException; use HPWebdeveloper\LaravelPayPocket\Models\Wallet; +use Illuminate\Database\Eloquent\Relations\MorphMany; trait HasWallet { @@ -16,7 +17,7 @@ trait HasWallet /** * Has Many Relation with Wallet Model */ - public function wallets() + public function wallets(): MorphMany { return $this->morphMany(Wallet::class, 'owner'); } @@ -26,7 +27,6 @@ public function wallets() */ public function getWalletBalanceAttribute(): int|float { - $totalBalance = 0; foreach ($this->walletsInOrder() as $walletInOrder) { @@ -51,8 +51,10 @@ public function hasSufficientBalance(int|float $value): bool /** * Get the balance of a specific wallet type. + * + * @throws InvalidWalletTypeException|WalletNotFoundException */ - public function getWalletBalanceByType(string $walletType): float|int + public function getWalletBalanceByType(string $walletType): int|float { if (! WalletEnums::isValid($walletType)) { throw new InvalidWalletTypeException("Invalid wallet type '{$walletType}'."); From 63d12bc559ade6e3f74943701adf84fc803267ea Mon Sep 17 00:00:00 2001 From: Sajjad Esmaeeli Date: Sat, 13 Jan 2024 17:07:06 +0100 Subject: [PATCH 2/7] add get wallet balance method as getter --- src/Interfaces/WalletOperations.php | 5 +++++ src/Services/PocketServices.php | 2 +- src/Traits/HasWallet.php | 5 +++++ 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/Interfaces/WalletOperations.php b/src/Interfaces/WalletOperations.php index 9ec3a7b..572bf82 100644 --- a/src/Interfaces/WalletOperations.php +++ b/src/Interfaces/WalletOperations.php @@ -32,4 +32,9 @@ public function pay(int|float $orderValue, ?string $notes = null): void; * Deposit an amount to the user's wallet of a specific type. */ public function deposit(string $type, int|float $amount, ?string $notes = null): bool; + + /** + * Get user's wallet balance. + */ + public function getWalletBalance(): int|float; } diff --git a/src/Services/PocketServices.php b/src/Services/PocketServices.php index b9b337b..71f5244 100644 --- a/src/Services/PocketServices.php +++ b/src/Services/PocketServices.php @@ -30,7 +30,7 @@ public function pay(WalletOperations $user, int|float $orderValue, ?string $note */ public function checkBalance(WalletOperations $user): int|float { - return $user->walletBalance; + return $user->getWalletBalance(); } /** diff --git a/src/Traits/HasWallet.php b/src/Traits/HasWallet.php index 1191ca1..d3826f6 100644 --- a/src/Traits/HasWallet.php +++ b/src/Traits/HasWallet.php @@ -68,4 +68,9 @@ public function getWalletBalanceByType(string $walletType): int|float return $wallet->balance; } + + public function getWalletBalance(): int|float + { + return $this->walletBalance; + } } From 19a56b101637d76c47f32211ab4159d36cb4cebe Mon Sep 17 00:00:00 2001 From: Sajjad Esmaeeli Date: Sun, 14 Jan 2024 00:08:24 +0100 Subject: [PATCH 3/7] remove extra params - add type hint --- src/Traits/BalanceOperation.php | 6 ------ src/Traits/Loggable.php | 3 ++- 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/src/Traits/BalanceOperation.php b/src/Traits/BalanceOperation.php index a559693..d19a979 100644 --- a/src/Traits/BalanceOperation.php +++ b/src/Traits/BalanceOperation.php @@ -18,8 +18,6 @@ public function hasBalance(): bool /** * Decrement Balance and create a log entry. - * - * @param ?string $notes */ public function decrementAndCreateLog(int|float $value, ?string $notes = null): void { @@ -29,8 +27,6 @@ public function decrementAndCreateLog(int|float $value, ?string $notes = null): /** * Increment Balance and create a log entry. - * - * @param ?string $notes */ public function incrementAndCreateLog(int|float $value, ?string $notes = null): void { @@ -40,8 +36,6 @@ public function incrementAndCreateLog(int|float $value, ?string $notes = null): /** * Create a new log record - * - * @param ?string $notes */ protected function createLog(string $logType, int|float $value, ?string $notes = null): void { diff --git a/src/Traits/Loggable.php b/src/Traits/Loggable.php index deddcf9..09b567c 100644 --- a/src/Traits/Loggable.php +++ b/src/Traits/Loggable.php @@ -3,10 +3,11 @@ namespace HPWebdeveloper\LaravelPayPocket\Traits; use HPWebdeveloper\LaravelPayPocket\Models\WalletsLog; +use Illuminate\Database\Eloquent\Relations\MorphMany; trait Loggable { - public function logs() + public function logs(): MorphMany { return $this->morphMany(WalletsLog::class, 'loggable'); } From 2e3e7b91b2fc54a58456e4f77a71d1175b3ed9bd Mon Sep 17 00:00:00 2001 From: Sajjad Esmaeeli Date: Sun, 14 Jan 2024 01:55:51 +0100 Subject: [PATCH 4/7] fix problem on reference generator - refactor --- config/pay-pocket.php | 5 ++-- src/Models/WalletsLog.php | 8 +++++++ src/Traits/BalanceOperation.php | 41 ++++++++++++++++++++------------- 3 files changed, 36 insertions(+), 18 deletions(-) diff --git a/config/pay-pocket.php b/config/pay-pocket.php index 5c8e375..9dd0dbe 100644 --- a/config/pay-pocket.php +++ b/config/pay-pocket.php @@ -14,6 +14,7 @@ */ return [ 'log_reference_length' => 12, - 'log_reference_prefix' => null, - 'log_reference_generator' => null, + 'log_reference_prefix' => '', + 'log_reference_generator_class' => Illuminate\Support\Str::class, + 'log_reference_generator_method' => 'random', ]; diff --git a/src/Models/WalletsLog.php b/src/Models/WalletsLog.php index b7a4130..91038e6 100644 --- a/src/Models/WalletsLog.php +++ b/src/Models/WalletsLog.php @@ -10,6 +10,14 @@ * HPWebdeveloper\LaravelPayPocket\Models\WalletsLog * * @property string $status + * @property int|float $from + * @property int|float $to + * @property string $type + * @property string $ip + * @property int|float $value + * @property string $wallet_name + * @property string $notes + * @property string $reference */ class WalletsLog extends Model { diff --git a/src/Traits/BalanceOperation.php b/src/Traits/BalanceOperation.php index d19a979..0f43ba9 100644 --- a/src/Traits/BalanceOperation.php +++ b/src/Traits/BalanceOperation.php @@ -2,11 +2,12 @@ namespace HPWebdeveloper\LaravelPayPocket\Traits; -use Illuminate\Support\Str; +use HPWebdeveloper\LaravelPayPocket\Models\WalletsLog; +use InvalidArgumentException; trait BalanceOperation { - protected $createdLog; + protected WalletsLog $createdLog; /** * Check if Balance is more than zero. @@ -43,29 +44,37 @@ protected function createLog(string $logType, int|float $value, ?string $notes = $newBalance = $logType === 'dec' ? $currentBalance - $value : $currentBalance + $value; - $refGen = config('pay-pocket.log_reference_generator', [ - Str::class, 'random', [config('pay-pocket.log_reference_length', 12)], - ]); - $refGen = [ - Str::class, 'random', [config('pay-pocket.log_reference_length', 12)], - ]; - - $reference = config('pay-pocket.reference_string_prefix', ''); - $reference .= isset($refGen[0], $refGen[1]) - ? $refGen[0]::{$refGen[1]}(...$refGen[2] ?? []) - : Str::random(config('pay-pocket.log_reference_length', 12)); - + /** @var \Illuminate\Database\Eloquent\Model $this */ $this->createdLog = $this->logs()->create([ 'wallet_name' => $this->type->value, 'from' => $currentBalance, 'to' => $newBalance, 'type' => $logType, - 'ip' => \Request::ip(), + 'ip' => request()->ip(), 'value' => $value, 'notes' => $notes, - 'reference' => $reference, + 'reference' => $this->generateReference(), ]); $this->createdLog->changeStatus('Done'); } + + /** + * @throws InvalidArgumentException + */ + protected function generateReference(): string + { + $className = config('pay-pocket.log_reference_generator_class'); + $methodName = config('pay-pocket.log_reference_generator_method'); + $length = config('pay-pocket.log_reference_length'); + $prefix = config('pay-pocket.log_reference_prefix'); + + if (!is_callable([$className, $methodName])) { + throw new InvalidArgumentException('Invalid configuration: The combination of log_reference_generator_class and log_reference_generator_method is not callable.'); + } + + $reference = call_user_func([$className, $methodName], $length); + + return $prefix . $reference; + } } From 3d9b8d5bf499091d10a346d74e4c407bb378948f Mon Sep 17 00:00:00 2001 From: SSEsmaeeli Date: Sun, 14 Jan 2024 00:56:19 +0000 Subject: [PATCH 5/7] Fix styling --- src/Traits/BalanceOperation.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Traits/BalanceOperation.php b/src/Traits/BalanceOperation.php index 0f43ba9..a19d44a 100644 --- a/src/Traits/BalanceOperation.php +++ b/src/Traits/BalanceOperation.php @@ -69,12 +69,12 @@ protected function generateReference(): string $length = config('pay-pocket.log_reference_length'); $prefix = config('pay-pocket.log_reference_prefix'); - if (!is_callable([$className, $methodName])) { + if (! is_callable([$className, $methodName])) { throw new InvalidArgumentException('Invalid configuration: The combination of log_reference_generator_class and log_reference_generator_method is not callable.'); } $reference = call_user_func([$className, $methodName], $length); - return $prefix . $reference; + return $prefix.$reference; } } From 24fe14fe5365597f75a3339caa727ec5e2cfcc35 Mon Sep 17 00:00:00 2001 From: Sajjad Esmaeeli Date: Sun, 14 Jan 2024 02:03:05 +0100 Subject: [PATCH 6/7] fix phpstan --- src/Traits/BalanceOperation.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Traits/BalanceOperation.php b/src/Traits/BalanceOperation.php index 0f43ba9..2d2a8af 100644 --- a/src/Traits/BalanceOperation.php +++ b/src/Traits/BalanceOperation.php @@ -44,7 +44,6 @@ protected function createLog(string $logType, int|float $value, ?string $notes = $newBalance = $logType === 'dec' ? $currentBalance - $value : $currentBalance + $value; - /** @var \Illuminate\Database\Eloquent\Model $this */ $this->createdLog = $this->logs()->create([ 'wallet_name' => $this->type->value, 'from' => $currentBalance, From 1c5b4d0924988b6e8dd97254bd066bd06e0a92be Mon Sep 17 00:00:00 2001 From: Sajjad Esmaeeli Date: Sun, 14 Jan 2024 02:09:25 +0100 Subject: [PATCH 7/7] refactor --- src/Traits/GetWallets.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/Traits/GetWallets.php b/src/Traits/GetWallets.php index 4606dd4..7c42a98 100644 --- a/src/Traits/GetWallets.php +++ b/src/Traits/GetWallets.php @@ -6,12 +6,10 @@ trait GetWallets { - private function walletsInOrder() + private function walletsInOrder(): array { return array_map( - function ($enumCase) { - return $enumCase->value; - }, + fn ($enumCase) => $enumCase->value, WalletEnums::cases() ); }