Completed
Push — master ( 2975e7...46c3ee )
by Rafael
07:49
created

DateFilter   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 11
dl 0
loc 48
ccs 29
cts 29
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B __invoke() 0 43 11
1
<?php
2
/*******************************************************************************
3
 *  This file is part of the GraphQL Bundle package.
4
 *
5
 *  (c) YnloUltratech <[email protected]>
6
 *
7
 *  For the full copyright and license information, please view the LICENSE
8
 *  file that was distributed with this source code.
9
 ******************************************************************************/
10
11
namespace Ynlo\GraphQLBundle\Filter\Common;
12
13
use Doctrine\ORM\QueryBuilder;
14
use Ynlo\GraphQLBundle\Filter\FilterContext;
15
use Ynlo\GraphQLBundle\Filter\FilterInterface;
16
use Ynlo\GraphQLBundle\Model\Filter\DateComparisonExpression;
17
use Ynlo\GraphQLBundle\Type\DateComparisonOperatorType;
18
19
class DateFilter implements FilterInterface
20
{
21
    /**
22
     * @inheritDoc
23
     */
24 8
    public function __invoke(FilterContext $context, QueryBuilder $qb, $condition)
25
    {
26 8
        if (!$condition instanceof DateComparisonExpression) {
27 1
            throw new \RuntimeException('Invalid filter condition');
28
        }
29
30 7
        if (!$context->getField() || !$context->getField()->getName()) {
31 1
            throw new \RuntimeException('There are not valid field related to this filter.');
32
        }
33
34 6
        $alias = $qb->getRootAliases()[0];
35 6
        $column = $context->getField()->getOriginName();
36 6
        $operator = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $operator is dead and can be removed.
Loading history...
37
38 6
        $date = $condition->getDate()->format('Y-m-d H:i:s');
39 6
        $maxDate = $date;
40 6
        if ($condition->getMaxDate()) {
41 2
            $maxDate = $condition->getMaxDate()->format('Y-m-d H:i:s');
42
        }
43
44 6
        switch ($condition->getOp()) {
45 6
            case DateComparisonOperatorType::AFTER:
46 2
                if ($condition->isStrict()) {
47 1
                    $qb->andWhere($qb->expr()->gt("{$alias}.{$column}", "'$date'"));
48
                } else {
49 1
                    $qb->andWhere($qb->expr()->gte("{$alias}.{$column}", "'$date'"));
50
                }
51 2
                break;
52 4
            case DateComparisonOperatorType::BEFORE:
53 2
                if ($condition->isStrict()) {
54 1
                    $qb->andWhere($qb->expr()->lt("{$alias}.{$column}", "'$date'"));
55
                } else {
56 1
                    $qb->andWhere($qb->expr()->lte("{$alias}.{$column}", "'$date'"));
57
                }
58 2
                break;
59 2
            case DateComparisonOperatorType::BETWEEN:
60 2
                if ($condition->isStrict()) {
61 1
                    $qb->andWhere($qb->expr()->gt("{$alias}.{$column}", "'$date'"));
62 1
                    $qb->andWhere($qb->expr()->lt("{$alias}.{$column}", "'$maxDate'"));
63
                } else {
64 1
                    $qb->andWhere($qb->expr()->between("{$alias}.{$column}", "'$date'", "'$maxDate'"));
65
                }
66 2
                break;
67
        }
68 6
    }
69
}
70