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

WalletService::getWallet()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 16
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 4
nop 1
dl 0
loc 16
ccs 6
cts 6
cp 1
crap 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Bavix\Wallet\Services;
4
5
use Bavix\Wallet\Exceptions\AmountInvalid;
6
use Bavix\Wallet\Interfaces\MinimalTaxable;
7
use Bavix\Wallet\Interfaces\Taxable;
8
use Bavix\Wallet\Interfaces\Wallet;
9
use Bavix\Wallet\Models\Wallet as WalletModel;
10
use Bavix\Wallet\Traits\HasWallet;
11
use function app;
12
13
class WalletService
14
{
15
16
    /**
17
     * @param Wallet $object
18
     * @return int
19
     */
20 9
    public function decimalPlaces(Wallet $object): int
21
    {
22 9
        $decimalPlaces = $this->getWallet($object)->decimal_places ?: 2;
23 9
        return 10 ** $decimalPlaces;
24
    }
25
26
    /**
27
     * Consider the fee that the system will receive.
28
     *
29
     * @param Wallet $wallet
30
     * @param int $amount
31
     * @return int
32
     */
33 22
    public function fee(Wallet $wallet, int $amount): int
34
    {
35 22
        $result = 0;
36
37 22
        if ($wallet instanceof Taxable) {
38 2
            $result = (int) ($amount * $wallet->getFeePercent() / 100);
39
40
            /**
41
             * Added minimum commission condition
42
             *
43
             * @see https://github.com/bavix/laravel-wallet/issues/64#issuecomment-514483143
44
             */
45 2
            if ($wallet instanceof MinimalTaxable) {
46 1
                $minimal = $wallet->getMinimalFee();
47 1
                if ($result < $minimal) {
48 1
                    $result = $wallet->getMinimalFee();
49
                }
50
            }
51
        }
52
53 22
        return $result;
54
    }
55
56
    /**
57
     * The amount of checks for errors
58
     *
59
     * @param int $amount
60
     * @throws
61
     */
62 44
    public function checkAmount(int $amount): void
63
    {
64 44
        if ($amount < 0) {
65 3
            throw new AmountInvalid(trans('wallet::errors.price_positive'));
0 ignored issues
show
Bug introduced by
It seems like trans('wallet::errors.price_positive') can also be of type array; however, parameter $message of Bavix\Wallet\Exceptions\...tInvalid::__construct() does only seem to accept string, 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

65
            throw new AmountInvalid(/** @scrutinizer ignore-type */ trans('wallet::errors.price_positive'));
Loading history...
66
        }
67 41
    }
68
69
    /**
70
     * @param Wallet $object
71
     * @return WalletModel
72
     */
73 56
    public function getWallet(Wallet $object): WalletModel
74
    {
75
        /**
76
         * @var WalletModel $wallet
77
         */
78 56
        $wallet = $object;
79
80 56
        if (!($object instanceof WalletModel)) {
81
            /**
82
             * @var HasWallet $object
83
             */
84 49
            $wallet = $object->wallet;
85
        }
86
87 56
        $wallet->exists or $wallet->save();
88 56
        return $wallet;
89
    }
90
91
    /**
92
     * @param Wallet $object
93
     * @return int
94
     */
95 55
    public function getBalance(Wallet $object): int
96
    {
97 55
        $wallet = $this->getWallet($object);
98 55
        $proxy = app(ProxyService::class);
99 55
        if (!$proxy->has($wallet->getKey())) {
0 ignored issues
show
Bug introduced by
It seems like $wallet->getKey() can also be of type boolean and null; however, parameter $key of Bavix\Wallet\Services\ProxyService::has() does only seem to accept string, 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

99
        if (!$proxy->has(/** @scrutinizer ignore-type */ $wallet->getKey())) {
Loading history...
100 55
            $proxy->set($wallet->getKey(), (int) $wallet->getOriginal('balance', 0));
0 ignored issues
show
Bug introduced by
It seems like $wallet->getKey() can also be of type boolean and null; however, parameter $key of Bavix\Wallet\Services\ProxyService::set() does only seem to accept string, 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

100
            $proxy->set(/** @scrutinizer ignore-type */ $wallet->getKey(), (int) $wallet->getOriginal('balance', 0));
Loading history...
101
        }
102
103 55
        return $proxy[$wallet->getKey()];
104
    }
105
106
    /**
107
     * @param WalletModel $wallet
108
     * @return bool
109
     */
110 8
    public function refresh(WalletModel $wallet): bool
111
    {
112 8
        $this->getBalance($wallet);
113 8
        $balance = $wallet->getAvailableBalance();
114 8
        $wallet->balance = $balance;
115
116 8
        $proxy = app(ProxyService::class);
117 8
        $proxy->set($wallet->getKey(), $balance);
0 ignored issues
show
Bug introduced by
It seems like $wallet->getKey() can also be of type boolean and null; however, parameter $key of Bavix\Wallet\Services\ProxyService::set() does only seem to accept string, 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

117
        $proxy->set(/** @scrutinizer ignore-type */ $wallet->getKey(), $balance);
Loading history...
118
119 8
        return $wallet->save();
120
    }
121
122
}
123