1 | <?php |
||||
2 | |||||
3 | namespace Bavix\Wallet\Traits; |
||||
4 | |||||
5 | use Bavix\Wallet\Exceptions\ConfirmedInvalid; |
||||
6 | use Bavix\Wallet\Exceptions\WalletOwnerInvalid; |
||||
7 | use Bavix\Wallet\Interfaces\Mathable; |
||||
8 | use Bavix\Wallet\Models\Transaction; |
||||
9 | use Bavix\Wallet\Services\CommonService; |
||||
10 | use Bavix\Wallet\Services\DbService; |
||||
11 | use Bavix\Wallet\Services\LockService; |
||||
12 | use Bavix\Wallet\Services\WalletService; |
||||
13 | |||||
14 | trait CanConfirm |
||||
15 | { |
||||
16 | /** |
||||
17 | * @param Transaction $transaction |
||||
18 | * @return bool |
||||
19 | */ |
||||
20 | 11 | public function confirm(Transaction $transaction): bool |
|||
21 | { |
||||
22 | return app(LockService::class)->lock($this, __FUNCTION__, function () use ($transaction) { |
||||
23 | 11 | $self = $this; |
|||
24 | |||||
25 | return app(DbService::class)->transaction(static function () use ($self, $transaction) { |
||||
26 | 11 | $wallet = app(WalletService::class)->getWallet($self); |
|||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||
27 | 11 | if (! $wallet->refreshBalance()) { |
|||
28 | 1 | return false; |
|||
29 | } |
||||
30 | |||||
31 | 10 | if ($transaction->type === Transaction::TYPE_WITHDRAW) { |
|||
32 | 2 | app(CommonService::class)->verifyWithdraw( |
|||
33 | 2 | $wallet, |
|||
34 | 2 | app(Mathable::class)->abs($transaction->amount) |
|||
0 ignored issues
–
show
app(Bavix\Wallet\Interfa...s($transaction->amount) of type string is incompatible with the type integer expected by parameter $amount of Bavix\Wallet\Services\Co...rvice::verifyWithdraw() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
35 | ); |
||||
36 | } |
||||
37 | |||||
38 | 8 | return $self->forceConfirm($transaction); |
|||
39 | 11 | }); |
|||
40 | 11 | }); |
|||
41 | } |
||||
42 | |||||
43 | /** |
||||
44 | * @param Transaction $transaction |
||||
45 | * @return bool |
||||
46 | */ |
||||
47 | 3 | public function safeConfirm(Transaction $transaction): bool |
|||
48 | { |
||||
49 | try { |
||||
50 | 3 | return $this->confirm($transaction); |
|||
51 | 2 | } catch (\Throwable $throwable) { |
|||
52 | 2 | return false; |
|||
53 | } |
||||
54 | } |
||||
55 | |||||
56 | /** |
||||
57 | * Removal of confirmation (forced), use at your own peril and risk. |
||||
58 | * |
||||
59 | * @param Transaction $transaction |
||||
60 | * @return bool |
||||
61 | */ |
||||
62 | 9 | public function resetConfirm(Transaction $transaction): bool |
|||
63 | { |
||||
64 | return app(LockService::class)->lock($this, __FUNCTION__, function () use ($transaction) { |
||||
65 | 9 | $self = $this; |
|||
66 | |||||
67 | return app(DbService::class)->transaction(static function () use ($self, $transaction) { |
||||
68 | 9 | $wallet = app(WalletService::class)->getWallet($self); |
|||
0 ignored issues
–
show
$self of type Bavix\Wallet\Traits\CanConfirm is incompatible with the type Bavix\Wallet\Interfaces\Wallet expected by parameter $object of Bavix\Wallet\Services\WalletService::getWallet() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
69 | 9 | if (! $wallet->refreshBalance()) { |
|||
70 | 1 | return false; |
|||
71 | } |
||||
72 | |||||
73 | 8 | if (! $transaction->confirmed) { |
|||
74 | 4 | throw new ConfirmedInvalid(trans('wallet::errors.unconfirmed_invalid')); |
|||
0 ignored issues
–
show
It seems like
trans('wallet::errors.unconfirmed_invalid') can also be of type array and array ; however, parameter $message of Bavix\Wallet\Exceptions\...dInvalid::__construct() does only seem to accept string , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
75 | } |
||||
76 | |||||
77 | 4 | return $transaction->update(['confirmed' => false]) && |
|||
78 | |||||
79 | // update balance |
||||
80 | 4 | app(CommonService::class) |
|||
81 | 4 | ->addBalance($wallet, -$transaction->amount); |
|||
82 | 9 | }); |
|||
83 | 9 | }); |
|||
84 | } |
||||
85 | |||||
86 | /** |
||||
87 | * @param Transaction $transaction |
||||
88 | * @return bool |
||||
89 | */ |
||||
90 | 5 | public function safeResetConfirm(Transaction $transaction): bool |
|||
91 | { |
||||
92 | try { |
||||
93 | 5 | return $this->resetConfirm($transaction); |
|||
94 | 2 | } catch (\Throwable $throwable) { |
|||
95 | 2 | return false; |
|||
96 | } |
||||
97 | } |
||||
98 | |||||
99 | /** |
||||
100 | * @param Transaction $transaction |
||||
101 | * @return bool |
||||
102 | * @throws ConfirmedInvalid |
||||
103 | * @throws WalletOwnerInvalid |
||||
104 | */ |
||||
105 | 10 | public function forceConfirm(Transaction $transaction): bool |
|||
106 | { |
||||
107 | return app(LockService::class)->lock($this, __FUNCTION__, function () use ($transaction) { |
||||
108 | 10 | $self = $this; |
|||
109 | |||||
110 | return app(DbService::class)->transaction(static function () use ($self, $transaction) { |
||||
111 | 10 | $wallet = app(WalletService::class) |
|||
112 | 10 | ->getWallet($self); |
|||
0 ignored issues
–
show
$self of type Bavix\Wallet\Traits\CanConfirm is incompatible with the type Bavix\Wallet\Interfaces\Wallet expected by parameter $object of Bavix\Wallet\Services\WalletService::getWallet() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
113 | |||||
114 | 10 | if ($transaction->confirmed) { |
|||
115 | 2 | throw new ConfirmedInvalid(trans('wallet::errors.confirmed_invalid')); |
|||
0 ignored issues
–
show
It seems like
trans('wallet::errors.confirmed_invalid') can also be of type array and array ; however, parameter $message of Bavix\Wallet\Exceptions\...dInvalid::__construct() does only seem to accept string , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
116 | } |
||||
117 | |||||
118 | 8 | if ($wallet->getKey() !== $transaction->wallet_id) { |
|||
119 | 2 | throw new WalletOwnerInvalid(trans('wallet::errors.owner_invalid')); |
|||
0 ignored issues
–
show
It seems like
trans('wallet::errors.owner_invalid') can also be of type array and array ; however, parameter $message of Bavix\Wallet\Exceptions\...rInvalid::__construct() does only seem to accept string , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
120 | } |
||||
121 | |||||
122 | 6 | return $transaction->update(['confirmed' => true]) && |
|||
123 | |||||
124 | // update balance |
||||
125 | 6 | app(CommonService::class) |
|||
126 | 6 | ->addBalance($wallet, $transaction->amount); |
|||
127 | 10 | }); |
|||
128 | 10 | }); |
|||
129 | } |
||||
130 | } |
||||
131 |