From 2501af7e4a2f08c5bcbf4b99d80f0d4a46678fbb Mon Sep 17 00:00:00 2001 From: Sajjad Esmaeeli Date: Thu, 4 Jan 2024 18:09:25 +0100 Subject: [PATCH] refactor: return types - type hints - possible exceptions --- 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/Services/PocketServices.php | 12 +++++++----- src/Traits/HandlesDeposit.php | 9 +++++---- 7 files changed, 22 insertions(+), 32 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..b8f61cc 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 = 'Invalid 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/Services/PocketServices.php b/src/Services/PocketServices.php index 1fe31cc..a52fa0d 100644 --- a/src/Services/PocketServices.php +++ b/src/Services/PocketServices.php @@ -2,24 +2,26 @@ namespace HPWebdeveloper\LaravelPayPocket\Services; +use Illuminate\Database\Eloquent\Model; + class PocketServices { - public function deposit($user, $type, $amount) + public function deposit(Model $user, string $type, int|float $amount): bool { return $user->deposit($type, $amount); } - public function pay($user, $orderValue) + public function pay(Model $user, int|float $orderValue): void { - return $user->pay($orderValue); + $user->pay($orderValue); } - public function checkBalance($user) + public function checkBalance(Model $user): int|float { return $user->walletBalance; } - public function walletBalanceByType($user, $type) + public function walletBalanceByType(Model $user, string $type): int|float { return $user->getWalletBalanceByType($type); } diff --git a/src/Traits/HandlesDeposit.php b/src/Traits/HandlesDeposit.php index 108c1ac..5657817 100644 --- a/src/Traits/HandlesDeposit.php +++ b/src/Traits/HandlesDeposit.php @@ -14,6 +14,8 @@ trait HandlesDeposit { /** * Deposit an amount to the user's wallet of a specific type. + * @throws InvalidDepositException + * @throws InvalidValueException|InvalidWalletTypeException */ public function deposit(string $type, int|float $amount): bool { @@ -50,12 +52,11 @@ private function getDepositableTypes(): array } /** - * Check if the given tyep is valid. + * Check if the given type is valid. * - * @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.');