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

CanConfirm::safeConfirm()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 4
c 1
b 0
f 1
nc 2
nop 1
dl 0
loc 6
ccs 0
cts 6
cp 0
crap 6
rs 10
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
        return DB::transaction(function () use ($transaction) {
23
            $wallet = app(WalletService::class)
24
                ->getWallet($this);
0 ignored issues
show
Bug introduced by
$this 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

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

33
                    /** @scrutinizer ignore-type */ \abs($transaction->amount)
Loading history...
34
                );
35
            }
36
37
            // update balance
38
            return app(CommonService::class)
39
                ->addBalance($wallet, $transaction->amount) &&
0 ignored issues
show
Bug introduced by
It seems like $wallet can also be of type Bavix\Wallet\Traits\HasWallet; however, parameter $wallet of Bavix\Wallet\Services\CommonService::addBalance() does only seem to accept Bavix\Wallet\Interfaces\Wallet, 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

39
                ->addBalance(/** @scrutinizer ignore-type */ $wallet, $transaction->amount) &&
Loading history...
40
                
41
                // confirm
42
                $this->forceConfirm($transaction);
43
        });
44
    }
45
46
    /**
47
     * @param Transaction $transaction
48
     * @return bool
49
     */
50
    public function safeConfirm(Transaction $transaction): bool
51
    {
52
        try {
53
            return $this->confirm($transaction);
54
        } catch (\Throwable $throwable) {
55
            return false;
56
        }
57
    }
58
59
    /**
60
     * @param Transaction $transaction
61
     * @return bool
62
     * @throws ConfirmedInvalid
63
     * @throws WalletOwnerInvalid
64
     */
65
    public function forceConfirm(Transaction $transaction): bool
66
    {
67
        $wallet = app(WalletService::class)
68
            ->getWallet($this);
0 ignored issues
show
Bug introduced by
$this 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
            ->getWallet(/** @scrutinizer ignore-type */ $this);
Loading history...
69
70
        if ($transaction->confirmed) {
71
            throw new ConfirmedInvalid(); // todo
72
        }
73
74
        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...
75
            throw new WalletOwnerInvalid(); // todo
76
        }
77
78
        return $transaction->update(['confirmed' => true]);
79
    }
80
81
}
82