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

CanExchange   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 4
Bugs 1 Features 2
Metric Value
eloc 21
c 4
b 1
f 2
dl 0
loc 50
ccs 0
cts 21
cp 0
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A exchange() 0 4 1
A forceExchange() 0 23 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
    public function exchange(Wallet $to, int $amount): Transfer
20
    {
21
        app(WalletService::class)->checkAmount($amount);
22
        return $this->forceExchange($to, $amount);
23
    }
24
25
    /**
26
     * @inheritDoc
27
     */
28
    public function safeExchange(Wallet $to, int $amount): ?Transfer
29
    {
30
       try {
31
           return $this->exchange($to, $amount);
32
       } catch (\Throwable $throwable) {
33
           return null;
34
       }
35
    }
36
37
    /**
38
     * @inheritDoc
39
     */
40
    public function forceExchange(Wallet $to, int $amount): Transfer
41
    {
42
        /**
43
         * @var Wallet $from
44
         */
45
        $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

45
        $from = app(WalletService::class)->getWallet(/** @scrutinizer ignore-type */ $this);
Loading history...
46
47
        return DB::transaction(function () use ($from, $to, $amount) {
48
            $rate = app(ExchangeService::class)->rate($from, $to);
49
            $fee = app(WalletService::class)->fee($to, $amount);
50
            $withdraw = $from->forceWithdraw($amount + $fee);
51
            $deposit = $to->deposit($amount * $rate);
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\Interfaces\Wallet::deposit(). ( Ignorable by Annotation )

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

51
            $deposit = $to->deposit(/** @scrutinizer ignore-type */ $amount * $rate);
Loading history...
52
53
            $transfers = app(CommonService::class)->multiBrings([
54
                (new Bring())
55
                    ->setStatus(Transfer::STATUS_EXCHANGE)
56
                    ->setDeposit($deposit)
57
                    ->setWithdraw($withdraw)
58
                    ->setFrom($from)
59
                    ->setTo($to)
60
            ]);
61
62
            return current($transfers);
63
        });
64
    }
65
66
}
67