Skip to content

Commit e8bac3b

Browse files
committed
optimize addBalance
1 parent fc78cbd commit e8bac3b

File tree

4 files changed

+14
-8
lines changed

4 files changed

+14
-8
lines changed

src/Services/CommonServiceLegacy.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
use Bavix\Wallet\Internal\Exceptions\RecordNotFoundException;
1414
use Bavix\Wallet\Internal\Exceptions\TransactionFailedException;
1515
use Bavix\Wallet\Internal\Service\DatabaseServiceInterface;
16-
use Bavix\Wallet\Internal\Service\MathServiceInterface;
1716
use Bavix\Wallet\Models\Transaction;
1817
use Bavix\Wallet\Models\Transfer;
1918
use Bavix\Wallet\Models\Wallet as WalletModel;
@@ -30,7 +29,6 @@ final class CommonServiceLegacy
3029
private RegulatorServiceInterface $regulatorService;
3130
private TransferDtoAssemblerInterface $transferDtoAssembler;
3231
private StateServiceInterface $stateService;
33-
private MathServiceInterface $mathService;
3432

3533
public function __construct(
3634
CastServiceInterface $castService,
@@ -40,8 +38,7 @@ public function __construct(
4038
TransferDtoAssemblerInterface $transferDtoAssembler,
4139
RegulatorServiceInterface $regulatorService,
4240
StateServiceInterface $stateService,
43-
AtmServiceInterface $atmService,
44-
MathServiceInterface $mathService
41+
AtmServiceInterface $atmService
4542
) {
4643
$this->atmService = $atmService;
4744
$this->castService = $castService;
@@ -51,7 +48,6 @@ public function __construct(
5148
$this->regulatorService = $regulatorService;
5249
$this->transferDtoAssembler = $transferDtoAssembler;
5350
$this->stateService = $stateService;
54-
$this->mathService = $mathService;
5551
}
5652

5753
/**
@@ -147,11 +143,11 @@ public function addBalance(Wallet $wallet, $amount): bool
147143
->whereKey($walletObject->getKey())
148144
->update(['balance' => $balance])
149145
;
150-
151-
$walletObject->fill(['balance' => $balance])->syncOriginalAttribute('balance');
152146
} finally {
153147
if ($result === 0) {
154-
$this->regulatorService->increase($walletObject, $this->mathService->negative($amount));
148+
$this->regulatorService->decrease($walletObject, $amount);
149+
} else {
150+
$walletObject->fill(['balance' => $balance])->syncOriginalAttribute('balance');
155151
}
156152
}
157153

src/Services/RegulatorService.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,12 @@ public function increase(Wallet $wallet, $value): string
7474
}
7575
}
7676

77+
/** @param float|int|string $value */
78+
public function decrease(Wallet $wallet, $value): string
79+
{
80+
return $this->increase($wallet, $this->mathService->negative($value));
81+
}
82+
7783
private function getKey(string $uuid): string
7884
{
7985
return $this->idempotentKey.'::'.$uuid;

src/Services/RegulatorServiceInterface.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,7 @@ public function sync(Wallet $wallet, $value): bool;
1919

2020
/** @param float|int|string $value */
2121
public function increase(Wallet $wallet, $value): string;
22+
23+
/** @param float|int|string $value */
24+
public function decrease(Wallet $wallet, $value): string;
2225
}

tests/Units/Domain/CartTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ public function testBoughtGoods(): void
254254
public function testWithdrawal(): void
255255
{
256256
$transactionLevel = Buyer::query()->getConnection()->transactionLevel();
257+
self::assertSame(0, $transactionLevel);
257258

258259
/**
259260
* @var Buyer $buyer

0 commit comments

Comments
 (0)