Completed
Push — master ( db3809...13cf22 )
by Kamil
19:30
created

EntityFilter   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 5
c 0
b 0
f 0
lcom 0
cbo 2
dl 0
loc 31
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A apply() 0 17 4
A getType() 0 4 1
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sylius\Component\Grid\Filter;
13
14
use Sylius\Component\Grid\Data\DataSourceInterface;
15
use Sylius\Component\Grid\Filtering\FilterInterface;
16
17
/**
18
 * @author Grzegorz Sadowski <[email protected]>
19
 */
20
final class EntityFilter implements FilterInterface
21
{
22
    /**
23
     * {@inheritdoc}
24
     */
25
    public function apply(DataSourceInterface $dataSource, $name, $data, array $options)
26
    {
27
        if (empty($data)) {
28
            return;
29
        }
30
31
        $fields = isset($options['fields']) ? $options['fields'] : [$name];
32
33
        $expressionBuilder = $dataSource->getExpressionBuilder();
34
35
        $expressions = [];
36
        foreach ($fields as $field) {
37
            $expressions[] = $expressionBuilder->equals($field, $data);
38
        }
39
40
        $dataSource->restrict($expressionBuilder->orX(...$expressions));
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function getType()
47
    {
48
        return 'entity';
49
    }
50
}
51