ConfirmedFilter   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 3
eloc 18
c 2
b 0
f 1
dl 0
loc 25
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A new() 0 7 1
A apply() 0 12 2
1
<?php
2
/*
3
 * Copyright (C) 2020  Jan Böhmer
4
 *
5
 * This program is free software: you can redistribute it and/or modify
6
 * it under the terms of the GNU Affero General Public License as published
7
 * by the Free Software Foundation, either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU Affero General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU Affero General Public License
16
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
17
 */
18
19
namespace App\Admin\Filter;
20
21
use Doctrine\ORM\QueryBuilder;
22
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Filter\FilterInterface;
23
use EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto;
24
use EasyCorp\Bundle\EasyAdminBundle\Dto\FieldDto;
25
use EasyCorp\Bundle\EasyAdminBundle\Dto\FilterDataDto;
26
use EasyCorp\Bundle\EasyAdminBundle\Filter\FilterTrait;
27
use EasyCorp\Bundle\EasyAdminBundle\Form\Filter\Type\BooleanFilterType;
28
29
class ConfirmedFilter implements FilterInterface
30
{
31
    use FilterTrait;
32
33
    public static function new(string $propertyName, $label = null): self
34
    {
35
        return (new self())
36
            ->setFilterFqcn(__CLASS__)
37
            ->setProperty($propertyName)
38
            ->setLabel($label)
39
            ->setFormType(BooleanFilterType::class);
40
    }
41
42
    public function apply(QueryBuilder $queryBuilder, FilterDataDto $filterDataDto, ?FieldDto $fieldDto, EntityDto $entityDto): void
43
    {
44
        if (false === $filterDataDto->getValue()) {
45
            $comparison = 'IS NULL';
46
            $queryBuilder
47
                ->andWhere(sprintf('%s.%s %s', $filterDataDto->getEntityAlias(), 'confirm1_timestamp', $comparison))
48
                ->orWhere(sprintf('%s.%s %s', $filterDataDto->getEntityAlias(), 'confirm2_timestamp', $comparison));
49
        } else {
50
            $comparison = 'IS NOT NULL';
51
            $queryBuilder
52
                ->andWhere(sprintf('%s.%s %s', $filterDataDto->getEntityAlias(), 'confirm1_timestamp', $comparison))
53
                ->andWhere(sprintf('%s.%s %s', $filterDataDto->getEntityAlias(), 'confirm2_timestamp', $comparison));
54
        }
55
    }
56
}
57