Passed
Pull Request — master (#51)
by Бабичев
05:07
created

CanConfirm::confirm()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 3
eloc 11
c 2
b 0
f 1
nc 1
nop 1
dl 0
loc 21
ccs 10
cts 10
cp 1
crap 3
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 4
    public function confirm(Transaction $transaction): bool
21
    {
22 4
        $self = $this;
23
        return DB::transaction(static function() use ($self, $transaction) {
24 4
            $wallet = app(WalletService::class)
25 4
                ->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 4
            if (!$wallet->refreshBalance()) {
28
                // @codeCoverageIgnoreStart
29
                return false;
30
                // @codeCoverageIgnoreEnd
31
            }
32
33 4
            if ($transaction->type === Transaction::TYPE_WITHDRAW) {
34 1
                app(CommonService::class)->verifyWithdraw(
35 1
                    $wallet,
36 1
                    \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 3
            return $self->forceConfirm($transaction);
41 4
        });
42
    }
43
44
    /**
45
     * @param Transaction $transaction
46
     * @return bool
47
     */
48 1
    public function safeConfirm(Transaction $transaction): bool
49
    {
50
        try {
51 1
            return $this->confirm($transaction);
52 1
        } catch (\Throwable $throwable) {
53 1
            return false;
54
        }
55
    }
56
57
    /**
58
     * @param Transaction $transaction
59
     * @return bool
60
     * @throws ConfirmedInvalid
61
     * @throws WalletOwnerInvalid
62
     */
63 4
    public function forceConfirm(Transaction $transaction): bool
64
    {
65 4
        $self = $this;
66
        return DB::transaction(static function() use ($self, $transaction) {
67
68 4
            $wallet = app(WalletService::class)
69 4
                ->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 4
            if ($transaction->confirmed) {
72 1
                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 3
            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 1
                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 2
            return $transaction->update(['confirmed' => true]) &&
80
81
                // update balance
82 2
                app(CommonService::class)
83 2
                    ->addBalance($wallet, $transaction->amount);
84
85 4
        });
86
    }
87
88
}
89