Completed
Push — master ( 3c1bd9...d781ee )
by wen
12:17
created

Filters   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 47
c 0
b 0
f 0
wmc 6
lcom 2
cbo 2
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A initialize() 0 6 1
A add() 0 14 2
A getActive() 0 6 1
A apply() 0 6 1
A getValues() 0 8 1
1
<?php
2
3
namespace Sco\Admin\View\Extensions;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Sco\Admin\Contracts\Initializable;
7
use Sco\Admin\Contracts\View\Filters\FilterInterface;
8
9
class Filters extends Extension implements Initializable
10
{
11
    public function initialize()
12
    {
13
        $this->each(function (FilterInterface $filter) {
14
            $filter->initialize();
15
        });
16
    }
17
18
    public function add($value)
19
    {
20
        if (!($value instanceof FilterInterface)) {
21
            throw new \InvalidArgumentException(
22
                sprintf(
23
                    'filter must be %s',
24
                    FilterInterface::class
25
                )
26
            );
27
        }
28
29
        $this->push($value);
30
        return $this;
31
    }
32
33
    public function getActive()
34
    {
35
        return $this->filter(function (FilterInterface $filter) {
36
            return $filter->isActive();
37
        });
38
    }
39
40
    public function apply(Builder $query)
41
    {
42
        $this->getActive()->each(function (FilterInterface $filter) use ($query) {
43
            $filter->apply($query);
44
        });
45
    }
46
47
    public function getValues()
48
    {
49
        return $this->mapWithKeys(function (FilterInterface $filter) {
50
            return [
51
                $filter->getName() => $filter->getValue(),
52
            ];
53
        });
54
    }
55
}
56