Skip to content

Commit

Permalink
Chore
Browse files Browse the repository at this point in the history
  • Loading branch information
MannikJ committed Jul 16, 2024
1 parent 05f268a commit e44945d
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 17 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
},
"scripts": {
"test": "vendor/bin/phpunit tests --colors=always --testdox",
"static": "vendor/bin/phpstan analyse",
"post-update-cmd": [
"vendor/bin/phpstan analyse",
"composer test"
Expand Down
2 changes: 1 addition & 1 deletion src/Models/Transaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public function getAmountAttribute()
return $this->getAmountWithSign();
}

public function setAmountAttribute($amount)
public function setAmountAttribute(int|float $amount)
{
if ($this->shouldConvertToAbsoluteAmount()) {
$amount = abs($amount);
Expand Down
44 changes: 31 additions & 13 deletions src/Models/Wallet.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,23 @@ public function owner(): MorphTo
*
* @param int $amount
* @return MannikJ\Laravel\Wallet\Models\Transaction
*
* @throws UnacceptedTransactionException
*/
public function deposit(float $amount, array $meta = [], string $type = 'deposit', bool $forceFail = false): Transaction
{
public function deposit(
float $amount,
array $meta = [],
string $type = 'deposit',
bool $forceFail = false
): Transaction {
$accepted = $amount >= 0
&& !$forceFail ? true : false;

if (!$this->exists) {
$this->save();
}

/** @var Transaction */
$transaction = $this->transactions()
->create([
'amount' => $amount,
Expand All @@ -90,12 +97,14 @@ public function deposit(float $amount, array $meta = [], string $type = 'deposit

/**
* Fail to move credits to this account.
*
* @param float $amount
* @return MannikJ\Laravel\Wallet\Models\Transaction
*
* @throws UnacceptedTransactionException
*/
public function failDeposit(float $amount, array $meta = [], string $type = 'deposit'): Transaction
{
public function failDeposit(
float $amount,
array $meta = [],
string $type = 'deposit'
) {
return $this->deposit($amount, $meta, $type, true);
}

Expand All @@ -105,8 +114,12 @@ public function failDeposit(float $amount, array $meta = [], string $type = 'dep
* @param float $amount Only the absolute value will be considered
* @return MannikJ\Laravel\Wallet\Models\Transaction
*/
public function withdraw(float $amount, array $meta = [], string $type = 'withdraw', bool $guarded = true)
{
public function withdraw(
float $amount,
array $meta = [],
string $type = 'withdraw',
bool $guarded = true
) {
$accepted = $guarded
? $this->canWithdraw($amount)
: true;
Expand Down Expand Up @@ -137,8 +150,11 @@ public function withdraw(float $amount, array $meta = [], string $type = 'withdr
*
* @param float $amount
*/
public function forceWithdraw(int|float $amount, array $meta = [], string $type = 'withdraw')
{
public function forceWithdraw(
int|float $amount,
array $meta = [],
string $type = 'withdraw'
) {
return $this->withdraw($amount, $meta, $type, false);
}

Expand All @@ -163,8 +179,10 @@ public function canWithdraw(float $amount = null)
* @param string $comment
* @return MannikJ\Laravel\Wallet\Models\Transaction
*/
public function setBalance(float $amount, string $comment = 'Manual offset transaction')
{
public function setBalance(
float $amount,
string $comment = 'Manual offset transaction'
) {
$actualBalance = $this->actualBalance();
$difference = $amount - $actualBalance;

Expand Down
9 changes: 6 additions & 3 deletions src/Traits/HasWallet.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace MannikJ\Laravel\Wallet\Traits;

use Illuminate\Database\Eloquent\Relations\HasManyThrough;
use Illuminate\Database\Eloquent\Relations\MorphOne;

// use MannikJ\Laravel\Wallet\Models\Transaction;
// use MannikJ\Laravel\Wallet\Models\Wallet;

Expand All @@ -10,23 +13,23 @@ trait HasWallet
/**
* Retrieve the balance of this user's wallet
*/
public function getBalanceAttribute()
public function getBalanceAttribute(): int|float
{
return $this->wallet->refresh()->balance;
}

/**
* Retrieve the wallet of this user
*/
public function wallet()
public function wallet(): MorphOne
{
return $this->morphOne(config('wallet.wallet_model'), 'owner')->withDefault();
}

/**
* Retrieve all transactions of this user
*/
public function walletTransactions()
public function walletTransactions(): HasManyThrough
{
return $this->hasManyThrough(
config('wallet.transaction_model'),
Expand Down

0 comments on commit e44945d

Please sign in to comment.