Completed
Push — master ( a7ce67...10cb9f )
by Paweł
31s
created

StringFilter::apply()   D

Complexity

Conditions 12
Paths 144

Size

Total Lines 40
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 40
rs 4.8484
c 0
b 0
f 0
cc 12
eloc 21
nc 144
nop 4

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Data\ExpressionBuilderInterface;
16
use Sylius\Component\Grid\Filtering\FilterInterface;
17
18
/**
19
 * @author Paweł Jędrzejewski <[email protected]>
20
 */
21
final class StringFilter implements FilterInterface
22
{
23
    const NAME = 'string';
24
25
    const TYPE_EQUAL = 'equal';
26
    const TYPE_NOT_EQUAL = 'not_equal';
27
    const TYPE_EMPTY = 'empty';
28
    const TYPE_NOT_EMPTY = 'not_empty';
29
    const TYPE_CONTAINS = 'contains';
30
    const TYPE_NOT_CONTAINS = 'not_contains';
31
    const TYPE_STARTS_WITH = 'starts_with';
32
    const TYPE_ENDS_WITH = 'ends_with';
33
    const TYPE_IN = 'in';
34
    const TYPE_NOT_IN = 'not_in';
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function apply(DataSourceInterface $dataSource, $name, $data, array $options)
40
    {
41
        $expressionBuilder = $dataSource->getExpressionBuilder();
42
43
        if (is_array($data) && !isset($data['type'])) {
44
            $data['type'] = isset($options['type']) ? $options['type'] : self::TYPE_CONTAINS;
45
        }
46
47
        if (!is_array($data)) {
48
            $data = ['type' => self::TYPE_CONTAINS, 'value' => $data];
49
        }
50
51
        $fields = array_key_exists('fields', $options) ? $options['fields'] : [$name];
52
53
        $type = $data['type'];
54
        $value = array_key_exists('value', $data) ? $data['value'] : null;
55
56
        if (!in_array($type, [self::TYPE_NOT_EMPTY, self::TYPE_EMPTY], true) && '' === trim($value)) {
57
            return;
58
        }
59
60
        if (1 === count($fields)) {
61
            $dataSource->restrict($this->getExpression($expressionBuilder, $type, current($fields), $value));
62
63
            return;
64
        }
65
66
        $expressions = [];
67
        foreach ($fields as $field) {
68
            $expressions[] = $this->getExpression($expressionBuilder, $type, $field, $value);
69
        }
70
71
        if (self::TYPE_NOT_EQUAL === $type) {
72
            $dataSource->restrict($expressionBuilder->andX(...$expressions));
73
74
            return;
75
        }
76
77
        $dataSource->restrict($expressionBuilder->orX(...$expressions));
78
    }
79
80
    /**
81
     * @param ExpressionBuilderInterface $expressionBuilder
82
     * @param string $type
83
     * @param string $field
84
     * @param mixed $value
85
     *
86
     * @return ExpressionBuilderInterface
87
     */
88
    private function getExpression(ExpressionBuilderInterface $expressionBuilder, $type, $field, $value)
89
    {
90
        switch ($type) {
91
            case self::TYPE_EQUAL:
92
                return $expressionBuilder->equals($field, $value);
93
            case self::TYPE_NOT_EQUAL:
94
                return $expressionBuilder->notEquals($field, $value);
95
            case self::TYPE_EMPTY:
96
                return $expressionBuilder->isNull($field);
97
            case self::TYPE_NOT_EMPTY:
98
                return $expressionBuilder->isNotNull($field);
99
            case self::TYPE_CONTAINS:
100
                return $expressionBuilder->like($field, '%'.$value.'%');
101
            case self::TYPE_NOT_CONTAINS:
102
                return $expressionBuilder->notLike($field, '%'.$value.'%');
103
            case self::TYPE_STARTS_WITH:
104
                return $expressionBuilder->like($field, $value.'%');
105
            case self::TYPE_ENDS_WITH:
106
                return $expressionBuilder->like($field, '%'.$value);
107
            case self::TYPE_IN:
108
                return $expressionBuilder->in($field, array_map('trim', explode(',', $value)));
109
            case self::TYPE_NOT_IN:
110
                return $expressionBuilder->notIn($field, array_map('trim', explode(',', $value)));
111
            default:
112
                throw new \InvalidArgumentException(sprintf('Could not get an expression for type "%s"!', $type));
113
        }
114
    }
115
}
116