Filterer   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 4
dl 0
loc 56
rs 10
c 0
b 0
f 0
ccs 24
cts 24
cp 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A filter() 0 21 4
A resolveTypes() 0 10 2
1
<?php
2
3
/*
4
 * This file is part of the Lug package.
5
 *
6
 * (c) Eric GELOEN <[email protected]>
7
 *
8
 * For the full copyright and license information, please read the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Lug\Component\Grid\Filter;
13
14
use Lug\Component\Grid\DataSource\DataSourceBuilderInterface;
15
use Lug\Component\Grid\Filter\Type\TypeInterface;
16
use Lug\Component\Grid\Model\GridInterface;
17
use Lug\Component\Registry\Model\RegistryInterface;
18
use Symfony\Component\OptionsResolver\OptionsResolver;
19
20
/**
21
 * @author GeLo <[email protected]>
22
 */
23
class Filterer implements FiltererInterface
24
{
25
    /**
26
     * @var RegistryInterface
27
     */
28
    private $filterRegistry;
29
30
    /**
31
     * @param RegistryInterface $filterRegistry
32
     */
33 2
    public function __construct(RegistryInterface $filterRegistry)
34
    {
35 2
        $this->filterRegistry = $filterRegistry;
36 2
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41 1
    public function filter(DataSourceBuilderInterface $builder, GridInterface $grid, array $filters = [])
42
    {
43 1
        foreach ($filters as $name => $filter) {
44 1
            if (!$grid->hasFilter($name)) {
45 1
                continue;
46
            }
47
48 1
            $gridFilter = $grid->getFilter($name);
49 1
            $types = $this->resolveTypes($gridFilter->getType());
50 1
            $resolver = new OptionsResolver();
51
52 1
            foreach ($types as $type) {
53 1
                $type->configureOptions($resolver);
54 1
            }
55
56 1
            reset($types)->filter($filter, $resolver->resolve(array_merge(
57 1
                ['filter' => $gridFilter, 'grid' => $grid, 'builder' => $builder],
58 1
                $gridFilter->getOptions()
59 1
            )));
60 1
        }
61 1
    }
62
63
    /**
64
     * @param string $type
65
     *
66
     * @return TypeInterface[]
67
     */
68 1
    private function resolveTypes($type)
69
    {
70 1
        $filterTypes = [];
71
72
        do {
73 1
            $filterTypes[] = $filterType = $this->filterRegistry[$type];
74 1
        } while (($type = $filterType->getParent()) !== null);
75
76 1
        return $filterTypes;
77
    }
78
}
79