Passed
Pull Request — master (#192)
by Бабичев
07:57 queued 02:48
created

CanConfirm   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Test Coverage

Coverage 91.11%

Importance

Changes 6
Bugs 2 Features 2
Metric Value
eloc 45
dl 0
loc 117
ccs 41
cts 45
cp 0.9111
rs 10
c 6
b 2
f 2
wmc 16

5 Methods

Rating   Name   Duplication   Size   Complexity  
A safeConfirm() 0 6 2
A forceConfirm() 0 22 4
A confirm() 0 18 3
A safeResetConfirm() 0 6 2
A resetConfirm() 0 23 5
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 2
    public function safeConfirm(Transaction $transaction): bool
49
    {
50
        try {
51 2
            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 5
    public function resetConfirm(Transaction $transaction): bool
64
    {
65
        return app(LockService::class)->lock($this, __FUNCTION__, function () use ($transaction) {
66 5
            $self = $this;
67
            return app(DbService::class)->transaction(static function () use ($self, $transaction) {
68 5
                $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 5
                if (!$wallet->refreshBalance()) {
70 1
                    return false;
71
                }
72
73 4
                if (!$transaction->confirmed) {
74
                    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
                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...
78
                    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

78
                    throw new WalletOwnerInvalid(/** @scrutinizer ignore-type */ trans('wallet::errors.owner_invalid'));
Loading history...
79
                }
80
81 4
                return $transaction->update(['confirmed' => false]) &&
82
83
                    // update balance
84 4
                    app(CommonService::class)
85 4
                        ->addBalance($wallet, -$transaction->amount);
86 5
            });
87 5
        });
88
    }
89
90
    /**
91
     * @param Transaction $transaction
92
     * @return bool
93
     */
94 2
    public function safeResetConfirm(Transaction $transaction): bool
95
    {
96
        try {
97 2
            return $this->resetConfirm($transaction);
98
        } catch (\Throwable $throwable) {
99
            return false;
100
        }
101
    }
102
103
    /**
104
     * @param Transaction $transaction
105
     * @return bool
106
     * @throws ConfirmedInvalid
107
     * @throws WalletOwnerInvalid
108
     */
109 10
    public function forceConfirm(Transaction $transaction): bool
110
    {
111
        return app(LockService::class)->lock($this, __FUNCTION__, function () use ($transaction) {
112 10
            $self = $this;
113
            return app(DbService::class)->transaction(static function () use ($self, $transaction) {
114
115 10
                $wallet = app(WalletService::class)
116 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

116
                    ->getWallet(/** @scrutinizer ignore-type */ $self);
Loading history...
117
118 10
                if ($transaction->confirmed) {
119 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

119
                    throw new ConfirmedInvalid(/** @scrutinizer ignore-type */ trans('wallet::errors.confirmed_invalid'));
Loading history...
120
                }
121
122 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...
123 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

123
                    throw new WalletOwnerInvalid(/** @scrutinizer ignore-type */ trans('wallet::errors.owner_invalid'));
Loading history...
124
                }
125
126 6
                return $transaction->update(['confirmed' => true]) &&
127
128
                    // update balance
129 6
                    app(CommonService::class)
130 6
                        ->addBalance($wallet, $transaction->amount);
131
132 10
            });
133 10
        });
134
    }
135
136
}
137