Completed
Pull Request — master (#40)
by Бабичев
03:53
created

WalletService::fee()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

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

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

69
        $proxy->set(/** @scrutinizer ignore-type */ $wallet->getKey(), $balance);
Loading history...
70
71 1
        return $wallet->save();
72
    }
73
74
}
75