Skip to content

Commit ee7eedd

Browse files
committed
update AtomicServiceInterface contract
1 parent 6fe8bfe commit ee7eedd

18 files changed

+237
-63
lines changed

src/Interfaces/Confirmable.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,13 @@
77
use Bavix\Wallet\Exceptions\BalanceIsEmpty;
88
use Bavix\Wallet\Exceptions\ConfirmedInvalid;
99
use Bavix\Wallet\Exceptions\InsufficientFunds;
10+
use Bavix\Wallet\Exceptions\UnconfirmedInvalid;
1011
use Bavix\Wallet\Exceptions\WalletOwnerInvalid;
12+
use Bavix\Wallet\Internal\Exceptions\ExceptionInterface;
13+
use Bavix\Wallet\Internal\Exceptions\LockProviderNotFoundException;
14+
use Bavix\Wallet\Internal\Exceptions\TransactionFailedException;
1115
use Bavix\Wallet\Models\Transaction;
16+
use Illuminate\Database\RecordsNotFoundException;
1217

1318
interface Confirmable
1419
{
@@ -17,13 +22,21 @@ interface Confirmable
1722
* @throws InsufficientFunds
1823
* @throws ConfirmedInvalid
1924
* @throws WalletOwnerInvalid
25+
* @throws LockProviderNotFoundException
26+
* @throws RecordsNotFoundException
27+
* @throws TransactionFailedException
28+
* @throws ExceptionInterface
2029
*/
2130
public function confirm(Transaction $transaction): bool;
2231

2332
public function safeConfirm(Transaction $transaction): bool;
2433

2534
/**
26-
* @throws ConfirmedInvalid
35+
* @throws UnconfirmedInvalid
36+
* @throws LockProviderNotFoundException
37+
* @throws RecordsNotFoundException
38+
* @throws TransactionFailedException
39+
* @throws ExceptionInterface
2740
*/
2841
public function resetConfirm(Transaction $transaction): bool;
2942

@@ -32,6 +45,10 @@ public function safeResetConfirm(Transaction $transaction): bool;
3245
/**
3346
* @throws ConfirmedInvalid
3447
* @throws WalletOwnerInvalid
48+
* @throws LockProviderNotFoundException
49+
* @throws RecordsNotFoundException
50+
* @throws TransactionFailedException
51+
* @throws ExceptionInterface
3552
*/
3653
public function forceConfirm(Transaction $transaction): bool;
3754
}

src/Interfaces/Exchangeable.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,25 @@
44

55
namespace Bavix\Wallet\Interfaces;
66

7+
use Bavix\Wallet\Exceptions\BalanceIsEmpty;
8+
use Bavix\Wallet\Exceptions\InsufficientFunds;
9+
use Bavix\Wallet\Internal\Exceptions\ExceptionInterface;
10+
use Bavix\Wallet\Internal\Exceptions\LockProviderNotFoundException;
11+
use Bavix\Wallet\Internal\Exceptions\TransactionFailedException;
712
use Bavix\Wallet\Models\Transfer;
13+
use Illuminate\Database\RecordsNotFoundException;
814

915
interface Exchangeable
1016
{
1117
/**
1218
* @param int|string $amount
19+
*
20+
* @throws BalanceIsEmpty
21+
* @throws InsufficientFunds
22+
* @throws LockProviderNotFoundException
23+
* @throws RecordsNotFoundException
24+
* @throws TransactionFailedException
25+
* @throws ExceptionInterface
1326
*/
1427
public function exchange(Wallet $to, $amount, ?array $meta = null): Transfer;
1528

@@ -20,6 +33,11 @@ public function safeExchange(Wallet $to, $amount, ?array $meta = null): ?Transfe
2033

2134
/**
2235
* @param int|string $amount
36+
*
37+
* @throws LockProviderNotFoundException
38+
* @throws RecordsNotFoundException
39+
* @throws TransactionFailedException
40+
* @throws ExceptionInterface
2341
*/
2442
public function forceExchange(Wallet $to, $amount, ?array $meta = null): Transfer;
2543
}

