Completed
Pull Request — master (#51)
by Бабичев
09:33 queued 02:25
created

CanExchange::exchange()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 1
eloc 5
c 2
b 0
f 1
nc 1
nop 3
dl 0
loc 9
ccs 6
cts 6
cp 1
crap 1
rs 10
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