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

Store   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
dl 0
loc 53
ccs 19
cts 19
cp 1
rs 10
c 1
b 0
f 0
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getBalance() 0 11 3
A incBalance() 0 7 1
A setBalance() 0 5 1
A toInt() 0 3 2
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