Skip to content

Commit 62f8283

Browse files
authored
Merge pull request #104 from bavix/storable
#103 add storable
2 parents 3f15bee + c754e06 commit 62f8283

File tree

12 files changed

+200
-23
lines changed

12 files changed

+200
-23
lines changed

.phpstorm.meta.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace PHPSTORM_META {
44

55
use Bavix\Wallet\Interfaces\Rateable;
6+
use Bavix\Wallet\Interfaces\Storable;
67
use Bavix\Wallet\Models\Transaction;
78
use Bavix\Wallet\Models\Transfer;
89
use Bavix\Wallet\Models\Wallet;
@@ -28,6 +29,7 @@
2829
Transfer::class => Transfer::class,
2930
Transaction::class => Transaction::class,
3031
Rateable::class => Rateable::class,
32+
Storable::class => Storable::class,
3133
]));
3234

3335
}

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
],
2525
"require": {
2626
"php": "^7.1|^8.0",
27+
"ext-pdo": "*",
2728
"illuminate/database": "^5.5|^6.0",
2829
"nesbot/carbon": "^1.20|^2.0",
2930
"doctrine/dbal": "^2.8",

config/config.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Bavix\Wallet\Models\Transfer;
1414
use Bavix\Wallet\Models\Wallet;
1515
use Bavix\Wallet\Simple\Rate;
16+
use Bavix\Wallet\Simple\Store;
1617

1718
return [
1819
/**
@@ -21,6 +22,7 @@
2122
*/
2223
'package' => [
2324
'rateable' => Rate::class,
25+
'storable' => Store::class,
2426
],
2527

2628
/**

src/Interfaces/Storable.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Bavix\Wallet\Interfaces;
4+
5+
interface Storable
6+
{
7+
/**
8+
* @param Wallet $object
9+
* @return int
10+
*/
11+
public function getBalance($object): int;
12+
13+
/**
14+
* @param Wallet $object
15+
* @param int $amount
16+
* @return int
17+
*/
18+
public function incBalance($object, int $amount): int;
19+
20+
/**
21+
* @param Wallet $object
22+
* @param int $amount
23+
* @return bool
24+
*/
25+
public function setBalance($object, int $amount): bool;
26+
}

src/Services/CommonService.php

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

55
use Bavix\Wallet\Exceptions\BalanceIsEmpty;
66
use Bavix\Wallet\Exceptions\InsufficientFunds;
7+
use Bavix\Wallet\Interfaces\Storable;
78
use Bavix\Wallet\Interfaces\Wallet;
89
use Bavix\Wallet\Models\Transaction;
910
use Bavix\Wallet\Models\Transfer;
@@ -219,17 +220,24 @@ public function addBalance(Wallet $wallet, int $amount): bool
219220
{
220221
return app(LockService::class)->lock($this, __FUNCTION__, static function () use ($wallet, $amount) {
221222
/**
222-
* @var ProxyService $proxy
223223
* @var WalletModel $wallet
224224
*/
225-
$proxy = app(ProxyService::class);
226-
$balance = $wallet->balance + $amount;
227-
if ($proxy->has($wallet->getKey())) {
228-
$balance = $proxy->get($wallet->getKey()) + $amount;
225+
$balance = app(Storable::class)
226+
->incBalance($wallet, $amount);
227+
228+
try {
229+
$result = $wallet->update(compact('balance'));
230+
} catch (\Throwable $throwable) {
231+
app(Storable::class)
232+
->setBalance($wallet, $wallet->getAvailableBalance());
233+
234+
throw $throwable;
229235
}
230236

231-
$result = $wallet->update(compact('balance'));
232-
$proxy->set($wallet->getKey(), $balance);
237+
if (!$result) {
238+
app(Storable::class)
239+
->setBalance($wallet, $wallet->getAvailableBalance());
240+
}
233241

234242
return $result;
235243
});

src/Services/ProxyService.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
namespace Bavix\Wallet\Services;
44

55
use ArrayAccess;
6+
use Bavix\Wallet\Interfaces\Storable;
67
use function array_key_exists;
78

89
/**
910
* Class ProxyService
1011
* @package Bavix\Wallet\Services
1112
* @codeCoverageIgnore
13+
* @deprecated
1214
*/
1315
class ProxyService implements ArrayAccess
1416
{
@@ -94,6 +96,7 @@ public function offsetUnset($offset): void
9496
*/
9597
public function fresh(): void
9698
{
99+
app()->instance(Storable::class, null);
97100
$this->data = [];
98101
}
99102

src/Services/WalletService.php

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

55
use Bavix\Wallet\Exceptions\AmountInvalid;
66
use Bavix\Wallet\Interfaces\MinimalTaxable;
7+
use Bavix\Wallet\Interfaces\Storable;
78
use Bavix\Wallet\Interfaces\Taxable;
89
use Bavix\Wallet\Interfaces\Wallet;
910
use Bavix\Wallet\Models\Wallet as WalletModel;
@@ -45,7 +46,7 @@ public function fee(Wallet $wallet, int $amount): int
4546
if ($wallet instanceof MinimalTaxable) {
4647
$minimal = $wallet->getMinimalFee();
4748
if ($fee < $minimal) {
48-
$fee = $wallet->getMinimalFee();
49+
$fee = $minimal;
4950
}
5051
}
5152

@@ -94,16 +95,12 @@ public function getWallet(Wallet $object, bool $autoSave = true): WalletModel
9495
/**
9596
* @param Wallet $object
9697
* @return int
98+
* @deprecated use Storable::getBalance
9799
*/
98100
public function getBalance(Wallet $object): int
99101
{
100-
$wallet = $this->getWallet($object);
101-
$proxy = app(ProxyService::class);
102-
if (!$proxy->has($wallet->getKey())) {
103-
$proxy->set($wallet->getKey(), (int)$wallet->getOriginal('balance', 0));
104-
}
105-
106-
return $proxy[$wallet->getKey()];
102+
return app(Storable::class)
103+
->getBalance($object);
107104
}
108105

109106
/**
@@ -112,15 +109,13 @@ public function getBalance(Wallet $object): int
112109
*/
113110
public function refresh(WalletModel $wallet): bool
114111
{
115-
return app(LockService::class)->lock($this, __FUNCTION__, function () use ($wallet) {
116-
$this->getBalance($wallet);
112+
return app(LockService::class)->lock($this, __FUNCTION__, static function () use ($wallet) {
113+
app(Storable::class)->getBalance($wallet);
117114
$balance = $wallet->getAvailableBalance();
118115
$wallet->balance = $balance;
119116

120-
$proxy = app(ProxyService::class);
121-
$proxy->set($wallet->getKey(), $balance);
122-
123-
return $wallet->save();
117+
return app(Storable::class)->setBalance($wallet, $balance) &&
118+
$wallet->save();
124119
});
125120
}
126121

src/Simple/Store.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace Bavix\Wallet\Simple;
4+
5+
use Bavix\Wallet\Interfaces\Storable;
6+
use Bavix\Wallet\Services\WalletService;
7+
8+
class Store implements Storable
9+
{
10+
11+
/**
12+
* @var array
13+
*/
14+
protected $balanceSheets = [];
15+
16+
/**
17+
* @inheritDoc
18+
*/
19+
public function getBalance($object): int
20+
{
21+
$wallet = app(WalletService::class)->getWallet($object);
22+
if (!\array_key_exists($wallet->getKey(), $this->balanceSheets)) {
23+
$this->balanceSheets[$wallet->getKey()] = (int)$wallet->getOriginal('balance', 0);
24+
}
25+
26+
return $this->balanceSheets[$wallet->getKey()];
27+
}
28+
29+
/**
30+
* @inheritDoc
31+
*/
32+
public function incBalance($object, int $amount): int
33+
{
34+
$balance = $this->getBalance($object) + $amount;
35+
$this->setBalance($object, $balance);
36+
return $balance;
37+
}
38+
39+
/**
40+
* @inheritDoc
41+
*/
42+
public function setBalance($object, int $amount): bool
43+
{
44+
$wallet = app(WalletService::class)->getWallet($object);
45+
$this->balanceSheets[$wallet->getKey()] = $amount;
46+
return true;
47+
}
48+
49+
}

src/Traits/HasWallet.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Bavix\Wallet\Traits;
44

5+
use Bavix\Wallet\Interfaces\Storable;
56
use Bavix\Wallet\Interfaces\Wallet;
67
use Bavix\Wallet\Models\Transaction;
78
use Bavix\Wallet\Models\Transfer;
@@ -72,7 +73,7 @@ public function deposit(int $amount, ?array $meta = null, bool $confirmed = true
7273
*/
7374
public function getBalanceAttribute(): int
7475
{
75-
return app(WalletService::class)->getBalance($this);
76+
return app(Storable::class)->getBalance($this);
7677
}
7778

7879
/**

src/WalletServiceProvider.php

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

55
use Bavix\Wallet\Commands\RefreshBalance;
66
use Bavix\Wallet\Interfaces\Rateable;
7+
use Bavix\Wallet\Interfaces\Storable;
78
use Bavix\Wallet\Models\Transaction;
89
use Bavix\Wallet\Models\Transfer;
910
use Bavix\Wallet\Models\Wallet;
@@ -82,6 +83,7 @@ public function register(): void
8283

8384
// Bind eloquent models to IoC container
8485
$this->app->singleton(Rateable::class, config('wallet.package.rateable'));
86+
$this->app->singleton(Storable::class, config('wallet.package.storable'));
8587
$this->app->singleton(ExchangeService::class, config('wallet.services.exchange'));
8688
$this->app->singleton(CommonService::class, config('wallet.services.common'));
8789
$this->app->singleton(ProxyService::class, config('wallet.services.proxy'));

0 commit comments

Comments
 (0)