Passed
Branch master (8e9667)
by Бабичев
09:33
created

Store::getBalance()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 8
ccs 5
cts 5
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\Storable;
6
use Bavix\Wallet\Services\WalletService;
7
8
class Store implements Storable
9
{
10
11
    /**
12
     * @var array
13
     */
14
    protected $balanceSheets = [];
15
16
    /**
17
     * @inheritDoc
18
     */
19 90
    public function getBalance($object): int
20
    {
21 90
        $wallet = app(WalletService::class)->getWallet($object);
22 90
        if (!\array_key_exists($wallet->getKey(), $this->balanceSheets)) {
23 90
            $this->balanceSheets[$wallet->getKey()] = (int)$wallet->getOriginal('balance', 0);
24
        }
25
26 90
        return $this->balanceSheets[$wallet->getKey()];
27
    }
28
29
    /**
30
     * @inheritDoc
31
     */
32 74
    public function incBalance($object, int $amount): int
33
    {
34 74
        $balance = $this->getBalance($object) + $amount;
35 74
        $this->setBalance($object, $balance);
36 74
        return $balance;
37
    }
38
39
    /**
40
     * @inheritDoc
41
     */
42 74
    public function setBalance($object, int $amount): bool
43
    {
44 74
        $wallet = app(WalletService::class)->getWallet($object);
45 74
        $this->balanceSheets[$wallet->getKey()] = $amount;
46 74
        return true;
47
    }
48
49
}
50