Completed
Push — 1.2 ( d3ea0d...d8c512 )
by David
16:21
created

NumberFilter   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 1
Bugs 1 Features 1
Metric Value
wmc 13
c 1
b 1
f 1
lcom 2
cbo 1
dl 0
loc 58
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getDefaultOptions() 0 4 1
C filter() 0 32 11
A getRenderSettings() 0 8 1
1
<?php
2
3
/*
4
 * This file is part of the Sonata 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\DoctrinePHPCRAdminBundle\Filter;
13
14
use Doctrine\ODM\PHPCR\Query\ExpressionBuilder;
15
use Doctrine\ODM\PHPCR\Query\Expression\Comparison;
16
use Sonata\AdminBundle\Form\Type\Filter\NumberType;
17
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
18
19
class NumberFilter extends Filter
20
{
21
    /**
22
     * {@inheritDoc}
23
     */
24
    public function filter(ProxyQueryInterface $proxyQuery, $alias, $field, $data)
25
    {
26
        if (!$data || !is_array($data) || !array_key_exists('value', $data) || !is_numeric($data['value'])) {
27
            return;
28
        }
29
30
        $type = isset($data['type']) ? $data['type'] : false;
31
        $where = $this->getWhere($proxyQuery);
0 ignored issues
show
Compatibility introduced by
$proxyQuery of type object<Sonata\AdminBundl...id\ProxyQueryInterface> is not a sub-type of object<Sonata\DoctrinePH...le\Datagrid\ProxyQuery>. It seems like you assume a concrete implementation of the interface Sonata\AdminBundle\Datagrid\ProxyQueryInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
32
33
        $value = $data['value'];
34
35
        switch ($type) {
36
            case NumberType::TYPE_GREATER_EQUAL:
37
                $where->gte()->field('a.'.$field)->literal($value);
38
                break;
39
            case NumberType::TYPE_GREATER_THAN:
40
                $where->gt()->field('a.'.$field)->literal($value);
41
                break;
42
            case NumberType::TYPE_LESS_EQUAL:
43
                $where->lte()->field('a.'.$field)->literal($value);
44
                break;
45
            case NumberType::TYPE_LESS_THAN:
46
                $where->lt()->field('a.'.$field)->literal($value);
47
                break;
48
            case NumberType::TYPE_EQUAL:
49
            default:
50
                $where->eq()->field('a.'.$field)->literal($value);
51
        }
52
53
        // filter is active as we have now modified the query
54
        $this->active = true;
55
    }
56
57
    /**
58
     * {@inheritDoc}
59
     */
60
    public function getDefaultOptions()
61
    {
62
        return array();
63
    }
64
65
    /**
66
     * {@inheritDoc}
67
     */
68
    public function getRenderSettings()
69
    {
70
        return array('sonata_type_filter_number', array(
71
            'field_type'    => $this->getFieldType(),
72
            'field_options' => $this->getFieldOptions(),
73
            'label'         => $this->getLabel()
74
        ));
75
    }
76
}
77