Passed
Pull Request — master (#104)
by Бабичев
494:32 queued 405:41
created

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