Passed
Push — master ( 36df2b...89c2b4 )
by noitran
03:13
created

HasCriteria::clearCriteria()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 5
c 0
b 0
f 0
ccs 0
cts 3
cp 0
rs 10
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Noitran\Repositories\Repositories\Concerns;
4
5
use Illuminate\Support\Collection;
6
use Noitran\Repositories\Contracts\Criteria\CriteriaInterface;
7
use Noitran\Repositories\Exceptions\RepositoryException;
8
9
/**
10
 * Trait HasCriteria
11
 */
12
trait HasCriteria
13
{
14
    /**
15
     * Collection of Criteria
16
     *
17
     * @var Collection
18
     */
19
    protected $criteria;
20
21
    /**
22
     * @var bool
23
     */
24
    protected $disableCriteria = false;
25
26
    /**
27
     * Get Collection of Criteria
28
     *
29
     * @return Collection|null
30
     */
31 10
    public function getCriteria(): ?Collection
32
    {
33 10
        return $this->criteria;
34
    }
35
36
    /**
37
     * Disable/Enable all Criteria
38
     *
39
     * @param bool $disable
40
     *
41
     * @return $this
42
     */
43
    public function disableCriteria($disable = true): self
44
    {
45
        $this->disableCriteria = $disable;
46
47
        return $this;
48
    }
49
50
    /**
51
     * Push Criteria into Collection
52
     *
53
     * @param $criteria
54
     *
55
     * @throws RepositoryException
56
     *
57
     * @return $this
58
     */
59 2
    public function pushCriteria($criteria): self
60
    {
61 2
        if (is_string($criteria)) {
62
            $criteria = new $criteria();
63
        }
64
65 2
        if (!$criteria instanceof CriteriaInterface) {
66
            throw new RepositoryException(
67
                'Class ' . get_class($criteria) . ' must be an instance of ' . CriteriaInterface::class
68
            );
69
        }
70
71 2
        $this->criteria->push($criteria);
72
73 2
        return $this;
74
    }
75
76
    /**
77
     * Remove Criteria from collection
78
     *
79
     * @param $criteria
80
     *
81
     * @return $this
82
     */
83
    public function popCriteria($criteria): self
84
    {
85
        $this->criteria = $this->criteria->reject(function ($value) use ($criteria) {
86
            if (is_object($value) && is_string($criteria)) {
87
                return get_class($value) === $criteria;
88
            }
89
90
            if (is_string($value) && is_object($criteria)) {
91
                return $value === get_class($criteria);
92
            }
93
94
            return get_class($value) === get_class($criteria);
95
        });
96
97
        return $this;
98
    }
99
100
    /**
101
     * Apply criteria in current Query
102
     *
103
     * @return $this
104
     */
105 10
    protected function applyCriteria(): self
106
    {
107 10
        if ($this->disableCriteria === true) {
108
            return $this;
109
        }
110
111 10
        $criteria = $this->getCriteria();
112
113 10
        if (! $criteria) {
0 ignored issues
show
introduced by
$criteria is of type Illuminate\Support\Collection, thus it always evaluated to true.
Loading history...
114
            return $this;
115
        }
116
117 10
        foreach ($criteria as $value) {
118 2
            if ($value instanceof CriteriaInterface) {
119 2
                $this->model = $value->apply($this->model, $this);
0 ignored issues
show
Bug introduced by
$this of type Noitran\Repositories\Rep...es\Concerns\HasCriteria is incompatible with the type Noitran\Repositories\Con...ory\RepositoryInterface expected by parameter $repository of Noitran\Repositories\Con...teriaInterface::apply(). ( Ignorable by Annotation )

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

119
                $this->model = $value->apply($this->model, /** @scrutinizer ignore-type */ $this);
Loading history...
Bug Best Practice introduced by
The property model does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
120
            }
121
        }
122
123 10
        return $this;
124
    }
125
126
    /**
127
     * Clear all Criteria
128
     *
129
     * @return $this
130
     */
131
    public function clearCriteria(): self
132
    {
133
        $this->criteria = new Collection();
134
135
        return $this;
136
    }
137
}
138