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

CommonService::addBalance()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3.009

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 4
nop 2
dl 0
loc 19
ccs 9
cts 10
cp 0.9
crap 3.009
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
namespace Bavix\Wallet\Services;
4
5
use Bavix\Wallet\Interfaces\Wallet;
6
use Bavix\Wallet\Models\Transaction;
7
use Bavix\Wallet\Objects\Operation;
8
9
class CommonService
10
{
11
12
    /**
13
     * @param Wallet $self
14
     * @param Operation[] $transactions
15
     * @return Transaction[]
16
     */
17 28
    public function enforce(Wallet $self, array $transactions): array
18
    {
19 28
        $objects = [];
20 28
        $amount = 0;
21
22 28
        foreach ($transactions as $transaction) {
23 28
            if ($transaction->isConfirmed()) {
24 28
                $amount += $transaction->getAmount();
25
            }
26
27 28
            $objects[] = $transaction->create($self);
28
        }
29
30 28
        $this->addBalance($self, $amount);
31 28
        return $objects;
32
    }
33
34
    /**
35
     * @param Wallet $wallet
36
     * @param int $amount
37
     * @return bool
38
     * @throws
39
     */
40 28
    public function addBalance(Wallet $wallet, int $amount): bool
41
    {
42
        /**
43
         * @var ProxyService $proxy
44
         * @var \Bavix\Wallet\Models\Wallet $wallet
45
         */
46 28
        $proxy = \app(ProxyService::class);
47 28
        $balance = $wallet->balance;
48 28
        if ($proxy->has($wallet->getKey())) {
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::has() 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

48
        if ($proxy->has(/** @scrutinizer ignore-type */ $wallet->getKey())) {
Loading history...
49 28
            $balance = $proxy->get($wallet->getKey());
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::get() 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

49
            $balance = $proxy->get(/** @scrutinizer ignore-type */ $wallet->getKey());
Loading history...
50
        }
51
52 28
        $balance += $amount;
53 28
        if ($wallet->update(\compact('balance'))) {
54 28
            $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

54
            $proxy->set(/** @scrutinizer ignore-type */ $wallet->getKey(), $balance);
Loading history...
55 28
            return true;
56
        }
57
58
        return false;
59
    }
60
61
}
62