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

CommonService::multiOperation()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 10
nc 3
nop 2
dl 0
loc 16
ccs 11
cts 11
cp 1
crap 3
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
namespace Bavix\Wallet\Services;
4
5
use Bavix\Wallet\Exceptions\BalanceIsEmpty;
6
use Bavix\Wallet\Exceptions\InsufficientFunds;
7
use Bavix\Wallet\Interfaces\Wallet;
8
use Bavix\Wallet\Models\Transaction;
9
use Bavix\Wallet\Models\Wallet as WalletModel;
10
use Bavix\Wallet\Objects\Bring;
11
use Bavix\Wallet\Objects\Operation;
12
use Bavix\Wallet\Traits\HasWallet;
13
use Illuminate\Support\Facades\DB;
14
use function app;
15
use function compact;
16
17
class CommonService
18
{
19
20
    /**
21
     * @param Wallet $wallet
22
     * @param int $amount
23
     * @return void
24
     * @throws BalanceIsEmpty
25
     * @throws InsufficientFunds
26
     */
27 34
    public function verifyWithdraw(Wallet $wallet, int $amount): void
28
    {
29
        /**
30
         * @var HasWallet $wallet
31
         */
32 34
        if ($amount && !$wallet->balance) {
33 14
            throw new BalanceIsEmpty(trans('wallet::errors.wallet_empty'));
0 ignored issues
show
Bug introduced by
It seems like trans('wallet::errors.wallet_empty') can also be of type array; however, parameter $message of Bavix\Wallet\Exceptions\...eIsEmpty::__construct() 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

33
            throw new BalanceIsEmpty(/** @scrutinizer ignore-type */ trans('wallet::errors.wallet_empty'));
Loading history...
34
        }
35
36 28
        if (!$wallet->canWithdraw($amount)) {
37 1
            throw new InsufficientFunds(trans('wallet::errors.insufficient_funds'));
0 ignored issues
show
Bug introduced by
It seems like trans('wallet::errors.insufficient_funds') can also be of type array; however, parameter $message of Bavix\Wallet\Exceptions\...entFunds::__construct() 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

37
            throw new InsufficientFunds(/** @scrutinizer ignore-type */ trans('wallet::errors.insufficient_funds'));
Loading history...
38
        }
39 27
    }
40
41
    /**
42
     * Create Operation with DB::transaction
43
     *
44
     * @param Wallet $self
45
     * @param Operation[] $transactions
46
     * @return Transaction[]
47
     */
48 30
    public function enforce(Wallet $self, array $transactions): array
49
    {
50
        return DB::transaction(function () use ($self, $transactions) {
51 30
            return $this->multiOperation($self, $transactions);
52 30
        });
53
    }
54
55
    /**
56
     * Create Operation without DB::transaction
57
     *
58
     * @param Wallet $self
59
     * @param array $operations
60
     * @return array
61
     */
62 30
    public function multiOperation(Wallet $self, array $operations): array
63
    {
64 30
        $amount = 0;
65 30
        $objects = [];
66 30
        foreach ($operations as $operation) {
67 30
            if ($operation->isConfirmed()) {
68 30
                $amount += $operation->getAmount();
69
            }
70
71 30
            $objects[] = $operation
72 30
                ->setWallet($self)
73 30
                ->create();
74
        }
75
76 30
        $this->addBalance($self, $amount);
77 30
        return $objects;
78
    }
79
80
    /**
81
     * Create Bring with DB::transaction
82
     *
83
     * @param Bring[] $brings
84
     * @return array
85
     * @throws
86
     */
87 18
    public function assemble(array $brings): array
88
    {
89
        return DB::transaction(function () use ($brings) {
90 18
            return $this->multiBrings($brings);
91 18
        });
92
    }
93
94
    /**
95
     * Create Bring without DB::transaction
96
     *
97
     * @param array $brings
98
     * @return array
99
     */
100 18
    public function multiBrings(array $brings): array
101
    {
102 18
        $objects = [];
103 18
        foreach ($brings as $bring) {
104 18
            $objects[] = $bring->create();
105
        }
106
107 18
        return $objects;
108
    }
109
110
    /**
111
     * @param Wallet $wallet
112
     * @param int $amount
113
     * @return bool
114
     * @throws
115
     */
116 30
    public function addBalance(Wallet $wallet, int $amount): bool
117
    {
118
        /**
119
         * @var ProxyService $proxy
120
         * @var WalletModel $wallet
121
         */
122 30
        $proxy = app(ProxyService::class);
123 30
        $balance = $wallet->balance + $amount;
124 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

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

125
            $balance = $proxy->get(/** @scrutinizer ignore-type */ $wallet->getKey()) + $amount;
Loading history...
126
        }
127
128 30
        $result = $wallet->update(compact('balance'));
129 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

129
        $proxy->set(/** @scrutinizer ignore-type */ $wallet->getKey(), $balance);
Loading history...
130
131 30
        return $result;
132
    }
133
134
}
135