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

WalletService   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
eloc 13
dl 0
loc 61
ccs 18
cts 18
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A refresh() 0 9 1
A fee() 0 7 2
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\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 18
    public function fee(Wallet $wallet, int $amount): int
23
    {
24 18
        if ($wallet instanceof Taxing) {
25 1
            return (int)($amount * $wallet->getFeePercent() / 100);
26
        }
27
28 18
        return 0;
29
    }
30
31
    /**
32
     * The amount of checks for errors
33
     *
34
     * @param int $amount
35
     * @throws
36
     */
37 33
    public function checkAmount(int $amount): void
38
    {
39 33
        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 30
    }
43
44
    /**
45
     * @param Wallet $object
46
     * @return WalletModel
47
     */
48 30
    public function getWallet(Wallet $object): WalletModel
49
    {
50 30
        if ($object instanceof WalletModel) {
51 14
            return $object;
52
        }
53
54
        /**
55
         * @var HasWallet $object
56
         */
57 24
        return $object->wallet;
58
    }
59
60
    /**
61
     * @param WalletModel $wallet
62
     * @return bool
63
     */
64 2
    public function refresh(WalletModel $wallet): bool
65
    {
66 2
        $balance = $wallet->getAvailableBalance();
67 2
        $wallet->balance = $balance;
68
69 2
        $proxy = app(ProxyService::class);
70 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

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