Completed
Push — master ( ae5e03...0447ee )
by Jeroen
10:35 queued 04:37
created

DateFilterType::apply()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 23

Duplication

Lines 23
Ratio 100 %

Code Coverage

Tests 15
CRAP Score 5

Importance

Changes 0
Metric Value
dl 23
loc 23
ccs 15
cts 15
cp 1
rs 9.2408
c 0
b 0
f 0
cc 5
nc 5
nop 2
crap 5
1
<?php
2
3
namespace Kunstmaan\AdminListBundle\AdminList\FilterType\DBAL;
4
5
use DateTime;
6
use Symfony\Component\HttpFoundation\Request;
7
8
/**
9
 * DateFilterType
10
 */
11 View Code Duplication
class DateFilterType extends AbstractDBALFilterType
0 ignored issues
show
Duplication introduced by Daan Poron
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
12
{
13
    /**
14
     * @param Request $request  The request
15
     * @param array   &$data    The data
16
     * @param string  $uniqueId The unique identifier
17
     */
18 1
    public function bindRequest(Request $request, array &$data, $uniqueId)
19
    {
20 1
        $data['comparator'] = $request->query->get('filter_comparator_' . $uniqueId);
21 1
        $data['value'] = $request->query->get('filter_value_' . $uniqueId);
22 1
    }
23
24
    /**
25
     * @param array  $data     The data
26
     * @param string $uniqueId The unique identifier
27
     */
28 3
    public function apply(array $data, $uniqueId)
29
    {
30 3
        if (isset($data['value'], $data['comparator'])) {
31 3
            $dateTime = DateTime::createFromFormat('d/m/Y', $data['value']);
32
33 3
            if (false === $dateTime) {
34
                // Failed to create DateTime object.
35 1
                return;
36
            }
37 2
            $date = $dateTime->format('Y-m-d');
38 2
            switch ($data['comparator']) {
39 2
                case 'before':
40 1
                    $this->queryBuilder->andWhere($this->queryBuilder->expr()->lte($this->getAlias() . $this->columnName, ':var_' . $uniqueId));
41
42 1
                    break;
43 1
                case 'after':
44 1
                    $this->queryBuilder->andWhere($this->queryBuilder->expr()->gt($this->getAlias() . $this->columnName, ':var_' . $uniqueId));
45
46 1
                    break;
47
            }
48 2
            $this->queryBuilder->setParameter('var_' . $uniqueId, $date);
49
        }
50 2
    }
51
52
    /**
53
     * @return string
54
     */
55 1
    public function getTemplate()
56
    {
57 1
        return '@KunstmaanAdminList/FilterType/dateFilter.html.twig';
58
    }
59
}
60