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

CanExchange   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Test Coverage

Coverage 34.62%

Importance

Changes 7
Bugs 2 Features 3
Metric Value
eloc 27
c 7
b 2
f 3
dl 0
loc 60
ccs 9
cts 26
cp 0.3462
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A safeExchange() 0 6 2
A exchange() 0 9 1
A forceExchange() 0 28 1
1
<?php
2
3
namespace Bavix\Wallet\Traits;
4
5
use Bavix\Wallet\Interfaces\Wallet;
6
use Bavix\Wallet\Models\Transfer;
7
use Bavix\Wallet\Objects\Bring;
8
use Bavix\Wallet\Services\CommonService;
9
use Bavix\Wallet\Services\ExchangeService;
10
use Bavix\Wallet\Services\LockService;
11
use Bavix\Wallet\Services\WalletService;
12
use Illuminate\Support\Facades\DB;
13
14
trait CanExchange
15
{
16
17
    /**
18
     * @inheritDoc
19
     */
20 1
    public function exchange(Wallet $to, int $amount, ?array $meta = null): Transfer
21
    {
22 1
        $wallet = app(WalletService::class)
23 1
            ->getWallet($this);
0 ignored issues
show
Bug introduced by
$this of type Bavix\Wallet\Traits\CanExchange 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

23
            ->getWallet(/** @scrutinizer ignore-type */ $this);
Loading history...
24
25 1
        app(CommonService::class)
26 1
            ->verifyWithdraw($wallet, $amount);
27
28
        return $this->forceExchange($to, $amount, $meta);
29
    }
30
31
    /**
32
     * @inheritDoc
33
     */
34 1
    public function safeExchange(Wallet $to, int $amount, ?array $meta = null): ?Transfer
35
    {
36
        try {
37 1
            return $this->exchange($to, $amount, $meta);
38 1
        } catch (\Throwable $throwable) {
39 1
            return null;
40
        }
41
    }
42
43
    /**
44
     * @inheritDoc
45
     */
46
    public function forceExchange(Wallet $to, int $amount, ?array $meta = null): Transfer
47
    {
48
        /**
49
         * @var Wallet $from
50
         */
51
        $from = app(WalletService::class)->getWallet($this);
0 ignored issues
show
Bug introduced by
$this of type Bavix\Wallet\Traits\CanExchange 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

51
        $from = app(WalletService::class)->getWallet(/** @scrutinizer ignore-type */ $this);
Loading history...
52
53
        return app(LockService::class)->lock($this, __FUNCTION__, function () use ($from, $to, $amount, $meta) {
54
            return DB::transaction(static function () use ($from, $to, $amount, $meta) {
55
                $rate = app(ExchangeService::class)->rate($from, $to);
56
                $fee = app(WalletService::class)->fee($to, $amount);
57
58
                $withdraw = app(CommonService::class)
59
                    ->forceWithdraw($from, $amount + $fee, $meta);
60
61
                $deposit = app(CommonService::class)
62
                    ->deposit($to, $amount * $rate, $meta);
0 ignored issues
show
Bug introduced by
$amount * $rate of type double is incompatible with the type integer expected by parameter $amount of Bavix\Wallet\Services\CommonService::deposit(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

62
                    ->deposit($to, /** @scrutinizer ignore-type */ $amount * $rate, $meta);
Loading history...
63
64
                $transfers = app(CommonService::class)->multiBrings([
65
                    (new Bring())
66
                        ->setStatus(Transfer::STATUS_EXCHANGE)
67
                        ->setDeposit($deposit)
68
                        ->setWithdraw($withdraw)
69
                        ->setFrom($from)
70
                        ->setTo($to)
71
                ]);
72
73
                return current($transfers);
74
            });
75
        });
76
    }
77
78
}
79