Skip to content

Commit 7336321

Browse files
committed
drop addBalance
1 parent a99f2ad commit 7336321

File tree

4 files changed

+21
-93
lines changed

4 files changed

+21
-93
lines changed

src/Internal/Service/DatabaseService.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ public function transaction(callable $callback)
4343
return $callback();
4444
}
4545

46+
$this->stateService->purge();
47+
4648
return $this->connection->transaction(function () use ($callback) {
4749
$result = $callback();
4850
$this->stateService->commit();

src/Services/CommonServiceLegacy.php

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
use Bavix\Wallet\Internal\Service\DatabaseServiceInterface;
1616
use Bavix\Wallet\Models\Transaction;
1717
use Bavix\Wallet\Models\Transfer;
18-
use Bavix\Wallet\Models\Wallet as WalletModel;
1918
use Illuminate\Database\RecordsNotFoundException;
2019

2120
/** @deprecated */
@@ -119,42 +118,6 @@ public function applyTransfers(array $objects): array
119118
});
120119
}
121120

122-
/**
123-
* @param int|string $amount
124-
*
125-
* @deprecated
126-
*
127-
* @throws LockProviderNotFoundException
128-
* @throws RecordsNotFoundException
129-
* @throws TransactionFailedException
130-
* @throws ExceptionInterface
131-
*/
132-
public function addBalance(Wallet $wallet, $amount): bool
133-
{
134-
return $this->databaseService->transaction(function () use ($wallet, $amount) {
135-
/** @var WalletModel $wallet */
136-
$walletObject = $this->castService->getWallet($wallet);
137-
$balance = $this->regulatorService->increase($walletObject, $amount);
138-
$this->stateService->persist($wallet);
139-
$result = 0;
140-
141-
try {
142-
$result = $walletObject->newQuery()
143-
->whereKey($walletObject->getKey())
144-
->update(['balance' => $balance])
145-
;
146-
} finally {
147-
if ($result === 0) {
148-
$this->regulatorService->decrease($walletObject, $amount);
149-
} else {
150-
$walletObject->fill(['balance' => $balance])->syncOriginalAttribute('balance');
151-
}
152-
}
153-
154-
return (bool) $result;
155-
});
156-
}
157-
158121
/**
159122
* @param float|int|string $amount
160123
*

src/Traits/CanConfirm.php

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
use Bavix\Wallet\Models\Transaction;
1919
use Bavix\Wallet\Services\AtomicServiceInterface;
2020
use Bavix\Wallet\Services\CastServiceInterface;
21-
use Bavix\Wallet\Services\CommonServiceLegacy;
2221
use Bavix\Wallet\Services\ConsistencyServiceInterface;
22+
use Bavix\Wallet\Services\RegulatorServiceInterface;
2323
use Illuminate\Database\RecordsNotFoundException;
2424

2525
trait CanConfirm
@@ -76,15 +76,12 @@ public function resetConfirm(Transaction $transaction): bool
7676
);
7777
}
7878

79-
$wallet = app(CastServiceInterface::class)->getWallet($this);
80-
$mathService = app(MathServiceInterface::class);
81-
$negativeAmount = $mathService->negative($transaction->amount);
79+
app(RegulatorServiceInterface::class)->decrease(
80+
app(CastServiceInterface::class)->getWallet($this),
81+
$transaction->amount
82+
);
8283

83-
return $transaction->update(['confirmed' => false]) &&
84-
// update balance
85-
app(CommonServiceLegacy::class)
86-
->addBalance($wallet, $negativeAmount)
87-
;
84+
return $transaction->update(['confirmed' => false]);
8885
});
8986
}
9087

@@ -124,11 +121,9 @@ public function forceConfirm(Transaction $transaction): bool
124121
);
125122
}
126123

127-
return $transaction->update(['confirmed' => true]) &&
128-
// update balance
129-
app(CommonServiceLegacy::class)
130-
->addBalance($wallet, $transaction->amount)
131-
;
124+
app(RegulatorServiceInterface::class)->increase($wallet, $transaction->amount);
125+
126+
return $transaction->update(['confirmed' => true]);
132127
});
133128
}
134129
}

tests/Units/Domain/BalanceTest.php

Lines changed: 10 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
use function app;
88
use Bavix\Wallet\Models\Wallet;
99
use Bavix\Wallet\Services\BookkeeperServiceInterface;
10-
use Bavix\Wallet\Services\CommonServiceLegacy;
1110
use Bavix\Wallet\Services\RegulatorServiceInterface;
11+
use Bavix\Wallet\Services\StateServiceInterface;
1212
use Bavix\Wallet\Test\Infra\Factories\BuyerFactory;
1313
use Bavix\Wallet\Test\Infra\Models\Buyer;
1414
use Bavix\Wallet\Test\Infra\TestCase;
@@ -77,7 +77,7 @@ public function testWithdrawWalletExists(): void
7777
/**
7878
* @throws
7979
*/
80-
public function testSimple(): void
80+
public function testSimpleLLLL(): void
8181
{
8282
/** @var Buyer $buyer */
8383
$buyer = BuyerFactory::new()->create();
@@ -92,8 +92,10 @@ public function testSimple(): void
9292
$wallet->deposit(1000);
9393
self::assertSame(1000, $wallet->balanceInt);
9494

95-
$result = app(CommonServiceLegacy::class)->addBalance($wallet, 100);
96-
self::assertTrue($result);
95+
$result = app(RegulatorServiceInterface::class)->increase($wallet, 100);
96+
app(StateServiceInterface::class)->persist($wallet);
97+
98+
self::assertSame(1100, (int) $result);
9799

98100
self::assertSame(1100, $wallet->balanceInt);
99101
self::assertTrue($wallet->refreshBalance());
@@ -104,8 +106,10 @@ public function testSimple(): void
104106
self::assertTrue($wallet->delete());
105107
self::assertFalse($wallet->exists);
106108
self::assertSame($wallet->getKey(), $key);
107-
$result = app(CommonServiceLegacy::class)->addBalance($wallet, 100);
108-
self::assertTrue($result); // automatic create default wallet
109+
$result = app(RegulatorServiceInterface::class)->increase($wallet, 100);
110+
app(StateServiceInterface::class)->persist($wallet);
111+
112+
self::assertSame(1100, (int) $result); // automatic create default wallet
109113

110114
$wallet->refreshBalance();
111115
$balance = 0;
@@ -226,40 +230,4 @@ public function testForceUpdate(): void
226230
self::assertSame(1000, $wallet->balanceInt);
227231
self::assertSame(1000, (int) $wallet->getRawOriginal('balance'));
228232
}
229-
230-
public function testFailUpdate(): void
231-
{
232-
/** @var Buyer $buyer */
233-
$buyer = BuyerFactory::new()->create();
234-
self::assertFalse($buyer->relationLoaded('wallet'));
235-
$wallet = $buyer->wallet;
236-
237-
self::assertFalse($wallet->exists);
238-
self::assertSame(0, $wallet->balanceInt);
239-
self::assertTrue($wallet->exists);
240-
241-
/** @var MockObject|Wallet $mockQuery */
242-
$mockQuery = $this->createMock(\get_class($wallet->newQuery()));
243-
$mockQuery->method('whereKey')->willReturn($mockQuery);
244-
$mockQuery->method('update')->willReturn(0);
245-
246-
/** @var MockObject|Wallet $mockWallet */
247-
$mockWallet = $this->createMock(\get_class($wallet));
248-
$mockWallet->method('newQuery')->willReturn($mockQuery);
249-
$mockWallet->method('getKey')->willReturn($wallet->getKey());
250-
$mockWallet->method('fill')->willReturn($mockWallet);
251-
$mockWallet->method('syncOriginalAttribute')->willReturn($mockWallet);
252-
$mockWallet->method('__get')->with('uuid')->willReturn($wallet->uuid);
253-
254-
$bookkeeper = app(BookkeeperServiceInterface::class);
255-
$regulator = app(RegulatorServiceInterface::class);
256-
$result = app(CommonServiceLegacy::class)
257-
->addBalance($mockWallet, 100)
258-
;
259-
260-
self::assertFalse($result);
261-
self::assertSame('0', $regulator->amount($wallet));
262-
self::assertSame('0', $bookkeeper->amount($wallet));
263-
self::assertSame('0', $wallet->balance);
264-
}
265233
}

0 commit comments

Comments
 (0)