Completed
Push — master ( 650783...0c9838 )
by wen
15:06
created

Filters   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A initialize() 0 6 1
A add() 0 15 2
A getActive() 0 6 1
A apply() 0 6 1
A getDisplayValues() 0 8 1
1
<?php
2
3
namespace Sco\Admin\Display\Extensions;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Sco\Admin\Contracts\Initializable;
7
use Sco\Admin\Contracts\Display\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
31
        return $this;
32
    }
33
34
    /**
35
     *
36
     * @return \Illuminate\Support\Collection
37
     */
38
    public function getActive()
39
    {
40
        return $this->filter(function (FilterInterface $filter) {
41
            return $filter->isActive();
42
        });
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function apply(Builder $query)
49
    {
50
        $this->getActive()->each(function (FilterInterface $filter) use ($query) {
51
            $filter->apply($query);
52
        });
53
    }
54
55
    public function getDisplayValues()
56
    {
57
        return $this->mapWithKeys(function (FilterInterface $filter) {
58
            return [
59
                $filter->getName() => $filter->getDisplayValue(),
60
            ];
61
        });
62
    }
63
}
64