Passed
Push — master ( ac21bd...36df2b )
by noitran
03:42
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 8
    public function getCriteria(): ?Collection
32
    {
33 8
        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
    public function pushCriteria($criteria): self
60
    {
61
        if (is_string($criteria)) {
62
            $criteria = new $criteria();
63
        }
64
65
        if (!$criteria instanceof CriteriaInterface) {
66
            throw new RepositoryException(
67
                'Class ' . get_class($criteria) . ' must be an instance of ' . CriteriaInterface::class
68
            );
69
        }
70
71
        $this->criteria->push($criteria);
72
73
        return $this;
74
    }
75
76
    /**
77
     * Remove Criteria from collection
78
     *
79
     * @param $criteria
80
     *
81
     * @return mixed
82
     */
83
    public function popCriteria($criteria)
0 ignored issues
show
Unused Code introduced by
The parameter $criteria is not used and could be removed. ( Ignorable by Annotation )

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

83
    public function popCriteria(/** @scrutinizer ignore-unused */ $criteria)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
84
    {
85
        // @todo implement
86
    }
87
88
    /**
89
     * Apply criteria in current Query
90
     *
91
     * @return $this
92
     */
93 8
    protected function applyCriteria(): self
94
    {
95 8
        if ($this->disableCriteria === true) {
96
            return $this;
97
        }
98
99 8
        $criteria = $this->getCriteria();
100
101 8
        if (! $criteria) {
0 ignored issues
show
introduced by
$criteria is of type Illuminate\Support\Collection, thus it always evaluated to true.
Loading history...
102 8
            return $this;
103
        }
104
105
        foreach ($criteria as $value) {
106
            if ($value instanceof CriteriaInterface) {
107
                $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

107
                $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...
108
            }
109
        }
110
111
        return $this;
112
    }
113
114
    /**
115
     * Clear all Criteria
116
     *
117
     * @return $this
118
     */
119
    public function clearCriteria(): self
120
    {
121
        $this->criteria = new Collection();
122
123
        return $this;
124
    }
125
}
126