Completed
Push — 3.x-dev-kit ( 4fe8f1 )
by
unknown
11:30 queued 08:33
created

ChoiceFilter::filter()   C

Complexity

Conditions 14
Paths 8

Size

Total Lines 40
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 40
rs 5.0864
cc 14
eloc 24
nc 8
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 Sonata Project package.
5
 *
6
 * (c) Thomas Rabaix <[email protected]>
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 Sonata\DoctrineORMAdminBundle\Filter;
13
14
use Doctrine\ORM\QueryBuilder;
15
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
16
use Sonata\AdminBundle\Form\Type\Filter\ChoiceType;
17
18
class ChoiceFilter extends Filter
19
{
20
    /**
21
     * @param ProxyQueryInterface|QueryBuilder $queryBuilder
22
     *
23
     * {@inheritdoc}
24
     */
25
    public function filter(ProxyQueryInterface $queryBuilder, $alias, $field, $data)
26
    {
27
        if (!$data || !is_array($data) || !array_key_exists('type', $data) || !array_key_exists('value', $data)) {
28
            return;
29
        }
30
31
        if (is_array($data['value'])) {
32
            if (count($data['value']) == 0) {
33
                return;
34
            }
35
36
            if (in_array('all', $data['value'], true)) {
37
                return;
38
            }
39
40
            // Have to pass IN array value as parameter. See: http://www.doctrine-project.org/jira/browse/DDC-3759
41
            $completeField = sprintf('%s.%s', $alias, $field);
42
            $parameterName = $this->getNewParameterName($queryBuilder);
43
            if ($data['type'] == ChoiceType::TYPE_NOT_CONTAINS) {
44
                $this->applyWhere($queryBuilder, $queryBuilder->expr()->notIn($completeField, ':'.$parameterName));
45
            } else {
46
                $this->applyWhere($queryBuilder, $queryBuilder->expr()->in($completeField, ':'.$parameterName));
47
            }
48
            $queryBuilder->setParameter($parameterName, $data['value']);
49
        } else {
50
            if ($data['value'] === '' || $data['value'] === null || $data['value'] === false || $data['value'] === 'all') {
51
                return;
52
            }
53
54
            $parameterName = $this->getNewParameterName($queryBuilder);
55
56
            if ($data['type'] == ChoiceType::TYPE_NOT_CONTAINS) {
57
                $this->applyWhere($queryBuilder, sprintf('%s.%s <> :%s', $alias, $field, $parameterName));
58
            } else {
59
                $this->applyWhere($queryBuilder, sprintf('%s.%s = :%s', $alias, $field, $parameterName));
60
            }
61
62
            $queryBuilder->setParameter($parameterName, $data['value']);
63
        }
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    public function getDefaultOptions()
70
    {
71
        return array();
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77
    public function getRenderSettings()
78
    {
79
        return array('sonata_type_filter_default', array(
80
            'operator_type' => 'sonata_type_equal',
81
            'field_type' => $this->getFieldType(),
82
            'field_options' => $this->getFieldOptions(),
83
            'label' => $this->getLabel(),
84
        ));
85
    }
86
}
87