Passed
Push — master ( 74cbab...62dc6b )
by Stephen
02:33
created

AbstractQueryWithFilters   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 64
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A filterFilters() 0 7 2
A __construct() 0 4 1
A applyFiltersToQuery() 0 13 2
1
<?php
2
3
4
namespace Sfneal\Queries;
5
6
7
use Sfneal\Queries\Interfaces\DynamicQuery;
8
use Sfneal\Queries\Traits\ApplyFilter;
9
use Illuminate\Database\Eloquent\Builder;
10
11
abstract class AbstractQueryWithFilters extends AbstractQuery implements DynamicQuery
12
{
13
    use ApplyFilter;
14
15
    /**
16
     * Array of attribute/form input name keys and Filter class values
17
     *
18
     * @var array
19
     */
20
    public $attribute_filters;
21
22
    /**
23
     * Filter values to be passed to Filer classes
24
     *
25
     * @var array
26
     */
27
    public $filters;
28
29
    /**
30
     * QueryWithFilters constructor.
31
     *
32
     * @param array $filters
33
     */
34
    public function __construct(array $filters)
35
    {
36
        // Remove invalid filters prior to executing query
37
        $this->filters = $this->filterFilters($filters);
38
    }
39
40
    /**
41
     * Apply Filter decorators to the query if both the parameter is given and the Filter class exists
42
     *
43
     * @param Builder $builder
44
     * @param array|null $filters
45
     * @return Builder
46
     */
47
    public function applyFiltersToQuery(Builder $builder, array $filters = null)
48
    {
49
        // Wrap scopes
50
        $builder->where(function (Builder $query) use ($filters) {
51
52
            // Check every parameter to see if there's a corresponding Filter class
53
            foreach ($filters ?? $this->filters as $filterName => $value) {
54
55
                // Apply Filter class if it exists and is a filterable attribute
56
                $query = self::applyFilterToQuery($query, $filterName, $value);
0 ignored issues
show
Bug Best Practice introduced by
The method Sfneal\Queries\AbstractQ...s::applyFilterToQuery() is not static, but was called statically. ( Ignorable by Annotation )

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

56
                /** @scrutinizer ignore-call */ 
57
                $query = self::applyFilterToQuery($query, $filterName, $value);
Loading history...
57
            }
58
        });
59
        return $builder;
60
    }
61
62
    /**
63
     * Parse filters and remove all keys that are not found in the declared attributes_filters
64
     *
65
     * @param array $filters filters passed from search form
66
     * @return array
67
     */
68
    private function filterFilters(array $filters): array {
69
        return array_filter($filters, function($value, $filter) {
70
71
            // Return true if the filter is a filterable attribute
72
            return in_array($filter, array_keys($this->attribute_filters)) && !is_null($value);
73
74
        },ARRAY_FILTER_USE_BOTH);
75
    }
76
}
77