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

WalletService   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 27
c 1
b 0
f 0
dl 0
loc 102
ccs 34
cts 34
cp 1
rs 10
wmc 14

6 Methods

Rating   Name   Duplication   Size   Complexity  
A refresh() 0 10 1
A getBalance() 0 10 3
A decimalPlaces() 0 4 2
A fee() 0 21 4
A getWallet() 0 10 2
A checkAmount() 0 4 2
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 43
    public function checkAmount(int $amount): void
63
    {
64 43
        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 40
    }
68
69
    /**
70
     * @param Wallet $object
71
     * @return WalletModel
72
     */
73 55
    public function getWallet(Wallet $object): WalletModel
74
    {
75 55
        if ($object instanceof WalletModel) {
76 44
            return $object;
77
        }
78
79
        /**
80
         * @var HasWallet $object
81
         */
82 48
        return $object->wallet;
83
    }
84
85
    /**
86
     * @param Wallet $object
87
     * @return int
88
     */
89 54
    public function getBalance(Wallet $object): int
90
    {
91 54
        $wallet = $this->getWallet($object);
92 54
        $wallet->exists or $wallet->save();
93 54
        $proxy = app(ProxyService::class);
94 54
        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

94
        if (!$proxy->has(/** @scrutinizer ignore-type */ $wallet->getKey())) {
Loading history...
95 54
            $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

95
            $proxy->set(/** @scrutinizer ignore-type */ $wallet->getKey(), (int) $wallet->getOriginal('balance', 0));
Loading history...
96
        }
97
98 54
        return $proxy[$wallet->getKey()];
99
    }
100
101
    /**
102
     * @param WalletModel $wallet
103
     * @return bool
104
     */
105 7
    public function refresh(WalletModel $wallet): bool
106
    {
107 7
        $this->getBalance($wallet);
108 7
        $balance = $wallet->getAvailableBalance();
109 7
        $wallet->balance = $balance;
110
111 7
        $proxy = app(ProxyService::class);
112 7
        $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

112
        $proxy->set(/** @scrutinizer ignore-type */ $wallet->getKey(), $balance);
Loading history...
113
114 7
        return $wallet->save();
115
    }
116
117
}
118