Skip to content

Commit 83e7697

Browse files
committed
StateServiceInterface
1 parent e0cfc5b commit 83e7697

File tree

6 files changed

+64
-2
lines changed

6 files changed

+64
-2
lines changed

src/Internal/Decorator/StorageServiceLockDecorator.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@
77
use Bavix\Wallet\Internal\Exceptions\LockProviderNotFoundException;
88
use Bavix\Wallet\Internal\Exceptions\RecordNotFoundException;
99
use Bavix\Wallet\Internal\Service\LockServiceInterface;
10+
use Bavix\Wallet\Internal\Service\StateServiceInterface;
1011
use Bavix\Wallet\Internal\Service\StorageServiceInterface;
1112

1213
final class StorageServiceLockDecorator implements StorageServiceInterface
1314
{
1415
public function __construct(
1516
private StorageServiceInterface $storageService,
17+
private StateServiceInterface $stateService,
1618
private LockServiceInterface $lockService
1719
) {
1820
}
@@ -29,7 +31,7 @@ public function missing(string $uuid): bool
2931

3032
public function get(string $uuid): string
3133
{
32-
return $this->storageService->get($uuid);
34+
return $this->stateService->get($uuid) ?? $this->storageService->get($uuid);
3335
}
3436

3537
public function sync(string $uuid, float|int|string $value): bool
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Bavix\Wallet\Internal\Service;
6+
7+
final class StateService implements StateServiceInterface
8+
{
9+
/**
10+
* @var array<string, string>
11+
*/
12+
private array $forks = [];
13+
14+
public function fork(string $uuid, string $value): void
15+
{
16+
$this->forks[$uuid] = $value;
17+
}
18+
19+
public function get(string $uuid): ?string
20+
{
21+
return $this->forks[$uuid] ?? null;
22+
}
23+
24+
public function drop(string $uuid): void
25+
{
26+
unset($this->forks[$uuid]);
27+
}
28+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Bavix\Wallet\Internal\Service;
6+
7+
interface StateServiceInterface
8+
{
9+
public function fork(string $uuid, string $value): void;
10+
11+
public function get(string $uuid): ?string;
12+
13+
public function drop(string $uuid): void;
14+
}

src/Models/Wallet.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Bavix\Wallet\Internal\Exceptions\LockProviderNotFoundException;
1515
use Bavix\Wallet\Internal\Exceptions\TransactionFailedException;
1616
use Bavix\Wallet\Internal\Service\MathServiceInterface;
17+
use Bavix\Wallet\Internal\Service\StateServiceInterface;
1718
use Bavix\Wallet\Internal\Service\UuidFactoryServiceInterface;
1819
use Bavix\Wallet\Services\AtomicServiceInterface;
1920
use Bavix\Wallet\Services\RegulatorServiceInterface;
@@ -114,6 +115,8 @@ public function setNameAttribute(string $name): void
114115
public function refreshBalance(): bool
115116
{
116117
return app(AtomicServiceInterface::class)->block($this, function () {
118+
app(StateServiceInterface::class)->drop($this->uuid);
119+
117120
$whatIs = $this->getBalanceAttribute();
118121
$balance = $this->getAvailableBalanceAttribute();
119122
if (app(MathServiceInterface::class)->compare($whatIs, $balance) === 0) {

src/Services/AtomicService.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Bavix\Wallet\Internal\Exceptions\TransactionFailedException;
1111
use Bavix\Wallet\Internal\Service\DatabaseServiceInterface;
1212
use Bavix\Wallet\Internal\Service\LockServiceInterface;
13+
use Bavix\Wallet\Internal\Service\StateServiceInterface;
1314
use Illuminate\Database\RecordsNotFoundException;
1415

1516
/**
@@ -19,6 +20,7 @@ final class AtomicService implements AtomicServiceInterface
1920
{
2021
public function __construct(
2122
private DatabaseServiceInterface $databaseService,
23+
private StateServiceInterface $stateService,
2224
private LockServiceInterface $lockService,
2325
private CastServiceInterface $castService
2426
) {
@@ -37,7 +39,17 @@ public function blocks(array $objects, callable $callback): mixed
3739
$callable = fn () => $this->databaseService->transaction($callback);
3840
foreach ($objects as $object) {
3941
$wallet = $this->castService->getWallet($object);
40-
$callable = fn () => $this->lockService->block($wallet->uuid, $callable);
42+
$callable = fn () => $this->lockService->block(
43+
$wallet->uuid,
44+
function () use ($wallet, $callable) {
45+
try {
46+
$this->stateService->fork($wallet->uuid, $wallet->balance);
47+
return $callable();
48+
} finally {
49+
$this->stateService->drop($wallet->uuid);
50+
}
51+
}
52+
);
4153
}
4254

4355
return $callable();

src/WalletServiceProvider.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@
5252
use Bavix\Wallet\Internal\Service\LockServiceInterface;
5353
use Bavix\Wallet\Internal\Service\MathService;
5454
use Bavix\Wallet\Internal\Service\MathServiceInterface;
55+
use Bavix\Wallet\Internal\Service\StateService;
56+
use Bavix\Wallet\Internal\Service\StateServiceInterface;
5557
use Bavix\Wallet\Internal\Service\StorageService;
5658
use Bavix\Wallet\Internal\Service\StorageServiceInterface;
5759
use Bavix\Wallet\Internal\Service\TranslatorService;
@@ -209,6 +211,7 @@ private function internal(array $configure): void
209211
$this->app->singleton(JsonServiceInterface::class, $configure['json'] ?? JsonService::class);
210212
$this->app->singleton(LockServiceInterface::class, $configure['lock'] ?? LockService::class);
211213
$this->app->singleton(MathServiceInterface::class, $configure['math'] ?? MathService::class);
214+
$this->app->singleton(StateServiceInterface::class, $configure['state'] ?? StateService::class);
212215
$this->app->singleton(TranslatorServiceInterface::class, $configure['translator'] ?? TranslatorService::class);
213216
$this->app->singleton(UuidFactoryServiceInterface::class, $configure['uuid'] ?? UuidFactoryService::class);
214217
}

0 commit comments

Comments
 (0)