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

WalletService::checkAmount()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 2
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\Taxing;
7
use Bavix\Wallet\Interfaces\Wallet;
8
use Bavix\Wallet\Models\Wallet as WalletModel;
9
use Bavix\Wallet\Traits\HasWallet;
10
use function app;
11
12
class WalletService
13
{
14
15
    /**
16
     * Consider the fee that the system will receive.
17
     *
18
     * @param Wallet $wallet
19
     * @param int $amount
20
     * @return int
21
     */
22 20
    public function fee(Wallet $wallet, int $amount): int
23
    {
24 20
        if ($wallet instanceof Taxing) {
25 1
            return (int)($amount * $wallet->getFeePercent() / 100);
26
        }
27
28 20
        return 0;
29
    }
30
31
    /**
32
     * The amount of checks for errors
33
     *
34
     * @param int $amount
35
     * @throws
36
     */
37 35
    public function checkAmount(int $amount): void
38
    {
39 35
        if ($amount < 0) {
40 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

40
            throw new AmountInvalid(/** @scrutinizer ignore-type */ trans('wallet::errors.price_positive'));
Loading history...
41
        }
42 32
    }
43
44
    /**
45
     * @param Wallet $object
46
     * @return WalletModel
47
     */
48 42
    public function getWallet(Wallet $object): WalletModel
49
    {
50 42
        if ($object instanceof WalletModel) {
51 35
            return $object;
52
        }
53
54
        /**
55
         * @var HasWallet $object
56
         */
57 41
        return $object->wallet;
58
    }
59
60
    /**
61
     * @param Wallet $object
62
     * @return int
63
     */
64 42
    public function getBalance(Wallet $object): int
65
    {
66 42
        $wallet = $this->getWallet($object);
67 42
        $wallet->exists or $wallet->save();
68 42
        $proxy = app(ProxyService::class);
69 42
        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

69
        if (!$proxy->has(/** @scrutinizer ignore-type */ $wallet->getKey())) {
Loading history...
70 42
            $proxy->set($wallet->getKey(), (int)($this->attributes['balance'] ?? 0));
0 ignored issues
show
Bug Best Practice introduced by
The property attributes does not exist on Bavix\Wallet\Services\WalletService. Did you maybe forget to declare it?
Loading history...
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

70
            $proxy->set(/** @scrutinizer ignore-type */ $wallet->getKey(), (int)($this->attributes['balance'] ?? 0));
Loading history...
71
        }
72
73 42
        return $proxy[$wallet->getKey()];
74
    }
75
76
    /**
77
     * @param WalletModel $wallet
78
     * @return bool
79
     */
80 2
    public function refresh(WalletModel $wallet): bool
81
    {
82 2
        $balance = $wallet->getAvailableBalance();
83 2
        $wallet->balance = $balance;
84
85 2
        $proxy = app(ProxyService::class);
86 2
        $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

86
        $proxy->set(/** @scrutinizer ignore-type */ $wallet->getKey(), $balance);
Loading history...
87
88 2
        return $wallet->save();
89
    }
90
91
}
92