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

CompositeKeyScope   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 14
eloc 39
c 2
b 0
f 0
dl 0
loc 78
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getBinaryColumns() 0 3 1
C apply() 0 46 12
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