Completed
Pull Request — 3.x (#252)
by
unknown
01:38
created

AbstractDateFilter   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 152
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

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

6 Methods

Rating   Name   Duplication   Size   Complexity  
D filter() 0 42 17
A getDefaultOptions() 0 4 1
B getRenderSettings() 0 29 4
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 (true !== is_array($data)) {
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']);
76
        }
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82
    public function getDefaultOptions()
83
    {
84
        return ['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
        // NEXT_MAJOR: Remove this line when drop Symfony <2.8 support
103
        if (method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix')) {
104
            $classnames = [
105
                'sonata_type_filter_date' => 'Sonata\AdminBundle\Form\Type\Filter\DateType',
106
                'sonata_type_filter_date_range' => 'Sonata\AdminBundle\Form\Type\Filter\DateRangeType',
107
                'sonata_type_filter_datetime' => 'Sonata\AdminBundle\Form\Type\Filter\DateTimeType',
108
                'sonata_type_filter_datetime_range' => 'Sonata\AdminBundle\Form\Type\Filter\DateTimeRangeType',
109
            ];
110
            $name = $classnames[$name];
111
        }
112
113
        return [$name, [
114
            'field_type' => $this->getFieldType(),
115
            'field_options' => $this->getFieldOptions(),
116
            'label' => $this->getLabel(),
117
        ]];
118
    }
119
120
    /**
121
     * @param \Sonata\AdminBundle\Datagrid\ProxyQueryInterface $queryBuilder
122
     * @param string                                           $operation
123
     * @param string                                           $field
124
     * @param \DateTime                                        $datetime
125
     */
126
    protected function applyType(ProxyQueryInterface $queryBuilder, $operation, $field, \DateTime $datetime = null)
127
    {
128
        $queryBuilder->field($field)->$operation($datetime);
129
        $this->active = true;
130
    }
131
132
    /**
133
     * Returns if the filter type requires a value to be set.
134
     *
135
     * @param int $type
136
     *
137
     * @return bool
138
     */
139
    protected function typeRequiresValue($type)
140
    {
141
        return in_array($type, [
142
                    DateType::TYPE_NULL,
143
                    DateType::TYPE_NOT_NULL, ]
144
        );
145
    }
146
147
    /**
148
     * Resolves DataType:: constants to MongoDb operators.
149
     *
150
     * @param int $type
151
     *
152
     * @return string
153
     */
154
    protected function getOperator($type)
155
    {
156
        $choices = [
157
            DateType::TYPE_NULL => 'equals',
158
            DateType::TYPE_NOT_NULL => 'notEqual',
159
            DateType::TYPE_EQUAL => 'equals',
160
            DateType::TYPE_GREATER_EQUAL => 'gte',
161
            DateType::TYPE_GREATER_THAN => 'gt',
162
            DateType::TYPE_LESS_EQUAL => 'lte',
163
            DateType::TYPE_LESS_THAN => 'lt',
164
        ];
165
166
        return $choices[intval($type)];
167
    }
168
}
169