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

CommonService::enforce()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 1
nop 2
dl 0
loc 15
ccs 9
cts 9
cp 1
crap 3
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\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 30
    public function enforce(Wallet $self, array $transactions): array
21
    {
22
        return DB::transaction(function () use ($self, $transactions) {
23 30
            $amount = 0;
24 30
            $objects = [];
25 30
            foreach ($transactions as $transaction) {
26 30
                if ($transaction->isConfirmed()) {
27 30
                    $amount += $transaction->getAmount();
28
                }
29
30 30
                $objects[] = $transaction->create($self);
31
            }
32
33 30
            $this->addBalance($self, $amount);
34 30
            return $objects;
35 30
        });
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 30
    public function addBalance(Wallet $wallet, int $amount): bool
62
    {
63
        /**
64
         * @var ProxyService $proxy
65
         * @var WalletModel $wallet
66
         */
67 30
        $proxy = \app(ProxyService::class);
68 30
        $balance = $wallet->balance + $amount;
69 30
        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 30
            $balance = $proxy->get($wallet->getKey()) + $amount;
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()) + $amount;
Loading history...
71
        }
72
73 30
        $result = $wallet->update(\compact('balance'));
74 30
        $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

74
        $proxy->set(/** @scrutinizer ignore-type */ $wallet->getKey(), $balance);
Loading history...
75
76 30
        return $result;
77
    }
78
79
}
80