Skip to content

Commit ad4845f

Browse files
authored
Merge pull request #55 from bavix/micro-optimization
add static
2 parents 8c600da + 576c2b6 commit ad4845f

File tree

6 files changed

+33
-24
lines changed

6 files changed

+33
-24
lines changed

src/Commands/RefreshBalance.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class RefreshBalance extends Command
3434
*/
3535
public function handle(): void
3636
{
37-
DB::transaction(function() {
37+
DB::transaction(static function() {
3838
$wallet = config('wallet.wallet.table');
3939
$trans = config('wallet.transaction.table');
4040

src/Services/CommonService.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,9 @@ public function multiOperation(Wallet $self, array $operations): array
172172
*/
173173
public function assemble(array $brings): array
174174
{
175-
return DB::transaction(function() use ($brings) {
176-
return $this->multiBrings($brings);
175+
$self = $this;
176+
return DB::transaction(static function() use ($self, $brings) {
177+
return $self->multiBrings($brings);
177178
});
178179
}
179180

src/Traits/CanConfirm.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ trait CanConfirm
1919
*/
2020
public function confirm(Transaction $transaction): bool
2121
{
22-
return DB::transaction(function() use ($transaction) {
22+
$self = $this;
23+
return DB::transaction(static function() use ($self, $transaction) {
2324
$wallet = app(WalletService::class)
24-
->getWallet($this);
25+
->getWallet($self);
2526

2627
if (!$wallet->refreshBalance()) {
2728
// @codeCoverageIgnoreStart
@@ -36,7 +37,7 @@ public function confirm(Transaction $transaction): bool
3637
);
3738
}
3839

39-
return $this->forceConfirm($transaction);
40+
return $self->forceConfirm($transaction);
4041
});
4142
}
4243

@@ -61,10 +62,11 @@ public function safeConfirm(Transaction $transaction): bool
6162
*/
6263
public function forceConfirm(Transaction $transaction): bool
6364
{
64-
return DB::transaction(function() use ($transaction) {
65+
$self = $this;
66+
return DB::transaction(static function() use ($self, $transaction) {
6567

6668
$wallet = app(WalletService::class)
67-
->getWallet($this);
69+
->getWallet($self);
6870

6971
if ($transaction->confirmed) {
7072
throw new ConfirmedInvalid(); // todo

src/Traits/CanExchange.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,13 @@ public function forceExchange(Wallet $to, int $amount, ?array $meta = null): Tra
4949
*/
5050
$from = app(WalletService::class)->getWallet($this);
5151

52-
return DB::transaction(function() use ($from, $to, $amount, $meta) {
52+
return DB::transaction(static function() use ($from, $to, $amount, $meta) {
5353
$rate = app(ExchangeService::class)->rate($from, $to);
5454
$fee = app(WalletService::class)->fee($to, $amount);
5555

5656
$withdraw = app(CommonService::class)
5757
->forceWithdraw($from, $amount + $fee, $meta);
58-
58+
5959
$deposit = app(CommonService::class)
6060
->deposit($to, $amount * $rate, $meta);
6161

src/Traits/CartPay.php

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,12 @@ public function payFreeCart(Cart $cart): array
3131
app(CommonService::class)
3232
->verifyWithdraw($this, 0);
3333

34-
return DB::transaction(function() use ($cart) {
34+
$self = $this;
35+
return DB::transaction(static function() use ($self, $cart) {
3536
$results = [];
3637
foreach ($cart->getItems() as $product) {
3738
$results[] = app(CommonService::class)->forceTransfer(
38-
$this,
39+
$self,
3940
$product,
4041
0,
4142
$product->getMetaProduct(),
@@ -73,12 +74,13 @@ public function payCart(Cart $cart, bool $force = null): array
7374
throw new ProductEnded(trans('wallet::errors.product_stock'));
7475
}
7576

76-
return DB::transaction(function() use ($cart, $force) {
77+
$self = $this;
78+
return DB::transaction(static function() use ($self, $cart, $force) {
7779
$results = [];
7880
foreach ($cart->getItems() as $product) {
7981
if ($force) {
8082
$results[] = app(CommonService::class)->forceTransfer(
81-
$this,
83+
$self,
8284
$product,
8385
$product->getAmountProduct(),
8486
$product->getMetaProduct(),
@@ -89,7 +91,7 @@ public function payCart(Cart $cart, bool $force = null): array
8991
}
9092

9193
$results[] = app(CommonService::class)->transfer(
92-
$this,
94+
$self,
9395
$product,
9496
$product->getAmountProduct(),
9597
$product->getMetaProduct(),
@@ -135,12 +137,13 @@ public function safeRefundCart(Cart $cart, bool $force = null, bool $gifts = nul
135137
*/
136138
public function refundCart(Cart $cart, bool $force = null, bool $gifts = null): bool
137139
{
138-
return DB::transaction(function() use ($cart, $force, $gifts) {
140+
$self = $this;
141+
return DB::transaction(static function() use ($self, $cart, $force, $gifts) {
139142
$results = [];
140-
$transfers = $cart->alreadyBuy($this, $gifts);
143+
$transfers = $cart->alreadyBuy($self, $gifts);
141144
if (count($transfers) !== count($cart)) {
142145
throw (new ModelNotFoundException())
143-
->setModel($this->transfers()->getMorphClass());
146+
->setModel($self->transfers()->getMorphClass());
144147
}
145148

146149
foreach ($cart->getItems() as $key => $product) {

src/Traits/HasWallet.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,10 @@ trait HasWallet
4545
*/
4646
public function deposit(int $amount, ?array $meta = null, bool $confirmed = true): Transaction
4747
{
48-
return DB::transaction(function() use ($amount, $meta, $confirmed) {
48+
$self = $this;
49+
return DB::transaction(static function() use ($self, $amount, $meta, $confirmed) {
4950
return app(CommonService::class)
50-
->deposit($this, $amount, $meta, $confirmed);
51+
->deposit($self, $amount, $meta, $confirmed);
5152
});
5253
}
5354

@@ -160,9 +161,10 @@ public function canWithdraw(int $amount): bool
160161
*/
161162
public function forceWithdraw(int $amount, ?array $meta = null, bool $confirmed = true): Transaction
162163
{
163-
return DB::transaction(function() use ($amount, $meta, $confirmed) {
164+
$self = $this;
165+
return DB::transaction(static function() use ($self, $amount, $meta, $confirmed) {
164166
return app(CommonService::class)
165-
->forceWithdraw($this, $amount, $meta, $confirmed);
167+
->forceWithdraw($self, $amount, $meta, $confirmed);
166168
});
167169
}
168170

@@ -177,9 +179,10 @@ public function forceWithdraw(int $amount, ?array $meta = null, bool $confirmed
177179
*/
178180
public function forceTransfer(Wallet $wallet, int $amount, ?array $meta = null): Transfer
179181
{
180-
return DB::transaction(function() use ($amount, $wallet, $meta) {
182+
$self = $this;
183+
return DB::transaction(static function() use ($self, $amount, $wallet, $meta) {
181184
return app(CommonService::class)
182-
->forceTransfer($this, $wallet, $amount, $meta);
185+
->forceTransfer($self, $wallet, $amount, $meta);
183186
});
184187
}
185188

0 commit comments

Comments
 (0)