From 576c2b6f2ebcd6fd3e453d8cb4e5f1b5c0cebd00 Mon Sep 17 00:00:00 2001 From: Babichev Maxim Date: Tue, 25 Jun 2019 16:29:17 +0300 Subject: [PATCH] add static --- src/Commands/RefreshBalance.php | 2 +- src/Services/CommonService.php | 5 +++-- src/Traits/CanConfirm.php | 12 +++++++----- src/Traits/CanExchange.php | 4 ++-- src/Traits/CartPay.php | 19 +++++++++++-------- src/Traits/HasWallet.php | 15 +++++++++------ 6 files changed, 33 insertions(+), 24 deletions(-) diff --git a/src/Commands/RefreshBalance.php b/src/Commands/RefreshBalance.php index c95452c9a..74adc1edf 100644 --- a/src/Commands/RefreshBalance.php +++ b/src/Commands/RefreshBalance.php @@ -34,7 +34,7 @@ class RefreshBalance extends Command */ public function handle(): void { - DB::transaction(function() { + DB::transaction(static function() { $wallet = config('wallet.wallet.table'); $trans = config('wallet.transaction.table'); diff --git a/src/Services/CommonService.php b/src/Services/CommonService.php index 5b03357ad..9850e8170 100644 --- a/src/Services/CommonService.php +++ b/src/Services/CommonService.php @@ -172,8 +172,9 @@ public function multiOperation(Wallet $self, array $operations): array */ public function assemble(array $brings): array { - return DB::transaction(function() use ($brings) { - return $this->multiBrings($brings); + $self = $this; + return DB::transaction(static function() use ($self, $brings) { + return $self->multiBrings($brings); }); } diff --git a/src/Traits/CanConfirm.php b/src/Traits/CanConfirm.php index 36874fb32..32b2f6051 100644 --- a/src/Traits/CanConfirm.php +++ b/src/Traits/CanConfirm.php @@ -19,9 +19,10 @@ trait CanConfirm */ public function confirm(Transaction $transaction): bool { - return DB::transaction(function() use ($transaction) { + $self = $this; + return DB::transaction(static function() use ($self, $transaction) { $wallet = app(WalletService::class) - ->getWallet($this); + ->getWallet($self); if (!$wallet->refreshBalance()) { // @codeCoverageIgnoreStart @@ -36,7 +37,7 @@ public function confirm(Transaction $transaction): bool ); } - return $this->forceConfirm($transaction); + return $self->forceConfirm($transaction); }); } @@ -61,10 +62,11 @@ public function safeConfirm(Transaction $transaction): bool */ public function forceConfirm(Transaction $transaction): bool { - return DB::transaction(function() use ($transaction) { + $self = $this; + return DB::transaction(static function() use ($self, $transaction) { $wallet = app(WalletService::class) - ->getWallet($this); + ->getWallet($self); if ($transaction->confirmed) { throw new ConfirmedInvalid(); // todo diff --git a/src/Traits/CanExchange.php b/src/Traits/CanExchange.php index 8ba71a65d..54709a43f 100644 --- a/src/Traits/CanExchange.php +++ b/src/Traits/CanExchange.php @@ -49,13 +49,13 @@ public function forceExchange(Wallet $to, int $amount, ?array $meta = null): Tra */ $from = app(WalletService::class)->getWallet($this); - return DB::transaction(function() use ($from, $to, $amount, $meta) { + return DB::transaction(static function() use ($from, $to, $amount, $meta) { $rate = app(ExchangeService::class)->rate($from, $to); $fee = app(WalletService::class)->fee($to, $amount); $withdraw = app(CommonService::class) ->forceWithdraw($from, $amount + $fee, $meta); - + $deposit = app(CommonService::class) ->deposit($to, $amount * $rate, $meta); diff --git a/src/Traits/CartPay.php b/src/Traits/CartPay.php index eccea856b..4e6db7ce8 100644 --- a/src/Traits/CartPay.php +++ b/src/Traits/CartPay.php @@ -31,11 +31,12 @@ public function payFreeCart(Cart $cart): array app(CommonService::class) ->verifyWithdraw($this, 0); - return DB::transaction(function() use ($cart) { + $self = $this; + return DB::transaction(static function() use ($self, $cart) { $results = []; foreach ($cart->getItems() as $product) { $results[] = app(CommonService::class)->forceTransfer( - $this, + $self, $product, 0, $product->getMetaProduct(), @@ -73,12 +74,13 @@ public function payCart(Cart $cart, bool $force = null): array throw new ProductEnded(trans('wallet::errors.product_stock')); } - return DB::transaction(function() use ($cart, $force) { + $self = $this; + return DB::transaction(static function() use ($self, $cart, $force) { $results = []; foreach ($cart->getItems() as $product) { if ($force) { $results[] = app(CommonService::class)->forceTransfer( - $this, + $self, $product, $product->getAmountProduct(), $product->getMetaProduct(), @@ -89,7 +91,7 @@ public function payCart(Cart $cart, bool $force = null): array } $results[] = app(CommonService::class)->transfer( - $this, + $self, $product, $product->getAmountProduct(), $product->getMetaProduct(), @@ -135,12 +137,13 @@ public function safeRefundCart(Cart $cart, bool $force = null, bool $gifts = nul */ public function refundCart(Cart $cart, bool $force = null, bool $gifts = null): bool { - return DB::transaction(function() use ($cart, $force, $gifts) { + $self = $this; + return DB::transaction(static function() use ($self, $cart, $force, $gifts) { $results = []; - $transfers = $cart->alreadyBuy($this, $gifts); + $transfers = $cart->alreadyBuy($self, $gifts); if (count($transfers) !== count($cart)) { throw (new ModelNotFoundException()) - ->setModel($this->transfers()->getMorphClass()); + ->setModel($self->transfers()->getMorphClass()); } foreach ($cart->getItems() as $key => $product) { diff --git a/src/Traits/HasWallet.php b/src/Traits/HasWallet.php index 00f226118..0caef8f6b 100644 --- a/src/Traits/HasWallet.php +++ b/src/Traits/HasWallet.php @@ -45,9 +45,10 @@ trait HasWallet */ public function deposit(int $amount, ?array $meta = null, bool $confirmed = true): Transaction { - return DB::transaction(function() use ($amount, $meta, $confirmed) { + $self = $this; + return DB::transaction(static function() use ($self, $amount, $meta, $confirmed) { return app(CommonService::class) - ->deposit($this, $amount, $meta, $confirmed); + ->deposit($self, $amount, $meta, $confirmed); }); } @@ -160,9 +161,10 @@ public function canWithdraw(int $amount): bool */ public function forceWithdraw(int $amount, ?array $meta = null, bool $confirmed = true): Transaction { - return DB::transaction(function() use ($amount, $meta, $confirmed) { + $self = $this; + return DB::transaction(static function() use ($self, $amount, $meta, $confirmed) { return app(CommonService::class) - ->forceWithdraw($this, $amount, $meta, $confirmed); + ->forceWithdraw($self, $amount, $meta, $confirmed); }); } @@ -177,9 +179,10 @@ public function forceWithdraw(int $amount, ?array $meta = null, bool $confirmed */ public function forceTransfer(Wallet $wallet, int $amount, ?array $meta = null): Transfer { - return DB::transaction(function() use ($amount, $wallet, $meta) { + $self = $this; + return DB::transaction(static function() use ($self, $amount, $wallet, $meta) { return app(CommonService::class) - ->forceTransfer($this, $wallet, $amount, $meta); + ->forceTransfer($self, $wallet, $amount, $meta); }); }