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

CommonService   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 49
ccs 0
cts 26
cp 0
rs 10
c 0
b 0
f 0
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A addBalance() 0 12 2
A enforce() 0 21 4
1
<?php
2
3
namespace Bavix\Wallet\Services;
4
5
use Bavix\Wallet\Interfaces\Wallet;
6
use Bavix\Wallet\Models\Wallet as WalletModel;
7
use Bavix\Wallet\Objects\Transaction;
8
use Illuminate\Support\Facades\DB;
9
10
class CommonService
11
{
12
13
    /**
14
     * @param Wallet $self
15
     * @param Transaction[] $transactions
16
     * @param bool $dbTran
17
     * @return \Bavix\Wallet\Models\Transaction[]
18
     */
19
    public function enforce(Wallet $self, array $transactions, bool $dbTran = null): array
20
    {
21
        $callback = function () use ($self, $transactions) {
22
            $objects = [];
23
24
            foreach ($transactions as $transaction) {
25
                if ($transaction->isConfirmed()) {
26
                    $this->addBalance($self, $transaction->getAmount());
27
                }
28
29
                $objects[] = $transaction->create($self);
30
            }
31
32
            return $objects;
33
        };
34
35
        if ($dbTran) {
36
            return DB::transaction($callback);
37
        }
38
39
        return $callback();
40
    }
41
42
    /**
43
     * @param Wallet $wallet
44
     * @param int $amount
45
     * @return bool
46
     */
47
    public function addBalance(Wallet $wallet, int $amount): bool
48
    {
49
        $newBalance = $wallet->getBalanceAttribute() + $amount;
50
        $wallet->balance = $newBalance;
0 ignored issues
show
Bug introduced by
Accessing balance on the interface Bavix\Wallet\Interfaces\Wallet suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
51
52
        if ($wallet->save()) {
0 ignored issues
show
Bug introduced by
The method save() does not exist on Bavix\Wallet\Interfaces\Wallet. It seems like you code against a sub-type of said class. However, the method does not exist in Bavix\Wallet\Interfaces\Customer or Bavix\Wallet\Interfaces\Product. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

52
        if ($wallet->/** @scrutinizer ignore-call */ save()) {
Loading history...
53
            $proxy = app(ProxyService::class);
54
            $proxy->set($wallet->getKey(), $newBalance);
0 ignored issues
show
Bug introduced by
The method getKey() does not exist on Bavix\Wallet\Interfaces\Wallet. It seems like you code against a sub-type of said class. However, the method does not exist in Bavix\Wallet\Interfaces\Customer or Bavix\Wallet\Interfaces\Product. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

54
            $proxy->set($wallet->/** @scrutinizer ignore-call */ getKey(), $newBalance);
Loading history...
55
            return true;
56
        }
57
58
        return false;
59
    }
60
61
}
62