WithFilters   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 9
eloc 19
c 2
b 0
f 0
dl 0
loc 60
ccs 23
cts 23
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A enableFilters() 0 6 2
A applyFilters() 0 13 3
A formatFilters() 0 9 2
A isRequestedFilter() 0 3 1
A getFilterValue() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Zing\QueryBuilder\Concerns;
6
7
use Illuminate\Support\Collection;
8
use Zing\QueryBuilder\Filter;
9
10
trait WithFilters
11
{
12
    /**
13
     * @param array<(string|\Zing\QueryBuilder\Filter)>|string|\Zing\QueryBuilder\Filter $filters
14
     *
15
     * @return $this
16
     */
17 43
    public function enableFilters($filters)
18
    {
19 43
        $filters = \is_array($filters) ? $filters : \func_get_args();
20 43
        $this->applyFilters($this->formatFilters($filters));
21
22 42
        return $this;
23
    }
24
25
    /**
26
     * @param array<(string|\Zing\QueryBuilder\Filter)> $filters
27
     */
28 47
    protected function formatFilters(array $filters): Collection
29
    {
30 47
        return collect($filters)->map(
31 47
            function ($filter): Filter {
32 47
                if ($filter instanceof Filter) {
33 45
                    return $filter;
34
                }
35
36 2
                return Filter::exact($filter);
37
            }
38
        );
39
    }
40
41 43
    protected function applyFilters(Collection $filters): void
42
    {
43 43
        $filters->each(
44 43
            function (Filter $filter): void {
45 43
                $thisIsRequestedFilter = $this->isRequestedFilter($filter);
46 43
                if ($thisIsRequestedFilter) {
47 40
                    $filter->filter($this->getBuilder(), $this->getFilterValue($filter));
0 ignored issues
show
Bug introduced by
It seems like getBuilder() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

47
                    $filter->filter($this->/** @scrutinizer ignore-call */ getBuilder(), $this->getFilterValue($filter));
Loading history...
48
49 39
                    return;
50
                }
51
52 5
                if ($filter->hasDefault()) {
53 2
                    $filter->filter($this->getBuilder(), $filter->getDefault());
54
                }
55
            }
56
        );
57
    }
58
59 45
    protected function isRequestedFilter(Filter $filter): bool
60
    {
61 45
        return $this->request->has($filter->getProperty());
62
    }
63
64
    /**
65
     * @return mixed
66
     */
67 42
    protected function getFilterValue(Filter $filter)
68
    {
69 42
        return $this->request->input($filter->getProperty());
70
    }
71
}
72