Completed
Push — master ( c17fc1...538d13 )
by
unknown
01:51 queued 12s
created

ClassFilter   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 1
dl 0
loc 62
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A filter() 0 15 5
A getDefaultOptions() 0 7 1
A getFieldType() 0 4 1
A getFieldOptions() 0 9 1
A getRenderSettings() 0 10 1
A getOperator() 0 4 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\DoctrineORMAdminBundle\Filter;
15
16
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
17
use Sonata\AdminBundle\Form\Type\Filter\DefaultType;
18
use Sonata\AdminBundle\Form\Type\Operator\EqualOperatorType;
19
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
20
21
class ClassFilter extends Filter
22
{
23
    public const CHOICES = [
24
        EqualOperatorType::TYPE_EQUAL => 'INSTANCE OF',
25
        EqualOperatorType::TYPE_NOT_EQUAL => 'NOT INSTANCE OF',
26
    ];
27
28
    public function filter(ProxyQueryInterface $queryBuilder, $alias, $field, $data): void
29
    {
30
        if (!$data || !\is_array($data) || !\array_key_exists('value', $data)) {
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...
31
            return;
32
        }
33
34
        if (0 === \strlen($data['value'])) {
35
            return;
36
        }
37
38
        $type = $data['type'] ?? EqualOperatorType::TYPE_EQUAL;
39
        $operator = $this->getOperator((int) $type);
40
41
        $this->applyWhere($queryBuilder, sprintf('%s %s %s', $alias, $operator, $data['value']));
42
    }
43
44
    public function getDefaultOptions()
45
    {
46
        return [
47
            'operator_type' => EqualOperatorType::class,
48
            'operator_options' => [],
49
        ];
50
    }
51
52
    public function getFieldType()
53
    {
54
        return $this->getOption('field_type', ChoiceType::class);
55
    }
56
57
    public function getFieldOptions()
58
    {
59
        $choiceOptions = [
60
            'required' => false,
61
            'choices' => $this->getOption('sub_classes'),
62
        ];
63
64
        return $this->getOption('choices', $choiceOptions);
65
    }
66
67
    public function getRenderSettings()
68
    {
69
        return [DefaultType::class, [
70
            'operator_type' => $this->getOption('operator_type'),
71
            'operator_options' => $this->getOption('operator_options'),
72
            'field_type' => $this->getFieldType(),
73
            'field_options' => $this->getFieldOptions(),
74
            'label' => $this->getLabel(),
75
        ]];
76
    }
77
78
    private function getOperator(int $type): string
79
    {
80
        return self::CHOICES[$type] ?? self::CHOICES[EqualOperatorType::TYPE_EQUAL];
81
    }
82
}
83