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

CommonService   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Test Coverage

Coverage 89.47%

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 49
ccs 17
cts 19
cp 0.8947
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 bool $dbTran
15
     * @param Wallet $self
16
     * @param Transaction[] $transactions
17
     * @return \Bavix\Wallet\Models\Transaction[]
18
     */
19 28
    public function enforce(bool $dbTran, Wallet $self, array $transactions): array
20
    {
21
        $callback = function () use ($self, $transactions) {
22 28
            $objects = [];
23
24 28
            foreach ($transactions as $transaction) {
25 28
                if ($transaction->isConfirmed()) {
26 28
                    $this->addBalance($self, $transaction->getAmount());
27
                }
28
29 28
                $objects[] = $transaction->create($self);
30
            }
31
32 28
            return $objects;
33 28
        };
34
35 28
        if ($dbTran) {
36 28
            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 28
    public function addBalance(Wallet $wallet, int $amount): bool
48
    {
49 28
        $newBalance = $wallet->getBalanceAttribute() + $amount;
50 28
        $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 28
        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 28
            $proxy = app(ProxyService::class);
54 28
            $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 28
            return true;
56
        }
57
58
        return false;
59
    }
60
61
}
62