Skip to content

Commit 912b632

Browse files
committed
atomic service
1 parent aee935d commit 912b632

11 files changed

+66
-76
lines changed

.phpstorm.meta.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
use Bavix\Wallet\Internal\Assembler\TransferLazyDtoAssemblerInterface;
88
use Bavix\Wallet\Internal\Repository\TransactionRepositoryInterface;
99
use Bavix\Wallet\Internal\Repository\TransferRepositoryInterface;
10-
use Bavix\Wallet\Services\AtomicKeyService;
11-
use Bavix\Wallet\Services\AtomicKeyServiceInterface;
10+
use Bavix\Wallet\Services\AtomicService;
11+
use Bavix\Wallet\Services\AtomicServiceInterface;
1212
use Bavix\Wallet\Services\CastServiceInterface;
1313
use Bavix\Wallet\Internal\Service\JsonServiceInterface;
1414
use Bavix\Wallet\Services\DiscountServiceInterface;
@@ -72,7 +72,7 @@
7272
// services
7373
AssistantServiceInterface::class => AssistantServiceInterface::class,
7474
AtmServiceInterface::class => AtmServiceInterface::class,
75-
AtomicKeyServiceInterface::class => AtomicKeyServiceInterface::class,
75+
AtomicServiceInterface::class => AtomicServiceInterface::class,
7676
BasketServiceInterface::class => BasketServiceInterface::class,
7777
BookkeeperServiceInterface::class => BookkeeperServiceInterface::class,
7878
CastServiceInterface::class => CastServiceInterface::class,

