Completed
Push — fix_phpstorm ( 2fbcbe...0ecc15 )
by
unknown
01:21
created

AbstractDateFilter   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 174
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

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

9 Methods

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