src/Internal/Service/DatabaseService.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@ public function __construct(
3333
*
3434
* @return mixed
3535
*/
36-
public function transaction(Closure $closure)
36+
public function transaction(callable $callback)
3737
{
3838
try {
3939
if ($this->connection->transactionLevel() > 0) {
40-
return $closure();
40+
return $callback();
4141
}
4242

43-
return $this->connection->transaction($closure);
43+
return $this->connection->transaction(Closure::fromCallable($callback));
4444
} catch (RecordsNotFoundException|ExceptionInterface $exception) {
4545
throw $exception;
4646
} catch (Throwable $throwable) {

src/Internal/Service/DatabaseServiceInterface.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
use Bavix\Wallet\Internal\Exceptions\ExceptionInterface;
88
use Bavix\Wallet\Internal\Exceptions\TransactionFailedException;
9-
use Closure;
109
use Illuminate\Database\RecordsNotFoundException;
1110

1211
interface DatabaseServiceInterface
@@ -18,5 +17,5 @@ interface DatabaseServiceInterface
1817
*
1918
* @return mixed
2019
*/
21-
public function transaction(Closure $closure);
20+
public function transaction(callable $callback);
2221
}

src/Internal/Service/StorageService.php

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,26 @@
55
namespace Bavix\Wallet\Internal\Service;
66

77
use Bavix\Wallet\Internal\Exceptions\ExceptionInterface;
8+
use Bavix\Wallet\Internal\Exceptions\LockProviderNotFoundException;
89
use Bavix\Wallet\Internal\Exceptions\RecordNotFoundException;
910
use Illuminate\Cache\CacheManager;
1011
use Illuminate\Config\Repository as ConfigRepository;
1112
use Illuminate\Contracts\Cache\Repository as CacheRepository;
1213

1314
final class StorageService implements StorageServiceInterface
1415
{
16+
private LockServiceInterface $lockService;
17+
private MathServiceInterface $mathService;
1518
private CacheRepository $cache;
1619

17-
private LockServiceInterface $lock;
18-
19-
private MathServiceInterface $math;
20-
2120
public function __construct(
2221
CacheManager $cacheManager,
2322
ConfigRepository $config,
24-
LockServiceInterface $lock,
25-
MathServiceInterface $math
23+
LockServiceInterface $lockService,
24+
MathServiceInterface $mathService
2625
) {
27-
$this->math = $math;
28-
$this->lock = $lock;
26+
$this->mathService = $mathService;
27+
$this->lockService = $lockService;
2928
$this->cache = $cacheManager->driver(
3029
$config->get('wallet.cache.driver', 'array')
3130
);
@@ -41,6 +40,7 @@ public function missing(string $key): bool
4140
return $this->cache->forget($key);
4241
}
4342

43+
/** @throws RecordNotFoundException */
4444
public function get(string $key): string
4545
{
4646
$value = $this->cache->get($key);
@@ -51,23 +51,29 @@ public function get(string $key): string
5151
);
5252
}
5353

54-
return $this->math->round($value);
54+
return $this->mathService->round($value);
5555
}
5656

5757
public function sync(string $key, $value): bool
5858
{
5959
return $this->cache->set($key, $value);
6060
}
6161

62+
/**
63+
* @param float|int|string $value
64+
*
65+
* @throws LockProviderNotFoundException
66+
* @throws RecordNotFoundException
67+
*/
6268
public function increase(string $key, $value): string
6369
{
64-
return $this->lock->block(
70+
return $this->lockService->block(
6571
$key.'::increase',
6672
function () use ($key, $value): string {
67-
$result = $this->math->add($this->get($key), $value);
73+
$result = $this->mathService->add($this->get($key), $value);
6874
$this->sync($key, $result);
6975

70-
return $this->math->round($result);
76+
return $this->mathService->round($result);
7177
}
7278
);
7379
}

src/Internal/Service/StorageServiceInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Bavix\Wallet\Internal\Service;
66

7+
use Bavix\Wallet\Internal\Exceptions\LockProviderNotFoundException;
78
use Bavix\Wallet\Internal\Exceptions\RecordNotFoundException;
89

910
interface StorageServiceInterface
@@ -21,6 +22,7 @@ public function sync(string $key, $value): bool;
2122
/**
2223
* @param float|int|string $value
2324
*
25+
* @throws LockProviderNotFoundException
2426
* @throws RecordNotFoundException
2527
*/
2628
public function increase(string $key, $value): string;

src/Models/Wallet.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
use Bavix\Wallet\Interfaces\Exchangeable;
1212
use Bavix\Wallet\Interfaces\WalletFloat;
1313
use Bavix\Wallet\Internal\Exceptions\ExceptionInterface;
14+
use Bavix\Wallet\Internal\Exceptions\LockProviderNotFoundException;
15+
use Bavix\Wallet\Internal\Exceptions\TransactionFailedException;
1416
use Bavix\Wallet\Internal\Service\DatabaseServiceInterface;
1517
use Bavix\Wallet\Services\WalletServiceLegacy;
1618
use Bavix\Wallet\Traits\CanConfirm;
@@ -20,6 +22,7 @@
2022
use function config;
2123
use Illuminate\Database\Eloquent\Model;
2224
use Illuminate\Database\Eloquent\Relations\MorphTo;
25+
use Illuminate\Database\RecordsNotFoundException;
2326
use Illuminate\Support\Str;
2427

2528
/**
@@ -97,6 +100,9 @@ public function setNameAttribute(string $name): void
97100
* Under ideal conditions, you will never need a method.
98101
* Needed to deal with out-of-sync.
99102
*
103+
* @throws LockProviderNotFoundException
104+
* @throws RecordsNotFoundException
105+
* @throws TransactionFailedException
100106
* @throws ExceptionInterface
101107
*/
102108
public function refreshBalance(): bool

src/Services/AtomicService.php

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,52 @@
44

55
namespace Bavix\Wallet\Services;
66

7+
use Bavix\Wallet\Interfaces\Wallet;
8+
use Bavix\Wallet\Internal\Exceptions\ExceptionInterface;
9+
use Bavix\Wallet\Internal\Exceptions\LockProviderNotFoundException;
10+
use Bavix\Wallet\Internal\Exceptions\TransactionFailedException;
11+
use Bavix\Wallet\Internal\Service\DatabaseServiceInterface;
712
use Bavix\Wallet\Internal\Service\LockServiceInterface;
13+
use Illuminate\Database\RecordsNotFoundException;
814

915
final class AtomicService implements AtomicServiceInterface
1016
{
1117
private const PREFIX = 'wallet_atomic::';
1218

19+
private DatabaseServiceInterface $databaseService;
1320
private LockServiceInterface $lockService;
1421
private CastServiceInterface $castService;
1522

1623
public function __construct(
24+
DatabaseServiceInterface $databaseService,
1725
LockServiceInterface $lockService,
1826
CastServiceInterface $castService
1927
) {
28+
$this->databaseService = $databaseService;
2029
$this->lockService = $lockService;
2130
$this->castService = $castService;
2231
}
2332

24-
/** @return mixed */
25-
public function block(object $object, callable $closure)
33+
/**
34+
* @throws LockProviderNotFoundException
35+
* @throws RecordsNotFoundException
36+
* @throws TransactionFailedException
37+
* @throws ExceptionInterface
38+
*
39+
* @return mixed
40+
*/
41+
public function block(Wallet $object, callable $callback)
2642
{
27-
return $this->lockService->block($this->key($object), $closure);
43+
return $this->lockService->block(
44+
$this->key($object),
45+
fn () => $this->databaseService->transaction($callback)
46+
);
2847
}
2948

30-
private function key(object $object): string
49+
private function key(Wallet $object): string
3150
{
32-
$model = $this->castService->getModel($object);
51+
$wallet = $this->castService->getWallet($object);
3352

34-
return self::PREFIX.'::'.get_class($model).'::'.$model->getKey();
53+
return self::PREFIX.'::'.get_class($wallet).'::'.$wallet->uuid;
3554
}
3655
}

src/Services/AtomicServiceInterface.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,21 @@
44

55
namespace Bavix\Wallet\Services;
66

7+
use Bavix\Wallet\Interfaces\Wallet;
8+
use Bavix\Wallet\Internal\Exceptions\ExceptionInterface;
9+
use Bavix\Wallet\Internal\Exceptions\LockProviderNotFoundException;
10+
use Bavix\Wallet\Internal\Exceptions\TransactionFailedException;
11+
use Illuminate\Database\RecordsNotFoundException;
12+
713
interface AtomicServiceInterface
814
{
9-
/** @return mixed */
10-
public function block(object $object, callable $closure);
15+
/**
16+
* @throws LockProviderNotFoundException
17+
* @throws RecordsNotFoundException
18+
* @throws TransactionFailedException
19+
* @throws ExceptionInterface
20+
*
21+
* @return mixed
22+
*/
23+
public function block(Wallet $object, callable $callback);
1124
}

0 commit comments

Comments
 (0)