Completed
Push — ezp-29724 ( 1025e5...495423 )
by
unknown
30:13 queued 07:05
created

Handler::handle()   B

Complexity

Conditions 10
Paths 10

Size

Total Lines 48

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
nc 10
nop 3
dl 0
loc 48
rs 7.2678
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * File containing the DoctrineDatabase base field value Handler class.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 */
9
namespace eZ\Publish\Core\Search\Legacy\Content\Common\Gateway\CriterionHandler\FieldValue;
10
11
use eZ\Publish\Core\Persistence\Database\DatabaseHandler;
12
use eZ\Publish\API\Repository\Values\Content\Query\Criterion;
13
use eZ\Publish\API\Repository\Values\Content\Query\Criterion\Operator as CriterionOperator;
14
use eZ\Publish\Core\Persistence\Database\SelectQuery;
15
use eZ\Publish\Core\Persistence\TransformationProcessor;
16
use RuntimeException;
17
18
/**
19
 * Content locator gateway implementation using the DoctrineDatabase.
20
 */
21
abstract class Handler
22
{
23
    /**
24
     * DB handler to fetch additional field information.
25
     *
26
     * @var \eZ\Publish\Core\Persistence\Database\DatabaseHandler
27
     * @deprecated Start to use DBAL $connection instead.
28
     */
29
    protected $dbHandler;
30
31
    /**
32
     * Map of criterion operators to the respective function names
33
     * in the DoctrineDatabase DBAL.
34
     *
35
     * @var array
36
     */
37
    protected $comparatorMap = array(
38
        CriterionOperator::EQ => 'eq',
39
        CriterionOperator::GT => 'gt',
40
        CriterionOperator::GTE => 'gte',
41
        CriterionOperator::LT => 'lt',
42
        CriterionOperator::LTE => 'lte',
43
        CriterionOperator::LIKE => 'like',
44
    );
45
46
    /**
47
     * Transformation processor.
48
     *
49
     * @var \eZ\Publish\Core\Persistence\TransformationProcessor
50
     */
51
    protected $transformationProcessor;
52
53
    /**
54
     * Creates a new criterion handler.
55
     *
56
     * @param \eZ\Publish\Core\Persistence\Database\DatabaseHandler $dbHandler
57
     * @param \eZ\Publish\Core\Persistence\TransformationProcessor $transformationProcessor
58
     */
59
    public function __construct(DatabaseHandler $dbHandler, TransformationProcessor $transformationProcessor)
60
    {
61
        $this->dbHandler = $dbHandler;
62
        $this->transformationProcessor = $transformationProcessor;
63
    }
64
65
    /**
66
     * Generates query expression for operator and value of a Field Criterion.
67
     *
68
     * @throws \RuntimeException If operator is not handled.
69
     *
70
     * @param \eZ\Publish\Core\Persistence\Database\SelectQuery $query
71
     * @param \eZ\Publish\API\Repository\Values\Content\Query\Criterion $criterion
72
     * @param string $column
73
     *
74
     * @return \eZ\Publish\Core\Persistence\Database\Expression
75
     */
76
    public function handle(SelectQuery $query, Criterion $criterion, $column)
77
    {
78
        $column = $this->dbHandler->quoteColumn($column);
79
80
        switch ($criterion->operator) {
81
            case Criterion\Operator::IN:
82
                $filter = $query->expr->in(
83
                    $column,
84
                    array_map(array($this, 'lowercase'), $criterion->value)
85
                );
86
                break;
87
88
            case Criterion\Operator::BETWEEN:
89
                $filter = $query->expr->between(
90
                    $column,
91
                    $query->bindValue($this->lowercase($criterion->value[0])),
92
                    $query->bindValue($this->lowercase($criterion->value[1]))
93
                );
94
                break;
95
96
            case Criterion\Operator::EQ:
97
            case Criterion\Operator::GT:
98
            case Criterion\Operator::GTE:
99
            case Criterion\Operator::LT:
100
            case Criterion\Operator::LTE:
101
            case Criterion\Operator::LIKE:
102
                $operatorFunction = $this->comparatorMap[$criterion->operator];
103
                $filter = $query->expr->$operatorFunction(
104
                    $column,
105
                    $query->bindValue($this->lowercase($criterion->value))
106
                );
107
                break;
108
109
            case Criterion\Operator::CONTAINS:
110
                $filter = $query->expr->like(
111
                    $column,
112
                    $query->bindValue(
113
                        '%' . $this->prepareLikeString($criterion->value) . '%'
114
                    )
115
                );
116
                break;
117
118
            default:
119
                throw new RuntimeException("Unknown operator '{$criterion->operator}' for Field criterion handler.");
120
        }
121
122
        return $filter;
123
    }
124
125
    /**
126
     * Returns the given $string prepared for use in SQL LIKE clause.
127
     *
128
     * LIKE clause wildcards '%' and '_' contained in the given $string will be escaped.
129
     *
130
     * @param $string
131
     *
132
     * @return string
133
     */
134
    protected function prepareLikeString($string)
135
    {
136
        return addcslashes($this->lowercase($string), '%_');
137
    }
138
139
    /**
140
     * Downcases a given string using string transformation processor.
141
     *
142
     * @param string $string
143
     *
144
     * @return string
145
     */
146
    protected function lowerCase($string)
147
    {
148
        return $this->transformationProcessor->transformByGroup($string, 'lowercase');
149
    }
150
}
151