NumberFilter   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A filter() 0 13 5
A getDefaultOptions() 0 4 1
A getRenderSettings() 0 8 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\DoctrineMongoDBAdminBundle\Filter;
15
16
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
17
use Sonata\AdminBundle\Form\Type\Filter\NumberType;
18
use Sonata\AdminBundle\Form\Type\Operator\NumberOperatorType;
19
20
class NumberFilter extends Filter
21
{
22
    private const CHOICES = [
23
        NumberOperatorType::TYPE_EQUAL => 'equals',
24
        NumberOperatorType::TYPE_GREATER_EQUAL => 'gte',
25
        NumberOperatorType::TYPE_GREATER_THAN => 'gt',
26
        NumberOperatorType::TYPE_LESS_EQUAL => 'lte',
27
        NumberOperatorType::TYPE_LESS_THAN => 'lt',
28
    ];
29
30
    public function filter(ProxyQueryInterface $queryBuilder, $alias, $field, $data): void
31
    {
32
        if (!$data || !\is_array($data) || !\array_key_exists('value', $data) || !is_numeric($data['value'])) {
0 ignored issues
show
Bug Best Practice introduced by Romain Norberg
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...
33
            return;
34
        }
35
36
        $type = $data['type'] ?? NumberOperatorType::TYPE_EQUAL;
37
38
        $operator = $this->getOperator((int) $type);
39
40
        $queryBuilder->field($field)->$operator((float) $data['value']);
41
        $this->active = true;
42
    }
43
44
    public function getDefaultOptions()
45
    {
46
        return [];
47
    }
48
49
    public function getRenderSettings()
50
    {
51
        return [NumberType::class, [
52
            'field_type' => $this->getFieldType(),
53
            'field_options' => $this->getFieldOptions(),
54
            'label' => $this->getLabel(),
55
        ]];
56
    }
57
58
    private function getOperator(int $type): string
59
    {
60
        return self::CHOICES[$type] ?? self::CHOICES[NumberOperatorType::TYPE_EQUAL];
61
    }
62
}
63