Completed
Pull Request — master (#40)
by Бабичев
05:28
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
use function app;
12
use function compact;
13
14
class CommonService
15
{
16
17
    /**
18
     * @param Wallet $self
19
     * @param Operation[] $transactions
20
     * @return Transaction[]
21
     */
22 30
    public function enforce(Wallet $self, array $transactions): array
23
    {
24
        return DB::transaction(function () use ($self, $transactions) {
25 30
            $amount = 0;
26 30
            $objects = [];
27 30
            foreach ($transactions as $transaction) {
28 30
                if ($transaction->isConfirmed()) {
29 30
                    $amount += $transaction->getAmount();
30
                }
31
32 30
                $objects[] = $transaction->create($self);
33
            }
34
35 30
            $this->addBalance($self, $amount);
36 30
            return $objects;
37 30
        });
38
    }
39
40
    /**
41
     * @param Bring[] $brings
42
     * @return array
43
     * @throws
44
     */
45 18
    public function assemble(array $brings): array
46
    {
47
        return DB::transaction(function () use ($brings) {
48 18
            $objects = [];
49 18
            foreach ($brings as $bring) {
50 18
                $objects[] = $bring->create();
51
            }
52
53 18
            return $objects;
54 18
        });
55
    }
56
57
    /**
58
     * @param Wallet $wallet
59
     * @param int $amount
60
     * @return bool
61
     * @throws
62
     */
63 30
    public function addBalance(Wallet $wallet, int $amount): bool
64
    {
65
        /**
66
         * @var ProxyService $proxy
67
         * @var WalletModel $wallet
68
         */
69 30
        $proxy = app(ProxyService::class);
70 30
        $balance = $wallet->balance + $amount;
71 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

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

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

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