HasCriteria::disableCriteria()   A
last analyzed

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

121
                $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...
122
            }
123 23
        }
124
125
        return $this;
126
    }
127
128
    /**
129
     * Clear all Criteria.
130
     *
131
     * @return $this
132
     */
133
    public function clearCriteria(): self
134
    {
135
        $this->criteria = new Collection();
136
137
        return $this;
138
    }
139
}
140