Completed
Pull Request — 3.x (#252)
by
unknown
05:50
created

AbstractDateFilter::filter()   D

Complexity

Conditions 17
Paths 45

Size

Total Lines 42
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 42
rs 4.9807
c 0
b 0
f 0
cc 17
eloc 23
nc 45
nop 4

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\DateRangeType;
16
use Sonata\AdminBundle\Form\Type\Filter\DateTimeRangeType;
17
use Sonata\AdminBundle\Form\Type\Filter\DateTimeType;
18
use Sonata\AdminBundle\Form\Type\Filter\DateType;
19
20
abstract class AbstractDateFilter extends Filter
21
{
22
    /**
23
     * Flag indicating that filter will have range.
24
     *
25
     * @var bool
26
     */
27
    protected $range = false;
28
29
    /**
30
     * Flag indicating that filter will filter by datetime instead by date.
31
     *
32
     * @var bool
33
     */
34
    protected $time = false;
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function filter(ProxyQueryInterface $queryBuilder, $alias, $field, $data)
40
    {
41
        //check data sanity
42
        if (true !== is_array($data)) {
43
            return;
44
        }
45
46
        //default type for simple filter
47
        $data['type'] = !isset($data['type']) || !is_numeric($data['type']) ? DateType::TYPE_EQUAL : $data['type'];
48
49
        // Some types do not require a value to be set (NULL, NOT NULL).
50
        if (!$this->typeRequiresValue($data['type']) && !$data['value']) {
51
            return;
52
        }
53
54
        switch ($data['type']) {
55
            case DateType::TYPE_EQUAL:
56
                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...
57
58
            case DateType::TYPE_GREATER_THAN:
59
                if (!array_key_exists('value', $data) || !$data['value']) {
60
                    return;
61
                }
62
63
                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...
64
65
            case DateType::TYPE_LESS_EQUAL:
66
                if (!array_key_exists('value', $data) || !$data['value']) {
67
                    return;
68
                }
69
70
                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...
71
72
            case DateType::TYPE_NULL:
73
            case DateType::TYPE_NOT_NULL:
74
                return $this->applyType($queryBuilder, $this->getOperator($data['type']), $field, null);
75
76
            case DateType::TYPE_GREATER_EQUAL:
77
            case DateType::TYPE_LESS_THAN:
78
                return $this->applyType($queryBuilder, $this->getOperator($data['type']), $field, $data['value']);
79
        }
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85
    public function getDefaultOptions()
86
    {
87
        return ['input_type' => 'datetime'];
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93
    public function getRenderSettings()
94
    {
95
        $name = 'sonata_type_filter_date';
96
97
        if ($this->time) {
98
            $name .= 'time';
99
        }
100
101
        if ($this->range) {
102
            $name .= '_range';
103
        }
104
105
        // NEXT_MAJOR: Remove this line when drop Symfony <2.8 support
106
        if (method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix')) {
107
            $classnames = [
108
                'sonata_type_filter_date' => DateType::class,
109
                'sonata_type_filter_date_range' => DateRangeType::class,
110
                'sonata_type_filter_datetime' => DateTimeType::class,
111
                'sonata_type_filter_datetime_range' => DateTimeRangeType::class,
112
            ];
113
            $name = $classnames[$name];
114
        }
115
116
        return [$name, [
117
            'field_type' => $this->getFieldType(),
118
            'field_options' => $this->getFieldOptions(),
119
            'label' => $this->getLabel(),
120
        ]];
121
    }
122
123
    /**
124
     * @param \Sonata\AdminBundle\Datagrid\ProxyQueryInterface $queryBuilder
125
     * @param string                                           $operation
126
     * @param string                                           $field
127
     * @param \DateTime                                        $datetime
128
     */
129
    protected function applyType(ProxyQueryInterface $queryBuilder, $operation, $field, \DateTime $datetime = null)
130
    {
131
        $queryBuilder->field($field)->$operation($datetime);
132
        $this->active = true;
133
    }
134
135
    /**
136
     * Returns if the filter type requires a value to be set.
137
     *
138
     * @param int $type
139
     *
140
     * @return bool
141
     */
142
    protected function typeRequiresValue($type)
143
    {
144
        return in_array($type, [
145
                    DateType::TYPE_NULL,
146
                    DateType::TYPE_NOT_NULL, ]
147
        );
148
    }
149
150
    /**
151
     * Resolves DataType:: constants to MongoDb operators.
152
     *
153
     * @param int $type
154
     *
155
     * @return string
156
     */
157
    protected function getOperator($type)
158
    {
159
        $choices = [
160
            DateType::TYPE_NULL => 'equals',
161
            DateType::TYPE_NOT_NULL => 'notEqual',
162
            DateType::TYPE_EQUAL => 'equals',
163
            DateType::TYPE_GREATER_EQUAL => 'gte',
164
            DateType::TYPE_GREATER_THAN => 'gt',
165
            DateType::TYPE_LESS_EQUAL => 'lte',
166
            DateType::TYPE_LESS_THAN => 'lt',
167
        ];
168
169
        return $choices[intval($type)];
170
    }
171
}
172