Skip to content

Commit

Permalink
fix problem of type hint + refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
SSEsmaeeli committed Jan 5, 2024
1 parent 208efc8 commit 7733a02
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/Facades/LaravelPayPocket.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*/
class LaravelPayPocket extends Facade
{
protected static function getFacadeAccessor()
protected static function getFacadeAccessor(): string
{
return \HPWebdeveloper\LaravelPayPocket\Services\PocketServices::class;
}
Expand Down
6 changes: 4 additions & 2 deletions src/Interfaces/WalletOperations.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ interface WalletOperations
{
public function getWalletBalanceAttribute();

public function getWalletBalanceByType(string $walletType);
public function getWalletBalanceByType(string $walletType): int|float;

public function hasSufficientBalance($value): bool;

public function pay(int|float $orderValue);
public function pay(int|float $orderValue): void;

public function deposit(string $type, int|float $amount): bool;

public function getWalletBalance(): int|float;
}
12 changes: 6 additions & 6 deletions src/Services/PocketServices.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,26 @@

namespace HPWebdeveloper\LaravelPayPocket\Services;

use Illuminate\Database\Eloquent\Model;
use HPWebdeveloper\LaravelPayPocket\Interfaces\WalletOperations;

class PocketServices
{
public function deposit(Model $user, string $type, int|float $amount): bool
public function deposit(WalletOperations $user, string $type, int|float $amount): bool
{
return $user->deposit($type, $amount);
}

public function pay(Model $user, int|float $orderValue): void
public function pay(WalletOperations $user, int|float $orderValue): void
{
$user->pay($orderValue);
}

public function checkBalance(Model $user): int|float
public function checkBalance(WalletOperations $user): int|float
{
return $user->walletBalance;
return $user->getWalletBalance();
}

public function walletBalanceByType(Model $user, string $type): int|float
public function walletBalanceByType(WalletOperations $user, string $type): int|float
{
return $user->getWalletBalanceByType($type);
}
Expand Down
6 changes: 2 additions & 4 deletions src/Traits/GetWallets.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
);
}
Expand Down
14 changes: 10 additions & 4 deletions src/Traits/HasWallet.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ public function wallets()
*/
public function getWalletBalanceAttribute()
{

$totalBalance = 0;

foreach ($this->walletsInOrder() as $walletInOrder) {
Expand All @@ -39,7 +38,6 @@ public function getWalletBalanceAttribute()
}

return $totalBalance;

}

/**
Expand All @@ -53,9 +51,12 @@ public function hasSufficientBalance($value): bool
/**
* Get the balance of a specific wallet type.
*
* @return float|int
* @param string $walletType
* @return int|float
* @throws InvalidWalletTypeException
* @throws WalletNotFoundException
*/
public function getWalletBalanceByType(string $walletType)
public function getWalletBalanceByType(string $walletType): int|float
{
if (! WalletEnums::isValid($walletType)) {
throw new InvalidWalletTypeException("Invalid wallet type '{$walletType}'.");
Expand All @@ -69,4 +70,9 @@ public function getWalletBalanceByType(string $walletType)

return $wallet->balance;
}

public function getWalletBalance(): int|float
{
return $this->walletBalance;
}
}

0 comments on commit 7733a02

Please sign in to comment.