Completed
Pull Request — 2.x (#311)
by
unknown
01:34
created

TranslationFieldFilter::filter()   C

Complexity

Conditions 12
Paths 26

Size

Total Lines 65

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 65
rs 6.3369
c 0
b 0
f 0
cc 12
nc 26
nop 4

How to fix   Long Method    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
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\TranslationBundle\Filter;
15
16
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
17
use Sonata\AdminBundle\Form\Type\Filter\DefaultType;
18
use Sonata\DoctrineORMAdminBundle\Filter\Filter;
19
use Sonata\TranslationBundle\Enum\TranslationFilterMode;
20
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
21
use Symfony\Component\Form\Extension\Core\Type\TextType;
22
23
final class TranslationFieldFilter extends Filter
24
{
25
    private $filterMode;
26
27
    public function __construct(string $filterMode)
28
    {
29
        $this->filterMode = $filterMode;
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function filter(ProxyQueryInterface $queryBuilder, $alias, $field, $data)
36
    {
37
        if (!$data || !\is_array($data) || !\array_key_exists('value', $data) || null === $data['value']) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $data of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
38
            return;
39
        }
40
41
        $data['value'] = trim($data['value']);
42
43
        if (0 === \strlen($data['value'])) {
44
            return;
45
        }
46
        $joinAlias = 'tff';
47
        $filterMode = $this->getOption('filter_mode');
48
49
        // verify if the join is not already done
50
        $aliasAlreadyExists = false;
51
        foreach ($queryBuilder->getDQLParts()['join'] as $joins) {
52
            foreach ($joins as $join) {
53
                if ($join->getAlias() === $joinAlias) {
54
                    $aliasAlreadyExists = true;
55
                    break 2;
56
                }
57
            }
58
        }
59
60
        if (!$aliasAlreadyExists) {
61
            $queryBuilder->leftJoin($alias . '.translations', $joinAlias);
62
        }
63
64
        if ($filterMode === TranslationFilterMode::GEDMO) {
65
            // search on translation OR on normal field when using Gedmo
66
            $this->applyWhere($queryBuilder, $queryBuilder->expr()->orX(
67
                $queryBuilder->expr()->andX(
68
                    $queryBuilder->expr()->eq($joinAlias . '.field', $queryBuilder->expr()->literal($field)),
69
                    $queryBuilder->expr()->like(
70
                        $joinAlias . '.content',
71
                        $queryBuilder->expr()->literal('%' . $data['value'] . '%')
72
                    )
73
                ),
74
                $queryBuilder->expr()->like(
75
                    sprintf('%s.%s', $alias, $field),
76
                    $queryBuilder->expr()->literal('%' . $data['value'] . '%')
77
                )
78
            ));
79
80
            $this->active = true;
81
        } elseif ($filterMode === TranslationFilterMode::KNPLABS) {
82
            // search on translation OR on normal field when using Knp
83
            $this->applyWhere($queryBuilder,
84
                $queryBuilder->expr()->andX(
85
                    $queryBuilder->expr()->like(
86
                        $joinAlias . ".$field",
87
                        $queryBuilder->expr()->literal('%' . $data['value'] . '%')
88
                    )
89
                )
90
            );
91
92
            $this->active = true;
93
        } else {
94
            throw new \LogicException(
95
                sprintf('Invalid filter mode given: "%s"', $filterMode)
96
            );
97
        }
98
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104
    public function getDefaultOptions()
105
    {
106
        return [
107
            'field_type' => TextType::class,
108
            'operator_type' => HiddenType::class,
109
            'filter_mode' => $this->filterMode,
110
            'operator_options' => [],
111
        ];
112
    }
113
114
    /**
115
     * {@inheritdoc}
116
     */
117
    public function getRenderSettings()
118
    {
119
        return [DefaultType::class, [
120
            'field_type' => $this->getFieldType(),
121
            'field_options' => $this->getFieldOptions(),
122
            'operator_type' => $this->getOption('operator_type'),
123
            'operator_options' => $this->getOption('operator_options'),
124
            'label' => $this->getLabel(),
125
        ]];
126
    }
127
128
    /**
129
     * {@inheritdoc}
130
     */
131
    protected function association(ProxyQueryInterface $queryBuilder, $data)
132
    {
133
        $alias = $queryBuilder->entityJoin($this->getParentAssociationMappings());
134
135
        return [$this->getOption('alias', $alias), $this->getFieldName()];
136
    }
137
}
138