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

Improve type annotations and PHPDocs. #13

Closed
wants to merge 7 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
5 changes: 5 additions & 0 deletions src/Facades/LaravelPayPocket.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
45 changes: 41 additions & 4 deletions src/Interfaces/WalletOperations.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 \HPWebdeveloper\LaravelPayPocket\Exceptions\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;
}
51 changes: 45 additions & 6 deletions src/Services/PocketServices.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,64 @@

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);

Check failure on line 21 in src/Services/PocketServices.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to an undefined method Illuminate\Database\Eloquent\Model::deposit().
}

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 \HPWebdeveloper\LaravelPayPocket\Exceptions\InsufficientBalanceException
*/
public function pay(Model $user, int|float $orderValue, ?string $notes = null): void
{
return $user->pay($orderValue, $notes);
$user->pay($orderValue, $notes);

Check failure on line 36 in src/Services/PocketServices.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to an undefined method Illuminate\Database\Eloquent\Model::pay().
}

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;

Check failure on line 49 in src/Services/PocketServices.php

View workflow job for this annotation

GitHub Actions / phpstan

Access to an undefined property Illuminate\Database\Eloquent\Model::$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);

Check failure on line 63 in src/Services/PocketServices.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to an undefined method Illuminate\Database\Eloquent\Model::getWalletBalanceByType().
}
}
}
36 changes: 29 additions & 7 deletions src/Traits/BalanceOperation.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,53 @@ trait BalanceOperation
protected $createdLog;

/**
* Check if Balance is more than zero.
* Check if Balance is more than zero.
*
* @return bool
*/
public function hasBalance(): bool
{
return $this->balance > 0;
}

/**
* 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;

Expand All @@ -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] ?? [])
Expand Down
18 changes: 15 additions & 3 deletions src/Traits/HandlesDeposit.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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
{
Expand All @@ -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.');
Expand Down
8 changes: 7 additions & 1 deletion src/Traits/HandlesPayment.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ trait HandlesPayment
/**
* Pay the order value from the user's wallets.
*
* @param int|float $orderValue
* @param ?string $notes
*
* @throws InsufficientBalanceException
* @return void
* @throws \HPWebdeveloper\LaravelPayPocket\Exceptions\InsufficientBalanceException
*/
public function pay(int|float $orderValue, ?string $notes = null): void
{
Expand All @@ -22,6 +25,9 @@ public function pay(int|float $orderValue, ?string $notes = null): void
DB::transaction(function () use ($orderValue, $notes) {
$remainingOrderValue = $orderValue;

/**
* @var \Illuminate\Support\Collection<TKey, \HPWebdeveloper\LaravelPayPocket\Models\Wallet>
*/
$walletsInOrder = $this->wallets()->whereIn('type', $this->walletsInOrder())->get();

foreach ($walletsInOrder as $wallet) {
Expand Down
19 changes: 14 additions & 5 deletions src/Traits/HasWallet.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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}'.");
Expand Down
3 changes: 2 additions & 1 deletion tests/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@

class User extends Authenticatable implements WalletOperations
{
use HasFactory, Notifiable;
use HasFactory;
use Notifiable;
use ManagesWallet;

/**
Expand Down
Loading