config/config.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
use Bavix\Wallet\Models\Wallet;
2323
use Bavix\Wallet\Services\AssistantService;
2424
use Bavix\Wallet\Services\AtmService;
25-
use Bavix\Wallet\Services\AtomicKeyService;
25+
use Bavix\Wallet\Services\AtomicService;
2626
use Bavix\Wallet\Services\BasketService;
2727
use Bavix\Wallet\Services\BookkeeperService;
2828
use Bavix\Wallet\Services\CastService;
@@ -73,7 +73,7 @@
7373
'services' => [
7474
'assistant' => AssistantService::class,
7575
'atm' => AtmService::class,
76-
'key' => AtomicKeyService::class,
76+
'atomic' => AtomicService::class,
7777
'basket' => BasketService::class,
7878
'bookkeeper' => BookkeeperService::class,
7979
'cast' => CastService::class,

src/Services/AtomicKeyService.php

Lines changed: 0 additions & 24 deletions
This file was deleted.

src/Services/AtomicKeyServiceInterface.php

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/Services/AtomicService.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Bavix\Wallet\Services;
6+
7+
use Bavix\Wallet\Internal\Service\LockServiceInterface;
8+
9+
final class AtomicService implements AtomicServiceInterface
10+
{
11+
private const PREFIX = 'wallet_atomic::';
12+
13+
private LockServiceInterface $lockService;
14+
private CastServiceInterface $castService;
15+
16+
public function __construct(
17+
LockServiceInterface $lockService,
18+
CastServiceInterface $castService
19+
) {
20+
$this->lockService = $lockService;
21+
$this->castService = $castService;
22+
}
23+
24+
/** @return mixed */
25+
public function block(object $object, callable $closure)
26+
{
27+
return $this->lockService->block($this->key($object), $closure);
28+
}
29+
30+
private function key(object $object): string
31+
{
32+
$model = $this->castService->getModel($object);
33+
34+
return self::PREFIX.'::'.get_class($model).'::'.$model->getKey();
35+
}
36+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Bavix\Wallet\Services;
6+
7+
interface AtomicServiceInterface
8+
{
9+
/** @return mixed */
10+
public function block(object $object, callable $closure);
11+
}

src/Services/WalletServiceLegacy.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,23 @@
44

55
namespace Bavix\Wallet\Services;
66

7-
use Bavix\Wallet\Internal\Service\LockServiceInterface;
87
use Bavix\Wallet\Internal\Service\MathServiceInterface;
98
use Bavix\Wallet\Models\Wallet as WalletModel;
109

1110
final class WalletServiceLegacy
1211
{
1312
private MathServiceInterface $math;
14-
private LockServiceInterface $lockService;
1513
private BookkeeperServiceInterface $bookkeeper;
16-
private AtomicKeyServiceInterface $atomicKeyService;
14+
private AtomicServiceInterface $atomicService;
1715

1816
public function __construct(
1917
MathServiceInterface $math,
20-
LockServiceInterface $lockService,
2118
BookkeeperServiceInterface $bookkeeper,
22-
AtomicKeyServiceInterface $atomicKeyService
19+
AtomicServiceInterface $atomicService
2320
) {
2421
$this->math = $math;
25-
$this->lockService = $lockService;
2622
$this->bookkeeper = $bookkeeper;
27-
$this->atomicKeyService = $atomicKeyService;
23+
$this->atomicService = $atomicService;
2824
}
2925

3026
/**
@@ -33,7 +29,7 @@ public function __construct(
3329
*/
3430
public function refresh(WalletModel $wallet): bool
3531
{
36-
return $this->lockService->block($this->atomicKeyService->getIdentifier($wallet), function () use ($wallet) {
32+
return $this->atomicService->block($wallet, function () use ($wallet) {
3733
$whatIs = $wallet->balance;
3834
$balance = $wallet->getAvailableBalance();
3935
if ($this->math->compare($whatIs, $balance) === 0) {

src/Traits/CanConfirm.php

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,10 @@
1111
use Bavix\Wallet\Exceptions\WalletOwnerInvalid;
1212
use Bavix\Wallet\Internal\Exceptions\ExceptionInterface;
1313
use Bavix\Wallet\Internal\Service\DatabaseServiceInterface;
14-
use Bavix\Wallet\Internal\Service\LockServiceInterface;
1514
use Bavix\Wallet\Internal\Service\MathServiceInterface;
1615
use Bavix\Wallet\Internal\Service\TranslatorServiceInterface;
1716
use Bavix\Wallet\Models\Transaction;
18-
use Bavix\Wallet\Services\AtomicKeyServiceInterface;
17+
use Bavix\Wallet\Services\AtomicServiceInterface;
1918
use Bavix\Wallet\Services\CastServiceInterface;
2019
use Bavix\Wallet\Services\CommonServiceLegacy;
2120
use Bavix\Wallet\Services\ConsistencyServiceInterface;
@@ -59,11 +58,7 @@ public function safeConfirm(Transaction $transaction): bool
5958
*/
6059
public function resetConfirm(Transaction $transaction): bool
6160
{
62-
$atomicKey = app(AtomicKeyServiceInterface::class)
63-
->getIdentifier($this)
64-
;
65-
66-
return app(LockServiceInterface::class)->block($atomicKey, fn () => app(DatabaseServiceInterface::class)->transaction(function () use ($transaction) {
61+
return app(AtomicServiceInterface::class)->block($this, fn () => app(DatabaseServiceInterface::class)->transaction(function () use ($transaction) {
6762
if (!$transaction->confirmed) {
6863
throw new UnconfirmedInvalid(
6964
app(TranslatorServiceInterface::class)->get('wallet::errors.unconfirmed_invalid'),
@@ -98,11 +93,7 @@ public function safeResetConfirm(Transaction $transaction): bool
9893
*/
9994
public function forceConfirm(Transaction $transaction): bool
10095
{
101-
$atomicKey = app(AtomicKeyServiceInterface::class)
102-
->getIdentifier($this)
103-
;
104-
105-
return app(LockServiceInterface::class)->block($atomicKey, fn () => app(DatabaseServiceInterface::class)->transaction(function () use ($transaction) {
96+
return app(AtomicServiceInterface::class)->block($this, fn () => app(DatabaseServiceInterface::class)->transaction(function () use ($transaction) {
10697
if ($transaction->confirmed) {
10798
throw new ConfirmedInvalid(
10899
app(TranslatorServiceInterface::class)->get('wallet::errors.confirmed_invalid'),

src/Traits/CanExchange.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@
88
use Bavix\Wallet\Internal\Assembler\TransferLazyDtoAssemblerInterface;
99
use Bavix\Wallet\Internal\Exceptions\ExceptionInterface;
1010
use Bavix\Wallet\Internal\Service\DatabaseServiceInterface;
11-
use Bavix\Wallet\Internal\Service\LockServiceInterface;
1211
use Bavix\Wallet\Internal\Service\MathServiceInterface;
1312
use Bavix\Wallet\Models\Transfer;
14-
use Bavix\Wallet\Services\AtomicKeyServiceInterface;
13+
use Bavix\Wallet\Services\AtomicServiceInterface;
1514
use Bavix\Wallet\Services\CastServiceInterface;
1615
use Bavix\Wallet\Services\CommonServiceLegacy;
1716
use Bavix\Wallet\Services\ConsistencyServiceInterface;
@@ -50,11 +49,7 @@ public function safeExchange(Wallet $to, $amount, ?array $meta = null): ?Transfe
5049
*/
5150
public function forceExchange(Wallet $to, $amount, ?array $meta = null): Transfer
5251
{
53-
$atomicKey = app(AtomicKeyServiceInterface::class)
54-
->getIdentifier($this)
55-
;
56-
57-
return app(LockServiceInterface::class)->block($atomicKey, fn () => app(DatabaseServiceInterface::class)->transaction(function () use ($to, $amount, $meta) {
52+
return app(AtomicServiceInterface::class)->block($this, fn () => app(DatabaseServiceInterface::class)->transaction(function () use ($to, $amount, $meta) {
5853
$prepareService = app(PrepareServiceInterface::class);
5954
$mathService = app(MathServiceInterface::class);
6055
$castService = app(CastServiceInterface::class);

src/Traits/HasGift.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,11 @@
1414
use Bavix\Wallet\Internal\Assembler\TransferDtoAssemblerInterface;
1515
use Bavix\Wallet\Internal\Exceptions\ExceptionInterface;
1616
use Bavix\Wallet\Internal\Service\DatabaseServiceInterface;
17-
use Bavix\Wallet\Internal\Service\LockServiceInterface;
1817
use Bavix\Wallet\Internal\Service\MathServiceInterface;
1918
use Bavix\Wallet\Models\Transaction;
2019
use Bavix\Wallet\Models\Transfer;
2120
use Bavix\Wallet\Services\AtmServiceInterface;
22-
use Bavix\Wallet\Services\AtomicKeyServiceInterface;
21+
use Bavix\Wallet\Services\AtomicServiceInterface;
2322
use Bavix\Wallet\Services\CastServiceInterface;
2423
use Bavix\Wallet\Services\CommonServiceLegacy;
2524
use Bavix\Wallet\Services\ConsistencyServiceInterface;
@@ -56,11 +55,7 @@ public function safeGift(Wallet $to, Product $product, bool $force = false): ?Tr
5655
*/
5756
public function gift(Wallet $to, Product $product, bool $force = false): Transfer
5857
{
59-
$atomicKey = app(AtomicKeyServiceInterface::class)
60-
->getIdentifier($this)
61-
;
62-
63-
return app(LockServiceInterface::class)->block($atomicKey, function () use ($to, $product, $force): Transfer {
58+
return app(AtomicServiceInterface::class)->block($this, function () use ($to, $product, $force): Transfer {
6459
/**
6560
* Who's giving? Let's call him Santa Claus.
6661
*

0 commit comments

Comments
 (0)