BooleanFilter   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 0
Metric Value
wmc 14
lcom 2
cbo 1
dl 0
loc 60
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
C filter() 0 33 12
A getDefaultOptions() 0 4 1
A getRenderSettings() 0 10 1
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) ? true : false;
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'] ? true : false;
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