Passed
Pull Request — master (#40)
by Бабичев
05:39 queued 01:32
created

CommonService   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Test Coverage

Coverage 96.15%

Importance

Changes 0
Metric Value
eloc 24
dl 0
loc 68
ccs 25
cts 26
cp 0.9615
rs 10
c 0
b 0
f 0
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A addBalance() 0 19 3
A assemble() 0 9 2
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\Models\Wallet as WalletModel;
8
use Bavix\Wallet\Objects\Bring;
9
use Bavix\Wallet\Objects\Operation;
10
use Illuminate\Support\Facades\DB;
11
12
class CommonService
13
{
14
15
    /**
16
     * @param Wallet $self
17
     * @param Operation[] $transactions
18
     * @return Transaction[]
19
     */
20 29
    public function enforce(Wallet $self, array $transactions): array
21
    {
22
        return DB::transaction(function () use ($self, $transactions) {
23 29
            $amount = 0;
24 29
            $objects = [];
25 29
            foreach ($transactions as $transaction) {
26 29
                if ($transaction->isConfirmed()) {
27 29
                    $amount += $transaction->getAmount();
28
                }
29
30 29
                $objects[] = $transaction->create($self);
31
            }
32
33 29
            $this->addBalance($self, $amount);
34 29
            return $objects;
35 29
        });
36
    }
37
38
    /**
39
     * @param Bring[] $brings
40
     * @return array
41
     * @throws
42
     */
43 18
    public function assemble(array $brings): array
44
    {
45
        return DB::transaction(function () use ($brings) {
46 18
            $objects = [];
47 18
            foreach ($brings as $bring) {
48 18
                $objects[] = $bring->create();
49
            }
50
51 18
            return $objects;
52 18
        });
53
    }
54
55
    /**
56
     * @param Wallet $wallet
57
     * @param int $amount
58
     * @return bool
59
     * @throws
60
     */
61 29
    public function addBalance(Wallet $wallet, int $amount): bool
62
    {
63
        /**
64
         * @var ProxyService $proxy
65
         * @var WalletModel $wallet
66
         */
67 29
        $proxy = \app(ProxyService::class);
68 29
        $balance = $wallet->balance;
69 29
        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

69
        if ($proxy->has(/** @scrutinizer ignore-type */ $wallet->getKey())) {
Loading history...
70 29
            $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

70
            $balance = $proxy->get(/** @scrutinizer ignore-type */ $wallet->getKey());
Loading history...
71
        }
72
73 29
        $balance += $amount;
74 29
        if ($wallet->update(\compact('balance'))) {
75 29
            $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

75
            $proxy->set(/** @scrutinizer ignore-type */ $wallet->getKey(), $balance);
Loading history...
76 29
            return true;
77
        }
78
79
        return false;
80
    }
81
82
}
83