MoneyAmountFilter::new()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 8
rs 10
cc 1
nc 1
nop 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\Type\ComparisonType;
28
29
/**
30
 * Similar to default Numeric filter, but ibput will be.
31
 */
32
class MoneyAmountFilter implements FilterInterface
33
{
34
    use FilterTrait;
35
36
    public static function new(string $propertyName, $label = null): self
37
    {
38
        return (new self())
39
            ->setFilterFqcn(__CLASS__)
40
            ->setProperty($propertyName)
41
            ->setLabel($label)
42
            ->setFormType(MoneyAmountFilterType::class)
43
            ->setFormTypeOption('translation_domain', 'EasyAdminBundle');
44
    }
45
46
    public function apply(QueryBuilder $queryBuilder, FilterDataDto $filterDataDto, ?FieldDto $fieldDto, EntityDto $entityDto): void
47
    {
48
        $alias = $filterDataDto->getEntityAlias();
49
        $property = $filterDataDto->getProperty();
50
        $comparison = $filterDataDto->getComparison();
51
        $parameterName = $filterDataDto->getParameterName();
52
        $parameter2Name = $filterDataDto->getParameter2Name();
53
        $value = $filterDataDto->getValue();
54
        $value2 = $filterDataDto->getValue2();
55
56
        if (ComparisonType::BETWEEN === $comparison) {
57
            $queryBuilder->andWhere(sprintf('%s.%s BETWEEN :%s and :%s', $alias, $property, $parameterName, $parameter2Name))
58
                ->setParameter($parameterName, $value)
59
                ->setParameter($parameter2Name, $value2);
60
        } else {
61
            $queryBuilder->andWhere(sprintf('%s.%s %s :%s', $alias, $property, $comparison, $parameterName))
62
                ->setParameter($parameterName, $value);
63
        }
64
    }
65
}
66