Skip to content

Commit

Permalink
Merge pull request #2 from SSEsmaeeli/main
Browse files Browse the repository at this point in the history
Optimize wallet retrieval to single query, configure GH actions for PHP 8.3 testing, fix PHPStan issues.
  • Loading branch information
HPWebdeveloper authored Dec 12, 2023
2 parents 4200ef4 + 2b091e8 commit 908da3d
Show file tree
Hide file tree
Showing 8 changed files with 10 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
fail-fast: true
matrix:
os: [ubuntu-latest, windows-latest]
php: [8.2, 8.1]
php: [8.3, 8.2, 8.1]
laravel: [10.*]
stability: [prefer-lowest, prefer-stable]
include:
Expand Down
2 changes: 1 addition & 1 deletion src/Exceptions/InsufficientBalanceException.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class InsufficientBalanceException extends 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($message = 'Insufficient balance to cover the order', $code = 0, ?\Throwable $previous = null)
{
parent::__construct($message, $code, $previous);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Exceptions/InvalidDepositException.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class InvalidDepositException extends Exception
* @param string $message
* @param int $code
*/
public function __construct($message = 'Invalid deposit operation', $code = 0, \Throwable $previous = null)
public function __construct($message = 'Invalid deposit operation', $code = 0, ?\Throwable $previous = null)
{
parent::__construct($message, $code, $previous);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Exceptions/InvalidValueException.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class InvalidValueException extends Exception
* @param string $message
* @param int $code
*/
public function __construct($message = 'Invalie value to deposit', $code = 0, \Throwable $previous = null)
public function __construct($message = 'Invalie value to deposit', $code = 0, ?\Throwable $previous = null)
{
parent::__construct($message, $code, $previous);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Exceptions/InvalidWalletTypeException.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class InvalidWalletTypeException extends Exception
{
public function __construct($message = 'Invalid wallet type', $code = 0, \Throwable $previous = null)
public function __construct($message = 'Invalid wallet type', $code = 0, ?\Throwable $previous = null)
{
parent::__construct($message, $code, $previous);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Exceptions/WalletNotFoundException.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class WalletNotFoundException extends Exception
{
public function __construct($message = 'Wallet not found', $code = 0, \Throwable $previous = null)
public function __construct($message = 'Wallet not found', $code = 0, ?\Throwable $previous = null)
{
parent::__construct($message, $code, $previous);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Models/Wallet.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* HPWebdeveloper\LaravelPayPocket\Models\Wallet
*
* @property mixed $balance
* @property HPWebdeveloper\LaravelPayPocket\Enums\WalletEnums $type
* @property WalletEnums $type
*/
class Wallet extends Model
{
Expand Down
9 changes: 3 additions & 6 deletions src/Traits/HandlesPayment.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace HPWebdeveloper\LaravelPayPocket\Traits;

use App\Enums\WalletEnums;
use HPWebdeveloper\LaravelPayPocket\Exceptions\InsufficientBalanceException;
use Illuminate\Support\Facades\DB;

Expand All @@ -11,11 +10,10 @@ trait HandlesPayment
/**
* Pay the order value from the user's wallets.
*
* @return void
*
* @throws InsufficientBalanceException
*/
public function pay(int|float $orderValue)
public function pay(int|float $orderValue): void
{
if (! $this->hasSufficientBalance($orderValue)) {
throw new InsufficientBalanceException('Insufficient balance to cover the order.');
Expand All @@ -24,10 +22,9 @@ public function pay(int|float $orderValue)
DB::transaction(function () use ($orderValue) {
$remainingOrderValue = $orderValue;

foreach ($this->walletsInOrder() as $walletInOrder) {
$walletEnumType = WalletEnums::tryFrom($walletInOrder);
$wallet = $this->wallets()->where('type', $walletEnumType)->first();
$walletsInOrder = $this->wallets()->whereIn('type', $this->walletsInOrder())->get();

foreach ($walletsInOrder as $wallet) {
if (! $wallet || ! $wallet->hasBalance()) {
continue;
}
Expand Down

0 comments on commit 908da3d

Please sign in to comment.