DateTimeFilterType   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 51
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 5
dl 51
loc 51
ccs 22
cts 22
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A bindRequest() 5 5 1
B apply() 25 25 7
A getTemplate() 4 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Kunstmaan\AdminListBundle\AdminList\FilterType\DBAL;
4
5
use DateTime;
6
use Symfony\Component\HttpFoundation\Request;
7
8
/**
9
 * DateTimeFilterType
10
 */
11 View Code Duplication
class DateTimeFilterType extends AbstractDBALFilterType
0 ignored issues
show
Duplication introduced by
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
            /** @var DateTime $datetime */
32 3
            $date = empty($data['value']['date']) ? date('d/m/Y') : $data['value']['date'];
33 3
            $time = empty($data['value']['time']) ? date('H:i') : $data['value']['time'];
34 3
            $dateTime = DateTime::createFromFormat('d/m/Y H:i', $date . ' ' . $time);
35
36 3
            if (false === $dateTime) {
37
                // Failed to create DateTime object.
38 1
                return;
39
            }
40 2
            switch ($data['comparator']) {
41 2
                case 'before':
42 1
                    $this->queryBuilder->andWhere($this->queryBuilder->expr()->lte($this->getAlias() . $this->columnName, ':var_' . $uniqueId));
43
44 1
                    break;
45 1
                case 'after':
46 1
                    $this->queryBuilder->andWhere($this->queryBuilder->expr()->gt($this->getAlias() . $this->columnName, ':var_' . $uniqueId));
47
48 1
                    break;
49
            }
50 2
            $this->queryBuilder->setParameter('var_' . $uniqueId, $dateTime->format('Y-m-d H:i'));
51
        }
52 2
    }
53
54
    /**
55
     * @return string
56
     */
57 1
    public function getTemplate()
58
    {
59 1
        return '@KunstmaanAdminList/FilterType/dateTimeFilter.html.twig';
60
    }
61
}
62