Completed
Push — 3.x ( 839353...0c5d0b )
by Grégoire
02:14
created

AbstractDateFilter   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 141
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 7
Bugs 1 Features 1
Metric Value
wmc 24
c 7
b 1
f 1
lcom 2
cbo 1
dl 0
loc 141
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
D filter() 0 42 17
A getDefaultOptions() 0 4 1
A getRenderSettings() 0 18 3
A applyType() 0 5 1
A typeRequiresValue() 0 7 1
A getOperator() 0 14 1
1
<?php
2
3
/*
4
 * This file is part of the Sonata Project 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\DoctrineMongoDBAdminBundle\Filter;
13
14
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
15
use Sonata\AdminBundle\Form\Type\Filter\DateType;
16
17
abstract class AbstractDateFilter extends Filter
18
{
19
    /**
20
     * Flag indicating that filter will have range.
21
     *
22
     * @var bool
23
     */
24
    protected $range = false;
25
26
    /**
27
     * Flag indicating that filter will filter by datetime instead by date.
28
     *
29
     * @var bool
30
     */
31
    protected $time = false;
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    public function filter(ProxyQueryInterface $queryBuilder, $alias, $field, $data)
37
    {
38
        //check data sanity
39
        if (is_array($data) !== true) {
40
            return;
41
        }
42
43
        //default type for simple filter
44
        $data['type'] = !isset($data['type']) || !is_numeric($data['type']) ? DateType::TYPE_EQUAL : $data['type'];
45
46
        // Some types do not require a value to be set (NULL, NOT NULL).
47
        if (!$this->typeRequiresValue($data['type']) && !$data['value']) {
48
            return;
49
        }
50
51
        switch ($data['type']) {
52
            case DateType::TYPE_EQUAL:
53
                return $this->applyTypeIsEqual($queryBuilder, $field, $data);
0 ignored issues
show
Bug introduced by
The method applyTypeIsEqual() does not exist on Sonata\DoctrineMongoDBAd...lter\AbstractDateFilter. Did you maybe mean applyType()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
54
55
            case DateType::TYPE_GREATER_THAN:
56
                if (!array_key_exists('value', $data) || !$data['value']) {
57
                    return;
58
                }
59
60
                return $this->applyTypeIsGreaterThan($queryBuilder, $field, $data);
0 ignored issues
show
Bug introduced by
The method applyTypeIsGreaterThan() does not exist on Sonata\DoctrineMongoDBAd...lter\AbstractDateFilter. Did you maybe mean applyType()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
61
62
            case DateType::TYPE_LESS_EQUAL:
63
                if (!array_key_exists('value', $data) || !$data['value']) {
64
                    return;
65
                }
66
67
                return $this->applyTypeIsLessEqual($queryBuilder, $field, $data);
0 ignored issues
show
Bug introduced by
The method applyTypeIsLessEqual() does not exist on Sonata\DoctrineMongoDBAd...lter\AbstractDateFilter. Did you maybe mean applyType()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
68
69
            case DateType::TYPE_NULL:
70
            case DateType::TYPE_NOT_NULL:
71
                return $this->applyType($queryBuilder, $this->getOperator($data['type']), $field, null);
72
73
            case DateType::TYPE_GREATER_EQUAL:
74
            case DateType::TYPE_LESS_THAN:
75
                return $this->applyType($queryBuilder, $this->getOperator($data['type']), $field, $data['value']);
0 ignored issues
show
Documentation introduced by
$data['value'] is of type string, but the function expects a null|object<DateTime>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
76
        }
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82
    public function getDefaultOptions()
83
    {
84
        return array('input_type' => 'datetime');
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90
    public function getRenderSettings()
91
    {
92
        $name = 'sonata_type_filter_date';
93
94
        if ($this->time) {
95
            $name .= 'time';
96
        }
97
98
        if ($this->range) {
99
            $name .= '_range';
100
        }
101
102
        return array($name, array(
103
                'field_type' => $this->getFieldType(),
104
                'field_options' => $this->getFieldOptions(),
105
                'label' => $this->getLabel(),
106
        ));
107
    }
108
109
    /**
110
     * @param \Sonata\AdminBundle\Datagrid\ProxyQueryInterface $queryBuilder
111
     * @param string                                           $operation
112
     * @param string                                           $field
113
     * @param \DateTime                                        $datetime
114
     */
115
    protected function applyType(ProxyQueryInterface $queryBuilder, $operation, $field, \DateTime $datetime = null)
116
    {
117
        $queryBuilder->field($field)->$operation($datetime);
118
        $this->active = true;
119
    }
120
121
    /**
122
     * Returns if the filter type requires a value to be set.
123
     *
124
     * @param int $type
125
     *
126
     * @return bool
127
     */
128
    protected function typeRequiresValue($type)
129
    {
130
        return in_array($type, array(
131
                    DateType::TYPE_NULL,
132
                    DateType::TYPE_NOT_NULL, )
133
        );
134
    }
135
136
    /**
137
     * Resolves DataType:: constants to MongoDb operators.
138
     *
139
     * @param int $type
140
     *
141
     * @return string
142
     */
143
    protected function getOperator($type)
144
    {
145
        $choices = array(
146
            DateType::TYPE_NULL => 'equals',
147
            DateType::TYPE_NOT_NULL => 'notEqual',
148
            DateType::TYPE_EQUAL => 'equals',
149
            DateType::TYPE_GREATER_EQUAL => 'gte',
150
            DateType::TYPE_GREATER_THAN => 'gt',
151
            DateType::TYPE_LESS_EQUAL => 'lte',
152
            DateType::TYPE_LESS_THAN => 'lt',
153
        );
154
155
        return $choices[intval($type)];
156
    }
157
}
158