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

DateFilterType   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 49
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 5
dl 49
loc 49
ccs 21
cts 21
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A bindRequest() 5 5 1
A apply() 23 23 5
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
 * 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