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

StringFilterType::apply()   B

Complexity

Conditions 9
Paths 9

Size

Total Lines 46

Duplication

Lines 46
Ratio 100 %

Code Coverage

Tests 28
CRAP Score 9.648

Importance

Changes 0
Metric Value
dl 46
loc 46
ccs 28
cts 35
cp 0.8
rs 7.6226
c 0
b 0
f 0
cc 9
nc 9
nop 2
crap 9.648
1
<?php
2
3
namespace Kunstmaan\AdminListBundle\AdminList\FilterType\ORM;
4
5
use Symfony\Component\HttpFoundation\Request;
6
7
/**
8
 * StringFilterType
9
 */
10 View Code Duplication
class StringFilterType extends AbstractORMFilterType
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...
11
{
12
    /**
13
     * @param Request $request
14
     * @param array   &$data
15
     * @param string  $uniqueId
16
     */
17 1
    public function bindRequest(Request $request, array &$data, $uniqueId)
18
    {
19 1
        $data['comparator'] = $request->query->get('filter_comparator_'.$uniqueId);
20 1
        $data['value'] = $request->query->get('filter_value_'.$uniqueId);
21 1
    }
22
23
    /**
24
     * @param array  $data
25
     * @param string $uniqueId
26
     */
27 6
    public function apply(array $data, $uniqueId)
28
    {
29 6
        if (isset($data['value'], $data['comparator'])) {
30 6
            switch ($data['comparator']) {
31 6
                case 'equals':
32 1
                    $this->queryBuilder->andWhere($this->queryBuilder->expr()->eq($this->getAlias().$this->columnName, ':var_'.$uniqueId));
33 1
                    $this->queryBuilder->setParameter('var_'.$uniqueId, $data['value']);
34
35 1
                    break;
36 5
                case 'notequals':
37 1
                    $this->queryBuilder->andWhere($this->queryBuilder->expr()->neq($this->getAlias().$this->columnName, ':var_'.$uniqueId));
38 1
                    $this->queryBuilder->setParameter('var_'.$uniqueId, $data['value']);
39
40 1
                    break;
41 4
                case 'contains':
42 1
                    $this->queryBuilder->andWhere($this->queryBuilder->expr()->like($this->getAlias().$this->columnName, ':var_'.$uniqueId));
43 1
                    $this->queryBuilder->setParameter('var_'.$uniqueId, '%'.$data['value'].'%');
44
45 1
                    break;
46 3
                case 'doesnotcontain':
47 1
                    $this->queryBuilder->andWhere($this->getAlias().$this->columnName.' NOT LIKE :var_'.$uniqueId);
48 1
                    $this->queryBuilder->setParameter('var_'.$uniqueId, '%'.$data['value'].'%');
49
50 1
                    break;
51 2
                case 'startswith':
52 1
                    $this->queryBuilder->andWhere($this->queryBuilder->expr()->like($this->getAlias().$this->columnName, ':var_'.$uniqueId));
53 1
                    $this->queryBuilder->setParameter('var_'.$uniqueId, $data['value'].'%');
54
55 1
                    break;
56 1
                case 'endswith':
57 1
                    $this->queryBuilder->andWhere($this->queryBuilder->expr()->like($this->getAlias().$this->columnName, ':var_'.$uniqueId));
58 1
                    $this->queryBuilder->setParameter('var_'.$uniqueId, '%'.$data['value']);
59
60 1
                    break;
61
                case 'empty':
62
                    $this->queryBuilder->andWhere($this->queryBuilder->expr()->orX(
63
                        $this->queryBuilder->expr()->isNull($this->getAlias().$this->columnName),
64
                        $this->queryBuilder->expr()->eq($this->getAlias().$this->columnName, '\'-\''),
65
                        $this->queryBuilder->expr()->eq($this->getAlias().$this->columnName, ':var_empty_'.$uniqueId)
66
                    ));
67
                    $this->queryBuilder->setParameter('var_empty_'.$uniqueId, '');
68
69
                    break;
70
            }
71
        }
72 6
    }
73
74
    /**
75
     * @return string
76
     */
77 1
    public function getTemplate()
78
    {
79 1
        return '@KunstmaanAdminList/FilterType/stringFilter.html.twig';
80
    }
81
}
82