Test Failed
Pull Request — master (#83)
by Бабичев
13:48 queued 09:03
created

CanConfirm   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 5
Bugs 2 Features 2
Metric Value
eloc 29
c 5
b 2
f 2
dl 0
loc 75
ccs 0
cts 27
cp 0
rs 10
wmc 9

3 Methods

Rating   Name   Duplication   Size   Complexity  
A forceConfirm() 0 22 4
A safeConfirm() 0 6 2
A confirm() 0 22 3
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\Models\Transaction;
8
use Bavix\Wallet\Services\CommonService;
9
use Bavix\Wallet\Services\LockService;
10
use Bavix\Wallet\Services\WalletService;
11
use Illuminate\Support\Facades\DB;
12
13
trait CanConfirm
14
{
15
16
17
    /**
18
     * @param Transaction $transaction
19
     * @return bool
20
     */
21
    public function confirm(Transaction $transaction): bool
22
    {
23
        return app(LockService::class)->lock($this, __FUNCTION__, function () use ($transaction) {
24
            $self = $this;
25
            return DB::transaction(static function() use ($self, $transaction) {
26
                $wallet = app(WalletService::class)
27
                    ->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
                    ->getWallet(/** @scrutinizer ignore-type */ $self);
Loading history...
28
29
                if (!$wallet->refreshBalance()) {
30
                    // @codeCoverageIgnoreStart
31
                    return false;
32
                    // @codeCoverageIgnoreEnd
33
                }
34
35
                if ($transaction->type === Transaction::TYPE_WITHDRAW) {
36
                    app(CommonService::class)->verifyWithdraw(
37
                        $wallet,
38
                        \abs($transaction->amount)
0 ignored issues
show
Bug introduced by
It seems like abs($transaction->amount) can also be of type double; however, parameter $amount of Bavix\Wallet\Services\Co...rvice::verifyWithdraw() does only seem to accept integer, 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

38
                        /** @scrutinizer ignore-type */ \abs($transaction->amount)
Loading history...
39
                    );
40
                }
41
42
                return $self->forceConfirm($transaction);
43
            });
44
        });
45
    }
46
47
    /**
48
     * @param Transaction $transaction
49
     * @return bool
50
     */
51
    public function safeConfirm(Transaction $transaction): bool
52
    {
53
        try {
54
            return $this->confirm($transaction);
55
        } catch (\Throwable $throwable) {
56
            return false;
57
        }
58
    }
59
60
    /**
61
     * @param Transaction $transaction
62
     * @return bool
63
     * @throws ConfirmedInvalid
64
     * @throws WalletOwnerInvalid
65
     */
66
    public function forceConfirm(Transaction $transaction): bool
67
    {
68
        return app(LockService::class)->lock($this, __FUNCTION__, function () use ($transaction) {
69
            $self = $this;
70
            return DB::transaction(static function () use ($self, $transaction) {
71
72
                $wallet = app(WalletService::class)
73
                    ->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

73
                    ->getWallet(/** @scrutinizer ignore-type */ $self);
Loading history...
74
75
                if ($transaction->confirmed) {
76
                    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; 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

76
                    throw new ConfirmedInvalid(/** @scrutinizer ignore-type */ trans('wallet::errors.confirmed_invalid'));
Loading history...
77
                }
78
79
                if ($wallet->id !== $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...
80
                    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; 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

80
                    throw new WalletOwnerInvalid(/** @scrutinizer ignore-type */ trans('wallet::errors.owner_invalid'));
Loading history...
81
                }
82
83
                return $transaction->update(['confirmed' => true]) &&
84
85
                    // update balance
86
                    app(CommonService::class)
87
                        ->addBalance($wallet, $transaction->amount);
88
89
            });
90
        });
91
    }
92
93
}
94