Passed
Push — master ( 653fca...0c85de )
by Maksim
03:56 queued 11s
created

CompositeKeyScope::apply()   C

Complexity

Conditions 12
Paths 1

Size

Total Lines 46
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 28
c 2
b 0
f 0
dl 0
loc 46
rs 6.9666
cc 12
nc 1
nop 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace MaksimM\CompositePrimaryKeys\Scopes;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use MaksimM\CompositePrimaryKeys\Exceptions\MissingPrimaryKeyValueException;
7
use MaksimM\CompositePrimaryKeys\Http\Traits\NormalizedKeysParser;
8
9
class CompositeKeyScope
10
{
11
    use NormalizedKeysParser;
12
13
    private $key;
14
    private $ids;
15
    private $inverse;
16
    private $binary_columns;
17
18
    public function __construct($key, $ids, $inverse, $binary_columns = [])
19
    {
20
        $this->key = $key;
21
        $this->ids = $ids;
22
        $this->inverse = $inverse;
23
        $this->binary_columns = $binary_columns;
24
    }
25
26
    /**
27
     * @return mixed
28
     */
29
    public function getBinaryColumns()
30
    {
31
        return $this->binary_columns;
32
    }
33
34
    /**
35
     * Apply a query scope.
36
     *
37
     * @param \Illuminate\Database\Query\Builder|Builder $query
38
     *
39
     * @return mixed
40
     */
41
    public function apply($query)
42
    {
43
        $query->where(function ($query) {
44
            foreach ($this->ids as $compositeKey) {
45
                // try to parse normalized key
46
                if (!is_array($compositeKey)) {
47
                    $compositeKey = $this->parseNormalizedKey($compositeKey);
48
                } else {
49
                    if (method_exists($query->getModel(), 'hexBinaryColumns') && $query->getModel()->hexBinaryColumns()) {
50
                        foreach ($compositeKey as $key => $value) {
51
                            $compositeKey[$key] = in_array($key, $this->getBinaryColumns()) ? $this->recoverBinaryKey(
52
                                $key,
53
                                $value
54
                            ) : $value;
55
                        }
56
                    }
57
                }
58
59
                $queryWriter = function ($query) use ($compositeKey) {
60
                    /*
61
                     * @var \Illuminate\Database\Query\Builder $query
62
                     */
63
                    foreach ($this->key as $key) {
64
                        if (empty($compositeKey)) {
65
                            $query->where($query->getModel()->qualifyColumn($key), null);
66
                        } else {
67
                            if (!isset($compositeKey[$key])) {
68
                                throw new MissingPrimaryKeyValueException(
69
                                    $key,
70
                                    'Missing value for key '.$key.' in record '.json_encode($compositeKey)
71
                                );
72
                            }
73
74
                            if ($this->inverse) {
75
                                $query->orWhere($query->getModel()->qualifyColumn($key), '!=', $compositeKey[$key]);
76
                            } else {
77
                                $query->where($query->getModel()->qualifyColumn($key), $compositeKey[$key]);
78
                            }
79
                        }
80
                    }
81
                };
82
83
                if ($this->inverse) {
84
                    $query->where($queryWriter);
85
                } else {
86
                    $query->orWhere($queryWriter);
87
                }
88
            }
89
        });
90
    }
91
}
92