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

CommonService   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Test Coverage

Coverage 94.74%

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 50
ccs 18
cts 19
cp 0.9474
rs 10
c 0
b 0
f 0
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A addBalance() 0 19 3
A enforce() 0 15 3
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