From 0620e4284262828bfa95146bebd4242e21f6dfba Mon Sep 17 00:00:00 2001 From: 3m1n3nc3 Date: Sat, 13 Jan 2024 02:39:02 +0100 Subject: [PATCH 01/14] Improve type annotations and PHPDoc --- src/Facades/LaravelPayPocket.php | 5 +++ src/Interfaces/WalletOperations.php | 45 +++++++++++++++++++++++--- src/Services/PocketServices.php | 49 ++++++++++++++++++++++++++--- src/Traits/BalanceOperation.php | 36 ++++++++++++++++----- src/Traits/HandlesDeposit.php | 18 +++++++++-- src/Traits/HandlesPayment.php | 3 ++ src/Traits/HasWallet.php | 19 ++++++++--- tests/Models/User.php | 3 +- 8 files changed, 153 insertions(+), 25 deletions(-) diff --git a/src/Facades/LaravelPayPocket.php b/src/Facades/LaravelPayPocket.php index 1c33b5f..48e55f6 100644 --- a/src/Facades/LaravelPayPocket.php +++ b/src/Facades/LaravelPayPocket.php @@ -6,6 +6,11 @@ /** * @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) */ class LaravelPayPocket extends Facade { diff --git a/src/Interfaces/WalletOperations.php b/src/Interfaces/WalletOperations.php index 47e132d..06163b2 100644 --- a/src/Interfaces/WalletOperations.php +++ b/src/Interfaces/WalletOperations.php @@ -4,13 +4,50 @@ interface WalletOperations { - public function getWalletBalanceAttribute(); + /** + * Get User's Wallet Balance + * + * @return int|float + */ + public function getWalletBalanceAttribute(): int|float; - public function getWalletBalanceByType(string $walletType); + /** + * Get the balance of a specific wallet type. + * + * + * @param string $walletType + * + * @return float|int + */ + public function getWalletBalanceByType(string $walletType): float|int; - public function hasSufficientBalance($value): bool; + /** + * Check if User's wallet balance is more than given value + * + * @param int|float $value + * + * @return bool + */ + public function hasSufficientBalance(int|float $value): bool; - public function pay(int|float $orderValue, ?string $notes = null); + /** + * Pay the order value from the user's wallets. + * + * @param int|float $orderValue + * @param ?string $notes + * + * @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 $type + * @param int|float $amount + * @param ?string $notes + * + * @return bool + */ 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 91db495..a1d6e08 100644 --- a/src/Services/PocketServices.php +++ b/src/Services/PocketServices.php @@ -2,24 +2,63 @@ namespace HPWebdeveloper\LaravelPayPocket\Services; +use Illuminate\Database\Eloquent\Model; + class PocketServices { - public function deposit($user, $type, $amount, $notes = null) + /** + * Deposit an amount to the user's wallet of a specific type. + * + * @param \Illuminate\Database\Eloquent\Model $user + * @param string $type + * @param int|float $amount + * @param ?string $notes + * + * @return bool + */ + public function deposit(Model $user, string $type, int|float $amount, ?string $notes = null): bool { return $user->deposit($type, $amount, $notes); } - public function pay($user, $orderValue, $notes = null) + /** + * Pay the order value from the user's wallets. + * + * @param \Illuminate\Database\Eloquent\Model $user + * @param int|float $orderValue + * @param ?string $notes + * + * @return void + * @throws InsufficientBalanceException + */ + public function pay(Model $user, int|float $orderValue, ?string $notes = null): void { - return $user->pay($orderValue, $notes); + $user->pay($orderValue, $notes); } - public function checkBalance($user) + /** + * Get the balance of the user. + * + * + * @param \Illuminate\Database\Eloquent\Model $user + * + * @return float|int + */ + public function checkBalance(Model $user): int|float { return $user->walletBalance; } - public function walletBalanceByType($user, $type) + /** + * Get the balance of a specific wallet type. + * + * + * @param \Illuminate\Database\Eloquent\Model $user + * @param string $type + * + * @return float|int + */ + public function walletBalanceByType(Model $user, string $type): float|int { return $user->getWalletBalanceByType($type); } diff --git a/src/Traits/BalanceOperation.php b/src/Traits/BalanceOperation.php index abbd4a5..95f8041 100644 --- a/src/Traits/BalanceOperation.php +++ b/src/Traits/BalanceOperation.php @@ -9,7 +9,9 @@ trait BalanceOperation protected $createdLog; /** - * Check if Balance is more than zero. + * Check if Balance is more than zero. + * + * @return bool */ public function hasBalance(): bool { @@ -17,27 +19,43 @@ public function hasBalance(): bool } /** - * Decrement Balance and create a log entry. + * Decrement Balance and create a log entry. + * + * @param int|float $value + * @param ?string $notes + * + * @return void */ - public function decrementAndCreateLog($value, $notes = null): void + public function decrementAndCreateLog(int|float $value, ?string $notes = null): void { $this->createLog('dec', $value, $notes); $this->decrement('balance', $value); } /** - * Increment Balance and create a log entry. + * Increment Balance and create a log entry. + * + * @param int|float $value + * @param ?string $notes + * + * @return void */ - public function incrementAndCreateLog($value, $notes = null): void + public function incrementAndCreateLog(int|float $value, ?string $notes = null): void { $this->createLog('inc', $value, $notes); $this->increment('balance', $value); } /** - * Create a new log record + * Create a new log record + * + * @param string $logType + * @param int|float $value + * @param ?string $notes + * + * @return void */ - protected function createLog($logType, $value, $notes = null): void + protected function createLog(string $logType, int|float $value, ?string $notes = null): void { $currentBalance = $this->balance ?? 0; @@ -46,6 +64,10 @@ protected function createLog($logType, $value, $notes = null): void $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] ?? []) diff --git a/src/Traits/HandlesDeposit.php b/src/Traits/HandlesDeposit.php index 03cbaf8..ba5f683 100644 --- a/src/Traits/HandlesDeposit.php +++ b/src/Traits/HandlesDeposit.php @@ -14,6 +14,13 @@ trait HandlesDeposit { /** * Deposit an amount to the user's wallet of a specific type. + * + * @param string $type + * @param int|float $amount + * @param ?string $notes + * + * @return bool + * @throws InvalidDepositException|InvalidValueException|InvalidWalletTypeException */ public function deposit(string $type, int|float $amount, ?string $notes = null): bool { @@ -38,6 +45,8 @@ public function deposit(string $type, int|float $amount, ?string $notes = null): /** * Get depositable types from WalletEnums. + * + * @return array */ private function getDepositableTypes(): array { @@ -50,12 +59,15 @@ private function getDepositableTypes(): array } /** - * Check if the given tyep is valid. + * Check if the given type is valid. + * + * @var string $type + * @var array $depositable * - * @param string $type * @return bool + * @throws InvalidWalletTypeException */ - private function isRequestValid($type, array $depositable) + private function isRequestValid($type, array $depositable): bool { if (! array_key_exists($type, $depositable)) { throw new InvalidWalletTypeException('Invalid deposit type.'); diff --git a/src/Traits/HandlesPayment.php b/src/Traits/HandlesPayment.php index 4bfa835..2f5d608 100644 --- a/src/Traits/HandlesPayment.php +++ b/src/Traits/HandlesPayment.php @@ -10,7 +10,10 @@ trait HandlesPayment /** * Pay the order value from the user's wallets. * + * @param int|float $orderValue + * @param ?string $notes * + * @return void * @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 d30ca89..8f945e8 100644 --- a/src/Traits/HasWallet.php +++ b/src/Traits/HasWallet.php @@ -22,9 +22,11 @@ public function wallets() } /** - * Get User's Wallet Balance + * Get User's Wallet Balance + * + * @return int|float */ - public function getWalletBalanceAttribute() + public function getWalletBalanceAttribute(): int|float { $totalBalance = 0; @@ -39,23 +41,30 @@ public function getWalletBalanceAttribute() } return $totalBalance; - } /** * Check if User's wallet balance is more than given value + * + * @param int|float $value + * + * @return bool */ - public function hasSufficientBalance($value): bool + public function hasSufficientBalance(int|float $value): bool { return (int) $this->walletBalance >= (int) $value; } + /** * Get the balance of a specific wallet type. * + * + * @param string $walletType + * * @return float|int */ - public function getWalletBalanceByType(string $walletType) + public function getWalletBalanceByType(string $walletType): float|int { if (! WalletEnums::isValid($walletType)) { throw new InvalidWalletTypeException("Invalid wallet type '{$walletType}'."); diff --git a/tests/Models/User.php b/tests/Models/User.php index 7a10991..c9a560a 100644 --- a/tests/Models/User.php +++ b/tests/Models/User.php @@ -11,7 +11,8 @@ class User extends Authenticatable implements WalletOperations { - use HasFactory, Notifiable; + use HasFactory; + use Notifiable; use ManagesWallet; /** From c6dbb69e40f55560150fd80e2f7e62b7b7b6e864 Mon Sep 17 00:00:00 2001 From: 3m1n3nc3 Date: Sat, 13 Jan 2024 02:45:29 +0100 Subject: [PATCH 02/14] Fix missing exception --- src/Interfaces/WalletOperations.php | 2 +- src/Services/PocketServices.php | 4 ++-- src/Traits/HandlesPayment.php | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Interfaces/WalletOperations.php b/src/Interfaces/WalletOperations.php index 06163b2..c41dfce 100644 --- a/src/Interfaces/WalletOperations.php +++ b/src/Interfaces/WalletOperations.php @@ -36,7 +36,7 @@ public function hasSufficientBalance(int|float $value): bool; * @param int|float $orderValue * @param ?string $notes * - * @throws InsufficientBalanceException + * @throws \HPWebdeveloper\LaravelPayPocket\Exceptions\InsufficientBalanceException */ public function pay(int|float $orderValue, ?string $notes = null): void; diff --git a/src/Services/PocketServices.php b/src/Services/PocketServices.php index a1d6e08..e5c836d 100644 --- a/src/Services/PocketServices.php +++ b/src/Services/PocketServices.php @@ -29,7 +29,7 @@ public function deposit(Model $user, string $type, int|float $amount, ?string $n * @param ?string $notes * * @return void - * @throws InsufficientBalanceException + * @throws \HPWebdeveloper\LaravelPayPocket\Exceptions\InsufficientBalanceException */ public function pay(Model $user, int|float $orderValue, ?string $notes = null): void { @@ -62,4 +62,4 @@ public function walletBalanceByType(Model $user, string $type): float|int { return $user->getWalletBalanceByType($type); } -} +} \ No newline at end of file diff --git a/src/Traits/HandlesPayment.php b/src/Traits/HandlesPayment.php index 2f5d608..2266cb8 100644 --- a/src/Traits/HandlesPayment.php +++ b/src/Traits/HandlesPayment.php @@ -14,7 +14,7 @@ trait HandlesPayment * @param ?string $notes * * @return void - * @throws InsufficientBalanceException + * @throws \HPWebdeveloper\LaravelPayPocket\Exceptions\InsufficientBalanceException */ public function pay(int|float $orderValue, ?string $notes = null): void { @@ -46,4 +46,4 @@ public function pay(int|float $orderValue, ?string $notes = null): void } }); } -} +} \ No newline at end of file From 176e4c3b618422cb302991348e7e842c1945e18b Mon Sep 17 00:00:00 2001 From: 3m1n3nc3 Date: Sat, 13 Jan 2024 02:58:38 +0100 Subject: [PATCH 03/14] Type $walletsInOrder in HandlesPayment's pay() method --- src/Traits/HandlesPayment.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Traits/HandlesPayment.php b/src/Traits/HandlesPayment.php index 2266cb8..868e590 100644 --- a/src/Traits/HandlesPayment.php +++ b/src/Traits/HandlesPayment.php @@ -25,6 +25,10 @@ public function pay(int|float $orderValue, ?string $notes = null): void DB::transaction(function () use ($orderValue, $notes) { $remainingOrderValue = $orderValue; + /** + * @var \Illuminate\Support\Collection $walletsInOrder + */ $walletsInOrder = $this->wallets()->whereIn('type', $this->walletsInOrder())->get(); foreach ($walletsInOrder as $wallet) { @@ -46,4 +50,4 @@ public function pay(int|float $orderValue, ?string $notes = null): void } }); } -} \ No newline at end of file +} From 515337092954537805461fcbbb8fafac8c53fde5 Mon Sep 17 00:00:00 2001 From: 3m1n3nc3 Date: Sat, 13 Jan 2024 03:11:30 +0100 Subject: [PATCH 04/14] Fix $walletsInOrder type, see last commit. --- src/Traits/HandlesPayment.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Traits/HandlesPayment.php b/src/Traits/HandlesPayment.php index 868e590..d8d83ca 100644 --- a/src/Traits/HandlesPayment.php +++ b/src/Traits/HandlesPayment.php @@ -26,8 +26,7 @@ public function pay(int|float $orderValue, ?string $notes = null): void $remainingOrderValue = $orderValue; /** - * @var \Illuminate\Support\Collection $walletsInOrder + * @var \Illuminate\Support\Collection */ $walletsInOrder = $this->wallets()->whereIn('type', $this->walletsInOrder())->get(); From 3fd701af9a777d6b5e98048aaf4bdae15ed953e2 Mon Sep 17 00:00:00 2001 From: 3m1n3nc3 Date: Sat, 13 Jan 2024 04:34:48 +0100 Subject: [PATCH 05/14] Update README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index d344f8c..4f432e7 100644 --- a/README.md +++ b/README.md @@ -55,7 +55,7 @@ php artisan vendor:publish --tag="config" This command will automatically publish the `pay-pocket.php` config file and also `WalletEnums.php` file into your application's `config` and `app/Enums` directories respectively. -## Updating +## Updating to ^2.0.0 If updating to version `^2.0.0`, new migration and config files have been added to support the new [Transaction Notes Feature](#transaction-notes-8) From db2a16ffad68da48d90cf94aa093ee0a64c3d529 Mon Sep 17 00:00:00 2001 From: 3m1n3nc3 Date: Sat, 13 Jan 2024 04:35:24 +0100 Subject: [PATCH 06/14] Remove README changes --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 4f432e7..54e116b 100644 --- a/README.md +++ b/README.md @@ -55,7 +55,7 @@ php artisan vendor:publish --tag="config" This command will automatically publish the `pay-pocket.php` config file and also `WalletEnums.php` file into your application's `config` and `app/Enums` directories respectively. -## Updating to ^2.0.0 +## Updating If updating to version `^2.0.0`, new migration and config files have been added to support the new [Transaction Notes Feature](#transaction-notes-8) @@ -137,7 +137,7 @@ LaravelPayPocket::deposit($user, 'wallet_1', 123.45); Note: `wallet_1` and `wallet_2` must already be defined in the `WalletEnums`. -#### Transaction Notes ([#8][i8]) +#### Transaction Info ([#8][i8]) When you need to add descriptions for a specific transaction, the `$notes` parameter enables you to provide details explaining the reason behind the transaction. From beee8d755bd9db2a8963b622c507b126aaf2484b Mon Sep 17 00:00:00 2001 From: HPWebdeveloper Date: Sat, 13 Jan 2024 10:26:00 +0000 Subject: [PATCH 07/14] Fix styling --- src/Interfaces/WalletOperations.php | 20 ++------------------ src/Services/PocketServices.php | 25 +++---------------------- src/Traits/BalanceOperation.php | 18 +++--------------- src/Traits/HandlesDeposit.php | 12 +++--------- src/Traits/HandlesPayment.php | 4 +--- src/Traits/HasWallet.php | 12 ------------ tests/Models/User.php | 2 +- 7 files changed, 13 insertions(+), 80 deletions(-) diff --git a/src/Interfaces/WalletOperations.php b/src/Interfaces/WalletOperations.php index c41dfce..cdffed0 100644 --- a/src/Interfaces/WalletOperations.php +++ b/src/Interfaces/WalletOperations.php @@ -6,35 +6,23 @@ interface WalletOperations { /** * Get User's Wallet Balance - * - * @return int|float */ public function getWalletBalanceAttribute(): int|float; /** * Get the balance of a specific wallet type. - * - * - * @param string $walletType - * - * @return float|int */ public function getWalletBalanceByType(string $walletType): float|int; /** * Check if User's wallet balance is more than given value - * - * @param int|float $value - * - * @return bool */ public function hasSufficientBalance(int|float $value): bool; /** * Pay the order value from the user's wallets. * - * @param int|float $orderValue - * @param ?string $notes + * @param ?string $notes * * @throws \HPWebdeveloper\LaravelPayPocket\Exceptions\InsufficientBalanceException */ @@ -43,11 +31,7 @@ public function pay(int|float $orderValue, ?string $notes = null): void; /** * Deposit an amount to the user's wallet of a specific type. * - * @param string $type - * @param int|float $amount - * @param ?string $notes - * - * @return bool + * @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 e5c836d..1ee674d 100644 --- a/src/Services/PocketServices.php +++ b/src/Services/PocketServices.php @@ -9,12 +9,7 @@ class PocketServices /** * Deposit an amount to the user's wallet of a specific type. * - * @param \Illuminate\Database\Eloquent\Model $user - * @param string $type - * @param int|float $amount - * @param ?string $notes - * - * @return bool + * @param ?string $notes */ public function deposit(Model $user, string $type, int|float $amount, ?string $notes = null): bool { @@ -24,11 +19,8 @@ public function deposit(Model $user, string $type, int|float $amount, ?string $n /** * Pay the order value from the user's wallets. * - * @param \Illuminate\Database\Eloquent\Model $user - * @param int|float $orderValue - * @param ?string $notes + * @param ?string $notes * - * @return void * @throws \HPWebdeveloper\LaravelPayPocket\Exceptions\InsufficientBalanceException */ public function pay(Model $user, int|float $orderValue, ?string $notes = null): void @@ -38,11 +30,6 @@ public function pay(Model $user, int|float $orderValue, ?string $notes = null): /** * Get the balance of the user. - * - * - * @param \Illuminate\Database\Eloquent\Model $user - * - * @return float|int */ public function checkBalance(Model $user): int|float { @@ -51,15 +38,9 @@ public function checkBalance(Model $user): int|float /** * Get the balance of a specific wallet type. - * - * - * @param \Illuminate\Database\Eloquent\Model $user - * @param string $type - * - * @return float|int */ public function walletBalanceByType(Model $user, string $type): float|int { return $user->getWalletBalanceByType($type); } -} \ No newline at end of file +} diff --git a/src/Traits/BalanceOperation.php b/src/Traits/BalanceOperation.php index 95f8041..a559693 100644 --- a/src/Traits/BalanceOperation.php +++ b/src/Traits/BalanceOperation.php @@ -10,8 +10,6 @@ trait BalanceOperation /** * Check if Balance is more than zero. - * - * @return bool */ public function hasBalance(): bool { @@ -21,10 +19,7 @@ public function hasBalance(): bool /** * Decrement Balance and create a log entry. * - * @param int|float $value - * @param ?string $notes - * - * @return void + * @param ?string $notes */ public function decrementAndCreateLog(int|float $value, ?string $notes = null): void { @@ -35,10 +30,7 @@ public function decrementAndCreateLog(int|float $value, ?string $notes = null): /** * Increment Balance and create a log entry. * - * @param int|float $value - * @param ?string $notes - * - * @return void + * @param ?string $notes */ public function incrementAndCreateLog(int|float $value, ?string $notes = null): void { @@ -49,11 +41,7 @@ public function incrementAndCreateLog(int|float $value, ?string $notes = null): /** * Create a new log record * - * @param string $logType - * @param int|float $value - * @param ?string $notes - * - * @return void + * @param ?string $notes */ protected function createLog(string $logType, int|float $value, ?string $notes = null): void { diff --git a/src/Traits/HandlesDeposit.php b/src/Traits/HandlesDeposit.php index ba5f683..2d8318a 100644 --- a/src/Traits/HandlesDeposit.php +++ b/src/Traits/HandlesDeposit.php @@ -15,11 +15,8 @@ trait HandlesDeposit /** * Deposit an amount to the user's wallet of a specific type. * - * @param string $type - * @param int|float $amount - * @param ?string $notes + * @param ?string $notes * - * @return bool * @throws InvalidDepositException|InvalidValueException|InvalidWalletTypeException */ public function deposit(string $type, int|float $amount, ?string $notes = null): bool @@ -45,8 +42,6 @@ public function deposit(string $type, int|float $amount, ?string $notes = null): /** * Get depositable types from WalletEnums. - * - * @return array */ private function getDepositableTypes(): array { @@ -61,10 +56,9 @@ private function getDepositableTypes(): array /** * Check if the given type is valid. * - * @var string $type - * @var array $depositable + * @var string + * @var array * - * @return bool * @throws InvalidWalletTypeException */ private function isRequestValid($type, array $depositable): bool diff --git a/src/Traits/HandlesPayment.php b/src/Traits/HandlesPayment.php index d8d83ca..e40ec19 100644 --- a/src/Traits/HandlesPayment.php +++ b/src/Traits/HandlesPayment.php @@ -10,10 +10,8 @@ trait HandlesPayment /** * Pay the order value from the user's wallets. * - * @param int|float $orderValue - * @param ?string $notes + * @param ?string $notes * - * @return void * @throws \HPWebdeveloper\LaravelPayPocket\Exceptions\InsufficientBalanceException */ public function pay(int|float $orderValue, ?string $notes = null): void diff --git a/src/Traits/HasWallet.php b/src/Traits/HasWallet.php index 8f945e8..6748dfa 100644 --- a/src/Traits/HasWallet.php +++ b/src/Traits/HasWallet.php @@ -23,8 +23,6 @@ public function wallets() /** * Get User's Wallet Balance - * - * @return int|float */ public function getWalletBalanceAttribute(): int|float { @@ -45,24 +43,14 @@ public function getWalletBalanceAttribute(): int|float /** * Check if User's wallet balance is more than given value - * - * @param int|float $value - * - * @return bool */ public function hasSufficientBalance(int|float $value): bool { return (int) $this->walletBalance >= (int) $value; } - /** * Get the balance of a specific wallet type. - * - * - * @param string $walletType - * - * @return float|int */ public function getWalletBalanceByType(string $walletType): float|int { diff --git a/tests/Models/User.php b/tests/Models/User.php index c9a560a..5c07e35 100644 --- a/tests/Models/User.php +++ b/tests/Models/User.php @@ -12,8 +12,8 @@ class User extends Authenticatable implements WalletOperations { use HasFactory; - use Notifiable; use ManagesWallet; + use Notifiable; /** * The attributes that are mass assignable. From b29b8f517b0286f2f47ec63cc25fa966451ad9d8 Mon Sep 17 00:00:00 2001 From: Sajjad Esmaeeli Date: Sat, 13 Jan 2024 16:57:23 +0100 Subject: [PATCH 08/14] 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 09/14] 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 10/14] 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 11/14] 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 12/14] 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 13/14] 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 14/14] 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() ); }