Passed
Push — master ( 6514fd...09b598 )
by Бабичев
01:51 queued 12s
created

CanConfirm::safeResetConfirm()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 2
rs 10
c 0
b 0
f 0
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
18
    /**
19
     * @param Transaction $transaction
20
     * @return bool
21
     */
22 11
    public function confirm(Transaction $transaction): bool
23
    {
24
        return app(LockService::class)->lock($this, __FUNCTION__, function () use ($transaction) {
25 11
            $self = $this;
26
            return app(DbService::class)->transaction(static function () use ($self, $transaction) {
27 11
                $wallet = app(WalletService::class)->getWallet($self);
0 ignored issues
show
Bug introduced by
$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 ignore-type  annotation

27
                $wallet = app(WalletService::class)->getWallet(/** @scrutinizer ignore-type */ $self);
Loading history...
28 11
                if (!$wallet->refreshBalance()) {
29 1
                    return false;
30
                }
31
32 10
                if ($transaction->type === Transaction::TYPE_WITHDRAW) {
33 2
                    app(CommonService::class)->verifyWithdraw(
34 2
                        $wallet,
35 2
                        app(Mathable::class)->abs($transaction->amount)
0 ignored issues
show
Bug introduced by
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 ignore-type  annotation

35
                        /** @scrutinizer ignore-type */ app(Mathable::class)->abs($transaction->amount)
Loading history...
36
                    );
37
                }
38
39 8
                return $self->forceConfirm($transaction);
40 11
            });
41 11
        });
42
    }
43
44
    /**
45
     * @param Transaction $transaction
46
     * @return bool
47
     */
48 3
    public function safeConfirm(Transaction $transaction): bool
49
    {
50
        try {
51 3
            return $this->confirm($transaction);
52 2
        } catch (\Throwable $throwable) {
53 2
            return false;
54
        }
55
    }
56
57
    /**
58
     * Removal of confirmation (forced), use at your own peril and risk.
59
     *
60
     * @param Transaction $transaction
61
     * @return bool
62
     */
63 9
    public function resetConfirm(Transaction $transaction): bool
64
    {
65
        return app(LockService::class)->lock($this, __FUNCTION__, function () use ($transaction) {
66 9
            $self = $this;
67
            return app(DbService::class)->transaction(static function () use ($self, $transaction) {
68 9
                $wallet = app(WalletService::class)->getWallet($self);
0 ignored issues
show
Bug introduced by
$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 ignore-type  annotation

68
                $wallet = app(WalletService::class)->getWallet(/** @scrutinizer ignore-type */ $self);
Loading history...
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
Bug introduced by
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 ignore-type  annotation

74
                    throw new ConfirmedInvalid(/** @scrutinizer ignore-type */ trans('wallet::errors.unconfirmed_invalid'));
Loading history...
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
            return app(DbService::class)->transaction(static function () use ($self, $transaction) {
110
111 10
                $wallet = app(WalletService::class)
112 10
                    ->getWallet($self);
0 ignored issues
show
Bug introduced by
$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 ignore-type  annotation

112
                    ->getWallet(/** @scrutinizer ignore-type */ $self);
Loading history...
113
114 10
                if ($transaction->confirmed) {
115 2
                    throw new ConfirmedInvalid(trans('wallet::errors.confirmed_invalid'));
0 ignored issues
show
Bug introduced by
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 ignore-type  annotation

115
                    throw new ConfirmedInvalid(/** @scrutinizer ignore-type */ trans('wallet::errors.confirmed_invalid'));
Loading history...
116
                }
117
118 8
                if ($wallet->getKey() !== $transaction->wallet_id) {
0 ignored issues
show
Bug introduced by
The property wallet_id does not exist on Bavix\Wallet\Models\Transaction. Did you mean wallet?
Loading history...
119 2
                    throw new WalletOwnerInvalid(trans('wallet::errors.owner_invalid'));
0 ignored issues
show
Bug introduced by
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 ignore-type  annotation

119
                    throw new WalletOwnerInvalid(/** @scrutinizer ignore-type */ trans('wallet::errors.owner_invalid'));
Loading history...
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
128 10
            });
129 10
        });
130
    }
131
132
}
133