Passed
Push — master ( 40d7f4...7a5fab )
by Бабичев
02:24 queued 11s
created

Store::toInt()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Bavix\Wallet\Simple;
4
5
use Bavix\Wallet\Interfaces\Mathable;
6
use Bavix\Wallet\Interfaces\Storable;
7
use Bavix\Wallet\Services\WalletService;
8
9
class Store implements Storable
10
{
11
12
    /**
13
     * @var array
14
     */
15
    protected $balanceSheets = [];
16
17
    /**
18
     * @inheritDoc
19
     */
20 120
    public function getBalance($object)
21
    {
22 120
        $wallet = app(WalletService::class)->getWallet($object);
23 120
        if (!\array_key_exists($wallet->getKey(), $this->balanceSheets)) {
24 120
            $balance = method_exists($wallet, 'getRawOriginal') ?
25 120
                $wallet->getRawOriginal('balance', 0) : $wallet->getOriginal('balance', 0);
26
27 120
            $this->balanceSheets[$wallet->getKey()] = $this->toInt($balance);
0 ignored issues
show
Bug introduced by
It seems like $balance can also be of type array; however, parameter $balance of Bavix\Wallet\Simple\Store::toInt() 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

27
            $this->balanceSheets[$wallet->getKey()] = $this->toInt(/** @scrutinizer ignore-type */ $balance);
Loading history...
28
        }
29
30 120
        return $this->balanceSheets[$wallet->getKey()];
31
    }
32
33
    /**
34
     * @inheritDoc
35
     */
36 103
    public function incBalance($object, $amount)
37
    {
38 103
        $math = app(Mathable::class);
39 103
        $balance = $math->add($this->getBalance($object), $amount);
40 103
        $balance = $this->toInt($balance);
41 103
        $this->setBalance($object, $balance);
42 103
        return $balance;
43
    }
44
45
    /**
46
     * @inheritDoc
47
     */
48 103
    public function setBalance($object, $amount): bool
49
    {
50 103
        $wallet = app(WalletService::class)->getWallet($object);
51 103
        $this->balanceSheets[$wallet->getKey()] = $this->toInt($amount);
52 103
        return true;
53
    }
54
55
    /**
56
     * @param string $balance
57
     * @return string
58
     */
59 120
    protected function toInt($balance): string
60
    {
61 120
        return app(Mathable::class)->round($balance ?: 0);
62
    }
63
64
}
65