Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix styling & improvements #11

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions src/Exceptions/InsufficientBalanceException.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
9 changes: 2 additions & 7 deletions src/Exceptions/InvalidDepositException.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
9 changes: 2 additions & 7 deletions src/Exceptions/InvalidValueException.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
3 changes: 2 additions & 1 deletion src/Exceptions/InvalidWalletTypeException.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
3 changes: 2 additions & 1 deletion src/Exceptions/WalletNotFoundException.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
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;
}
14 changes: 8 additions & 6 deletions src/Services/PocketServices.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,26 @@

namespace HPWebdeveloper\LaravelPayPocket\Services;

use HPWebdeveloper\LaravelPayPocket\Interfaces\WalletOperations;

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

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

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

public function walletBalanceByType($user, $type)
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
10 changes: 6 additions & 4 deletions src/Traits/HandlesDeposit.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ 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
{
Expand Down Expand Up @@ -50,12 +53,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.');
Expand Down
12 changes: 8 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,10 @@ public function hasSufficientBalance($value): bool
/**
* Get the balance of a specific wallet type.
*
* @return float|int
* @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 +68,9 @@ public function getWalletBalanceByType(string $walletType)

return $wallet->balance;
}

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