Completed
Push — composer-autoload ( 2ad06b )
by Kamil
18:19
created

DateFilter   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 2
dl 0
loc 42
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A apply() 0 16 4
A getDateTime() 0 12 3
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
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 Sylius\Component\Grid\Filter;
13
14
use Sylius\Component\Grid\Data\DataSourceInterface;
15
use Sylius\Component\Grid\Filtering\FilterInterface;
16
17
/**
18
 * @author Grzegorz Sadowski <[email protected]>
19
 */
20
final class DateFilter implements FilterInterface
21
{
22
    const NAME = 'date';
23
24
    /**
25
     * {@inheritdoc}
26
     */
27
    public function apply(DataSourceInterface $dataSource, $name, $data, array $options)
28
    {
29
        $expressionBuilder = $dataSource->getExpressionBuilder();
30
31
        $field = isset($options['field']) ? $options['field'] : $name;
32
33
        $from = $this->getDateTime($data['from']);
34
        if (null !== $from) {
35
            $expressionBuilder->greaterThanOrEqual($field, $from);
36
        }
37
38
        $to = $this->getDateTime($data['to']);
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $to. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
39
        if (null !== $to) {
40
            $expressionBuilder->lessThan($field, $to);
41
        }
42
    }
43
44
    /**
45
     * @param string[] $data
46
     *
47
     * @return null|string
48
     */
49
    private function getDateTime(array $data)
50
    {
51
        if (empty($data['date'])) {
52
            return null;
53
        }
54
55
        if (empty($data['time'])) {
56
            return $data['date'];
57
        }
58
59
        return $data['date'].' '.$data['time'];
60
    }
61
}
62