BooleanFilter::filter()   B
last analyzed

Complexity

Conditions 10
Paths 9

Size

Total Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 33
rs 7.6666
c 0
b 0
f 0
cc 10
nc 9
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
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\DoctrineMongoDBAdminBundle\Filter;
15
16
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
17
use Sonata\AdminBundle\Form\Type\Filter\DefaultType;
18
use Sonata\Form\Type\BooleanType;
19
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
20
21
class BooleanFilter extends Filter
22
{
23
    /**
24
     * @param string $alias
25
     * @param string $field
26
     * @param mixed  $data
27
     */
28
    public function filter(ProxyQueryInterface $queryBuilder, $alias, $field, $data): void
29
    {
30
        if (!$data || !\is_array($data) || !\array_key_exists('type', $data) || !\array_key_exists('value', $data)) {
31
            return;
32
        }
33
34
        if (\is_array($data['value'])) {
35
            $values = [];
36
            foreach ($data['value'] as $v) {
37
                if (!\in_array($v, [BooleanType::TYPE_NO, BooleanType::TYPE_YES], true)) {
38
                    continue;
39
                }
40
41
                $values[] = BooleanType::TYPE_YES === $v;
42
            }
43
44
            if (0 === \count($values)) {
45
                return;
46
            }
47
48
            $queryBuilder->field($field)->in($values);
49
            $this->active = true;
50
        } else {
51
            if (!\in_array($data['value'], [BooleanType::TYPE_NO, BooleanType::TYPE_YES], true)) {
52
                return;
53
            }
54
55
            $value = BooleanType::TYPE_YES === $data['value'];
56
57
            $queryBuilder->field($field)->equals($value);
58
            $this->active = true;
59
        }
60
    }
61
62
    /**
63
     * @return array
64
     */
65
    public function getDefaultOptions()
66
    {
67
        return [];
68
    }
69
70
    public function getRenderSettings()
71
    {
72
        return [DefaultType::class, [
73
            'field_type' => $this->getFieldType(),
74
            'field_options' => $this->getFieldOptions(),
75
            'operator_type' => HiddenType::class,
76
            'operator_options' => [],
77
            'label' => $this->getLabel(),
78
        ]];
79
    }
80
}
81