Skip to content

Commit ebdb041

Browse files
committed
refactor code
1 parent 3865c76 commit ebdb041

File tree

5 files changed

+19
-19
lines changed

5 files changed

+19
-19
lines changed

src/Models/Wallet.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public function setNameAttribute(string $name): void
7575
*/
7676
public function calculateBalance(): bool
7777
{
78-
$balance = $this->holder->transactions()
78+
$balance = $this->transactions()
7979
->where('wallet_id', $this->getKey())
8080
->where('confirmed', true)
8181
->sum('amount');

src/Tax.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class Tax
1818
public static function fee(Wallet $wallet, int $amount): int
1919
{
2020
if ($wallet instanceof Taxing) {
21-
return (int) ($amount * $wallet->getFeePercent() / 100);
21+
return (int)($amount * $wallet->getFeePercent() / 100);
2222
}
2323

2424
return 0;

src/Traits/CanBePaid.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public function refund(Product $product, bool $force = false): bool
102102
->setModel($this->transfers()->getMorphClass());
103103
}
104104

105-
return DB::transaction(function() use ($product, $transfer, $force) {
105+
return DB::transaction(function () use ($product, $transfer, $force) {
106106
if ($force) {
107107
$product->forceTransfer($this, $transfer->deposit->amount, $product->getMetaProduct());
108108
} else {

src/Traits/HasWallet.php

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
use Bavix\Wallet\Exceptions\AmountInvalid;
66
use Bavix\Wallet\Exceptions\BalanceIsEmpty;
77
use Bavix\Wallet\Exceptions\InsufficientFunds;
8-
use Bavix\Wallet\Tax;
98
use Bavix\Wallet\Interfaces\Wallet;
10-
use Bavix\Wallet\Models\Wallet as WalletModel;
119
use Bavix\Wallet\Models\Transaction;
1210
use Bavix\Wallet\Models\Transfer;
11+
use Bavix\Wallet\Models\Wallet as WalletModel;
12+
use Bavix\Wallet\Tax;
1313
use Bavix\Wallet\WalletProxy;
1414
use Illuminate\Database\Eloquent\Model;
1515
use Illuminate\Database\Eloquent\Relations\MorphMany;
@@ -117,7 +117,7 @@ public function canWithdraw(int $amount): bool
117117
*/
118118
public function transfer(Wallet $wallet, int $amount, ?array $meta = null): Transfer
119119
{
120-
return DB::transaction(function() use ($amount, $wallet, $meta) {
120+
return DB::transaction(function () use ($amount, $wallet, $meta) {
121121
$fee = Tax::fee($wallet, $amount);
122122
$withdraw = $this->withdraw($amount + $fee, $meta);
123123
$deposit = $wallet->deposit($amount, $meta);
@@ -153,7 +153,7 @@ public function safeTransfer(Wallet $wallet, int $amount, ?array $meta = null):
153153
*/
154154
public function forceTransfer(Wallet $wallet, int $amount, ?array $meta = null): Transfer
155155
{
156-
return DB::transaction(function() use ($amount, $wallet, $meta) {
156+
return DB::transaction(function () use ($amount, $wallet, $meta) {
157157
$fee = Tax::fee($wallet, $amount);
158158
$withdraw = $this->forceWithdraw($amount + $fee, $meta);
159159
$deposit = $wallet->deposit($amount, $meta);
@@ -198,21 +198,18 @@ protected function assemble(Wallet $wallet, Transaction $withdraw, Transaction $
198198
*/
199199
protected function change(int $amount, ?array $meta, bool $confirmed): Transaction
200200
{
201-
return DB::transaction(function() use ($amount, $meta, $confirmed) {
201+
return DB::transaction(function () use ($amount, $meta, $confirmed) {
202202

203-
if ($this instanceof WalletModel) {
204-
$payable = $this->holder;
205-
$wallet = $this;
206-
} else {
207-
$payable = $this;
203+
$wallet = $this;
204+
if (!($this instanceof WalletModel)) {
208205
$wallet = $this->wallet;
209206
}
210207

211208
if ($confirmed) {
212209
$this->addBalance($wallet, $amount);
213210
}
214211

215-
return $payable->transactions()->create([
212+
return $this->transactions()->create([
216213
'type' => $amount > 0 ? 'deposit' : 'withdraw',
217214
'wallet_id' => $wallet->getKey(),
218215
'uuid' => Uuid::uuid4()->toString(),
@@ -230,7 +227,8 @@ protected function change(int $amount, ?array $meta, bool $confirmed): Transacti
230227
*/
231228
public function transactions(): MorphMany
232229
{
233-
return $this->morphMany(config('wallet.transaction.model'), 'payable');
230+
return ($this instanceof WalletModel ? $this->holder : $this)
231+
->morphMany(config('wallet.transaction.model'), 'payable');
234232
}
235233

236234
/**
@@ -241,7 +239,8 @@ public function transactions(): MorphMany
241239
*/
242240
public function transfers(): MorphMany
243241
{
244-
return $this->morphMany(config('wallet.transfer.model'), 'from');
242+
return ($this instanceof WalletModel ? $this->holder : $this)
243+
->morphMany(config('wallet.transfer.model'), 'from');
245244
}
246245

247246
/**
@@ -252,7 +251,8 @@ public function transfers(): MorphMany
252251
*/
253252
public function wallet(): MorphOne
254253
{
255-
return $this->morphOne(config('wallet.wallet.model'), 'holder')
254+
return ($this instanceof WalletModel ? $this->holder : $this)
255+
->morphOne(config('wallet.wallet.model'), 'holder')
256256
->withDefault([
257257
'name' => config('wallet.wallet.default.name'),
258258
'slug' => config('wallet.wallet.default.slug'),
@@ -289,7 +289,7 @@ public function getBalanceAttribute(): int
289289
if ($this instanceof WalletModel) {
290290
$this->exists or $this->save();
291291
if (!WalletProxy::has($this->getKey())) {
292-
WalletProxy::set($this->getKey(), (int) ($this->attributes['balance'] ?? 0));
292+
WalletProxy::set($this->getKey(), (int)($this->attributes['balance'] ?? 0));
293293
}
294294

295295
return WalletProxy::get($this->getKey());

src/WalletProxy.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public static function has(int $key): bool
2525
*/
2626
public static function get(int $key): int
2727
{
28-
return (int) (static::$rows[$key] ?? 0);
28+
return (int)(static::$rows[$key] ?? 0);
2929
}
3030

3131
/**

0 commit comments

Comments
 (0)