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

CanExchange   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 7
Bugs 2 Features 3
Metric Value
eloc 26
c 7
b 2
f 3
dl 0
loc 59
ccs 26
cts 26
cp 1
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A exchange() 0 9 1
A forceExchange() 0 27 1
A safeExchange() 0 6 2
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\WalletService;
11
use Illuminate\Support\Facades\DB;
12
13
trait CanExchange
14
{
15
16
    /**
17
     * @inheritDoc
18
     */
19 2
    public function exchange(Wallet $to, int $amount, ?array $meta = null): Transfer
20
    {
21 2
        $wallet = app(WalletService::class)
22 2
            ->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

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

50
        $from = app(WalletService::class)->getWallet(/** @scrutinizer ignore-type */ $this);
Loading history...
51
52
        return DB::transaction(static function() use ($from, $to, $amount, $meta) {
53 1
            $rate = app(ExchangeService::class)->rate($from, $to);
54 1
            $fee = app(WalletService::class)->fee($to, $amount);
55
56 1
            $withdraw = app(CommonService::class)
57 1
                ->forceWithdraw($from, $amount + $fee, $meta);
58
59 1
            $deposit = app(CommonService::class)
60 1
                ->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

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