Skip to content

Commit

Permalink
Merge pull request #8 from flavorly/next
Browse files Browse the repository at this point in the history
  • Loading branch information
nikuscs authored May 25, 2024
2 parents c66c28f + badc49c commit 67b2348
Show file tree
Hide file tree
Showing 11 changed files with 142 additions and 100 deletions.
31 changes: 14 additions & 17 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,22 @@
}
],
"require": {
"php": "^8.2",
"brick/math": "^0.11.0",
"brick/money": "^0.8.0",
"illuminate/contracts": "^11.0",
"spatie/laravel-data": "^4.5",
"spatie/laravel-package-tools": "^1.14.0"
"php": "^8.3",
"brick/math": "^0.12.1",
"brick/money": "^0.9.0",
"illuminate/contracts": "*",
"spatie/laravel-data": "^4.6",
"spatie/laravel-package-tools": "^1.16"
},
"require-dev": {
"laravel/pint": "^1.0",
"nunomaduro/collision": "^7.9",
"nunomaduro/larastan": "^2.0.1",
"orchestra/testbench": "^8.0",
"pestphp/pest": "^2.0",
"pestphp/pest-plugin-arch": "^2.0",
"pestphp/pest-plugin-laravel": "^2.0",
"phpstan/extension-installer": "^1.1",
"phpstan/phpstan-deprecation-rules": "^1.0",
"phpstan/phpstan-phpunit": "^1.0",
"spatie/laravel-ray": "^1.26"
"larastan/larastan": "^v2.9.6",
"laravel/pint": "^1.15",
"nunomaduro/collision": "^7.10",
"orchestra/testbench": "^8.22",
"pestphp/pest": "^2.34",
"pestphp/pest-plugin-arch": "^2.7",
"pestphp/pest-plugin-laravel": "^2.4",
"spatie/laravel-ray": "^1.36"
},
"autoload": {
"psr-4": {
Expand Down
16 changes: 10 additions & 6 deletions src/CacheService.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,18 +90,22 @@ public function hasCache(): bool

/**
* Get the current Balance in cache
*
* @throws WalletDatabaseTransactionException
*/
public function balance(): float|int|string
{
return Cache::tags($this->tags())->get($this->prefix());
$balance = Cache::tags($this->tags())->get($this->prefix());
if (is_string($balance) || is_int($balance) || is_float($balance)) {
return $balance;
}
throw new WalletDatabaseTransactionException('Invalid balance type');
}

/**
* Get the lock instance but without blocking ( yet )
*
* @param int|null $lockFor
*/
public function lock(null|int $lockFor = null): Lock
public function lock(?int $lockFor = null): Lock
{
return Cache::lock(
$this->blockPrefix(),
Expand Down Expand Up @@ -177,7 +181,7 @@ public function locked(): bool
/** @var CacheManager $lockConnection */
$lockConnection = $store->lockConnection();

return null !== $lockConnection->get(Cache::getPrefix().$this->blockPrefix());
return $lockConnection->get(Cache::getPrefix().$this->blockPrefix()) !== null;
}

/**
Expand All @@ -192,7 +196,7 @@ public function isWithin(): bool
* Put some value in the cache
* In this case only for balance but could be used for another scenario in the future
*/
public function put(float|int|string $balance): void
public function put(mixed $balance): void
{
Cache::tags($this->tags())->put(
$this->prefix(),
Expand Down
19 changes: 0 additions & 19 deletions src/Commands/WalletCommand.php

This file was deleted.

4 changes: 3 additions & 1 deletion src/Concerns/EvaluatesClosures.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ trait EvaluatesClosures
{
/**
* Stolen from Filament, evaluate the closure with given params, and exclude some.
*
* @param array<string,mixed> $parameters
*/
protected function evaluate($value, array $parameters = []): mixed
protected function evaluate(mixed $value, array $parameters = []): mixed
{
if ($value instanceof Closure) {
return app()->call(
Expand Down
6 changes: 2 additions & 4 deletions src/Concerns/HasWallet.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,11 @@ public function getBalanceWithoutCacheAttribute(): string
/**
* Alias for Credit
*
* @param string|null $endpoint
*
* @throws WalletLockedException
* @throws \Throwable
*/
public function credit(float|int|string $amount, array $meta = [], null|string $endpoint = null, bool $throw = false): string
public function credit(float|int|string $amount, array $meta = [], ?string $endpoint = null, bool $throw = false): string
{
return $this->wallet()->credit(
amount: $amount,
Expand All @@ -86,12 +85,11 @@ public function credit(float|int|string $amount, array $meta = [], null|string $
/**
* Alias for debit
*
* @param string|null $endpoint
*
* @throws WalletLockedException
* @throws \Throwable
*/
public function debit(float|int|string $amount, array $meta = [], null|string $endpoint = null, bool $throw = false): string
public function debit(float|int|string $amount, array $meta = [], ?string $endpoint = null, bool $throw = false): string
{
return $this->wallet()->debit(
amount: $amount,
Expand Down
28 changes: 23 additions & 5 deletions src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@

use Brick\Money\Currency;
use Flavorly\Wallet\Contracts\WalletContract;
use InvalidArgumentException;

/**
* Ensures the configuration is bootstrapped and available to the wallet.
* We pass the current model to the configuration class
* So we can grab an additional configuration from the model
*/
final class Configuration
final readonly class Configuration
{
public function __construct(
private readonly WalletContract $model,
private WalletContract $model,
) {
}

Expand All @@ -22,21 +23,35 @@ public function __construct(
*/
public function getDecimals(): int
{
return $this->model->getAttribute(config('laravel-wallet.columns.decimals', 'wallet_decimals')) ?? 10;
//@phpstan-ignore-next-line
$decimals = $this->model->getAttribute(config('laravel-wallet.columns.decimals', 'wallet_decimals')) ?? 10;
if (is_string($decimals)) {
return (int) $decimals;
}

if (is_int($decimals) || is_float($decimals)) {
return (int) $decimals;
}

return 10;
}

/**
* Get the model primary key
*/
public function getPrimaryKey(): string
{
return $this->model->getKey();
if (! is_string($this->model->getKey()) && ! is_int($this->model->getKey())) {
throw new InvalidArgumentException('Primary key must be a string or an integer');
}

return (string) $this->model->getKey();
}

/**
* Get the Database/Model Balance Attribute
*/
public function getBalance(): float|int|string
public function getBalance(): mixed
{
return $this->model->getAttribute($this->getBalanceColumn());
}
Expand All @@ -46,6 +61,7 @@ public function getBalance(): float|int|string
*/
public function getMaximumCredit(): float|int|string
{
//@phpstan-ignore-next-line
return $this->model->getAttribute(config('laravel-wallet.columns.credit', 'wallet_credit')) ?? 0;
}

Expand All @@ -54,6 +70,7 @@ public function getMaximumCredit(): float|int|string
*/
public function getCurrency(): string
{
//@phpstan-ignore-next-line
return $this->model->getAttribute(config('laravel-wallet.columns.currency', 'wallet_currency')) ?? config('laravel-wallet.currency', 'USD');
}

Expand All @@ -62,6 +79,7 @@ public function getCurrency(): string
*/
public function getBalanceColumn(): string
{
//@phpstan-ignore-next-line
return config('laravel-wallet.columns.balance', 'wallet_balance');
}

Expand Down
39 changes: 35 additions & 4 deletions src/Contracts/WalletContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Flavorly\Wallet\Contracts;

use Brick\Money\Money;
use Flavorly\Wallet\Models\Transaction;
use Flavorly\Wallet\Wallet;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphMany;
Expand All @@ -12,19 +13,49 @@
*/
interface WalletContract
{
/**
* Returns the wallet instance
*/
public function wallet(): Wallet;

/**
* Returns all transactions
*
* @return MorphMany<Transaction>
*/
public function transactions(): MorphMany;

/**
* Gets the balance attribute cached
*/
public function getBalanceAttribute(): string;

/**
* Gets the balance attribute without cache
*/
public function getBalanceWithoutCacheAttribute(): string;

public function credit(float|int|string $amount, array $meta = [], null|string $endpoint = null, bool $throw = false): string;

public function debit(float|int|string $amount, array $meta = [], null|string $endpoint = null, bool $throw = false): string;

/**
* Credits the user or model with the given amount
*
* @param array<string,mixed> $meta
*/
public function credit(float|int|string $amount, array $meta = [], ?string $endpoint = null, bool $throw = false): string;

/**
* Debits the user or model with the given amount
*
* @param array<string,mixed> $meta
*/
public function debit(float|int|string $amount, array $meta = [], ?string $endpoint = null, bool $throw = false): string;

/**
* Checks if the user has balance for the given amount
*/
public function hasBalanceFor(float|int|string $amount): bool;

/**
* Get the balance formatted as money Value
*/
public function getBalanceAsMoneyAttribute(): Money;
}
10 changes: 5 additions & 5 deletions src/Math.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
* Please see Brick\Money for more information about the concept
* on why we use Integers instead of floats
*/
final class Math
final readonly class Math
{
public function __construct(
protected readonly int $floatScale,
protected readonly int $integerScale = 20,
protected int $floatScale,
protected int $integerScale = 20,
) {
}

Expand Down Expand Up @@ -224,7 +224,7 @@ public function round(float|int|string $number, int $precision = 0): string
* @throws MathException
* @throws RoundingNecessaryException
*/
public function abs(float|int|string $number): string
public function abs(float|int|string $number, ?int $scale = null): string
{
return (string) BigDecimal::of($number)->abs()->toScale($scale ?? $this->floatScale, RoundingMode::DOWN);
}
Expand All @@ -233,7 +233,7 @@ public function abs(float|int|string $number): string
* @throws MathException
* @throws RoundingNecessaryException
*/
public function negative(float|int|string $number): string
public function negative(float|int|string $number, ?int $scale = null): string
{
$number = BigDecimal::of($number);
if ($number->isNegative()) {
Expand Down
21 changes: 13 additions & 8 deletions src/Models/Transaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@

class Transaction extends Model
{
/**
* @var string[]
*/
protected $fillable = [
'transactionable_type',
'transactionable_id',
Expand All @@ -23,14 +20,22 @@ class Transaction extends Model
];

/**
* @var array<string, string>
* @return MorphTo<Model, Transaction>
*/
protected $casts = [
'meta' => 'array',
];

public function transactionable(): MorphTo
{
return $this->morphTo();
}

/**
* Get the attributes that should be cast.
*
* @return array<string, string>
*/
protected function casts(): array
{
return [
'meta' => 'array',
];
}
}
Loading

0 comments on commit 67b2348

Please sign in to comment.