Test Failed
Pull Request — master (#83)
by Бабичев
07:00
created

CanConfirm::forceConfirm()   A

Complexity

Conditions 4
Paths 1

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 3
Bugs 1 Features 2
Metric Value
cc 4
eloc 11
c 3
b 1
f 2
nc 1
nop 1
dl 0
loc 21
ccs 0
cts 11
cp 0
crap 20
rs 9.9
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\WalletService;
10
use Illuminate\Support\Facades\DB;
11
12
trait CanConfirm
13
{
14
15
16
    /**
17
     * @param Transaction $transaction
18
     * @return bool
19
     */
20
    public function confirm(Transaction $transaction): bool
21
    {
22
        $self = $this;
23
        return DB::transaction(static function() use ($self, $transaction) {
24
            $wallet = app(WalletService::class)
25
                ->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

25
                ->getWallet(/** @scrutinizer ignore-type */ $self);
Loading history...
26
27
            if (!$wallet->refreshBalance()) {
28
                // @codeCoverageIgnoreStart
29
                return false;
30
                // @codeCoverageIgnoreEnd
31
            }
32
33
            if ($transaction->type === Transaction::TYPE_WITHDRAW) {
34
                app(CommonService::class)->verifyWithdraw(
35
                    $wallet,
36
                    \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

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

69
                ->getWallet(/** @scrutinizer ignore-type */ $self);
Loading history...
70
71
            if ($transaction->confirmed) {
72
                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

72
                throw new ConfirmedInvalid(/** @scrutinizer ignore-type */ trans('wallet::errors.confirmed_invalid'));
Loading history...
73
            }
74
75
            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...
76
                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

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