Completed
Push — 3.x ( 839353...0c5d0b )
by Grégoire
02:14
created

ChoiceFilter::filter()   C

Complexity

Conditions 14
Paths 8

Size

Total Lines 36
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 36
rs 5.0864
cc 14
eloc 21
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\DoctrineMongoDBAdminBundle\Filter;
13
14
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
15
use Sonata\AdminBundle\Form\Type\Filter\ChoiceType;
16
17
class ChoiceFilter extends Filter
18
{
19
    /**
20
     * @param ProxyQueryInterface $queryBuilder
21
     * @param string              $alias
22
     * @param string              $field
23
     * @param mixed               $data
24
     *
25
     * @return
26
     */
27
    public function filter(ProxyQueryInterface $queryBuilder, $alias, $field, $data)
28
    {
29
        if (!$data || !is_array($data) || !array_key_exists('type', $data) || !array_key_exists('value', $data)) {
30
            return;
31
        }
32
33
        if (is_array($data['value'])) {
34
            if (count($data['value']) == 0) {
35
                return;
36
            }
37
38
            if (in_array('all', $data['value'])) {
39
                return;
40
            }
41
42
            if ($data['type'] == ChoiceType::TYPE_NOT_CONTAINS) {
43
                $queryBuilder->field($field)->notIn($data['value']);
44
            } else {
45
                $queryBuilder->field($field)->in($data['value']);
46
            }
47
48
            $this->active = true;
49
        } else {
50
            if ($data['value'] === '' || $data['value'] === null || $data['value'] === false || $data['value'] === 'all') {
51
                return;
52
            }
53
54
            if ($data['type'] == ChoiceType::TYPE_NOT_CONTAINS) {
55
                $queryBuilder->field($field)->notEqual($data['value']);
56
            } else {
57
                $queryBuilder->field($field)->equals($data['value']);
58
            }
59
60
            $this->active = true;
61
        }
62
    }
63
64
    /**
65
     * @return array
66
     */
67
    public function getDefaultOptions()
68
    {
69
        return array();
70
    }
71
72
    public function getRenderSettings()
73
    {
74
        return array('sonata_type_filter_default', array(
75
                'operator_type' => 'sonata_type_boolean',
76
                'field_type' => $this->getFieldType(),
77
                'field_options' => $this->getFieldOptions(),
78
                'label' => $this->getLabel(),
79
        ));
80
    }
81
}
82