Completed
Push — EZP-31644 ( 2e0a1e...93bb44 )
by
unknown
19:12
created

Field::handle()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 56

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 3
nop 4
dl 0
loc 56
rs 8.9599
c 0
b 0
f 0

How to fix   Long Method   

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 field criterion 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;
10
11
use eZ\Publish\Core\Search\Legacy\Content\Common\Gateway\CriteriaConverter;
12
use eZ\Publish\API\Repository\Exceptions\NotImplementedException;
13
use eZ\Publish\Core\Search\Legacy\Content\Common\Gateway\CriterionHandler\FieldValue\Converter as FieldValueConverter;
14
use eZ\Publish\Core\Persistence\Database\DatabaseHandler;
15
use eZ\Publish\API\Repository\Values\Content\Query\Criterion;
16
use eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\ConverterRegistry as Registry;
17
use eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter;
18
use eZ\Publish\Core\Base\Exceptions\InvalidArgumentException;
19
use eZ\Publish\Core\Persistence\TransformationProcessor;
20
use eZ\Publish\Core\Persistence\Database\SelectQuery;
21
use eZ\Publish\SPI\Persistence\Content\Type\Handler as ContentTypeHandler;
22
use eZ\Publish\SPI\Persistence\Content\Language\Handler as LanguageHandler;
23
24
/**
25
 * Field criterion handler.
26
 */
27
class Field extends FieldBase
28
{
29
    /**
30
     * Field converter registry.
31
     *
32
     * @var \eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\ConverterRegistry
33
     */
34
    protected $fieldConverterRegistry;
35
36
    /**
37
     * Field value converter.
38
     *
39
     * @var \eZ\Publish\Core\Search\Legacy\Content\Common\Gateway\CriterionHandler\FieldValue\Converter
40
     */
41
    protected $fieldValueConverter;
42
43
    /**
44
     * Transformation processor.
45
     *
46
     * @var \eZ\Publish\Core\Persistence\TransformationProcessor
47
     */
48
    protected $transformationProcessor;
49
50
    /**
51
     * Construct from handler handler.
52
     *
53
     * @param \eZ\Publish\Core\Persistence\Database\DatabaseHandler $dbHandler
54
     * @param \eZ\Publish\SPI\Persistence\Content\Type\Handler $contentTypeHandler
55
     * @param \eZ\Publish\SPI\Persistence\Content\Language\Handler $languageHandler
56
     * @param \eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\ConverterRegistry $fieldConverterRegistry
57
     * @param \eZ\Publish\Core\Search\Legacy\Content\Common\Gateway\CriterionHandler\FieldValue\Converter $fieldValueConverter
58
     * @param \eZ\Publish\Core\Persistence\TransformationProcessor $transformationProcessor
59
     */
60
    public function __construct(
61
        DatabaseHandler $dbHandler,
62
        ContentTypeHandler $contentTypeHandler,
63
        LanguageHandler $languageHandler,
64
        Registry $fieldConverterRegistry,
65
        FieldValueConverter $fieldValueConverter,
66
        TransformationProcessor $transformationProcessor
67
    ) {
68
        parent::__construct($dbHandler, $contentTypeHandler, $languageHandler);
69
70
        $this->fieldConverterRegistry = $fieldConverterRegistry;
71
        $this->fieldValueConverter = $fieldValueConverter;
72
        $this->transformationProcessor = $transformationProcessor;
73
    }
74
75
    /**
76
     * Check if this criterion handler accepts to handle the given criterion.
77
     *
78
     * @param \eZ\Publish\API\Repository\Values\Content\Query\Criterion $criterion
79
     *
80
     * @return bool
81
     */
82
    public function accept(Criterion $criterion)
83
    {
84
        return $criterion instanceof Criterion\Field;
85
    }
86
87
    /**
88
     * Returns relevant field information for the specified field.
89
     *
90
     * The returned information is returned as an array of the attribute
91
     * identifier and the sort column, which should be used.
92
     *
93
     * @throws \eZ\Publish\Core\Base\Exceptions\InvalidArgumentException If no searchable fields are found for the given $fieldIdentifier.
94
     * @throws \RuntimeException if no converter is found
95
     *
96
     * @param string $fieldIdentifier
97
     *
98
     * @return array
99
     */
100
    protected function getFieldsInformation($fieldIdentifier)
101
    {
102
        $fieldMapArray = [];
103
        $fieldMap = $this->contentTypeHandler->getSearchableFieldMap();
104
105
        foreach ($fieldMap as $contentTypeIdentifier => $fieldIdentifierMap) {
106
            // First check if field exists in the current ContentType, there is nothing to do if it doesn't
107
            if (!isset($fieldIdentifierMap[$fieldIdentifier])) {
108
                continue;
109
            }
110
111
            $fieldTypeIdentifier = $fieldIdentifierMap[$fieldIdentifier]['field_type_identifier'];
112
            $fieldMapArray[$fieldTypeIdentifier]['ids'][] = $fieldIdentifierMap[$fieldIdentifier]['field_definition_id'];
113
            if (!isset($fieldMapArray[$fieldTypeIdentifier]['column'])) {
114
                $fieldMapArray[$fieldTypeIdentifier]['column'] = $this->fieldConverterRegistry->getConverter($fieldTypeIdentifier)->getIndexColumn();
115
            }
116
        }
117
118
        if (empty($fieldMapArray)) {
119
            throw new InvalidArgumentException(
120
                '$criterion->target',
121
                "No searchable fields found for the given criterion target '{$fieldIdentifier}'."
122
            );
123
        }
124
125
        return $fieldMapArray;
126
    }
127
128
    /**
129
     * Generate query expression for a Criterion this handler accepts.
130
     *
131
     * accept() must be called before calling this method.
132
     *
133
     * @throws \eZ\Publish\API\Repository\Exceptions\NotImplementedException If no searchable fields are found for the given criterion target.
134
     *
135
     * @param \eZ\Publish\Core\Search\Legacy\Content\Common\Gateway\CriteriaConverter $converter
136
     * @param \eZ\Publish\Core\Persistence\Database\SelectQuery $query
137
     * @param \eZ\Publish\API\Repository\Values\Content\Query\Criterion $criterion
138
     * @param array $languageSettings
139
     *
140
     * @return \eZ\Publish\Core\Persistence\Database\Expression
141
     */
142
    public function handle(
143
        CriteriaConverter $converter,
144
        SelectQuery $query,
145
        Criterion $criterion,
146
        array $languageSettings
147
    ) {
148
        $fieldsInformation = $this->getFieldsInformation($criterion->target);
149
150
        $subSelect = $query->subSelect();
151
        $subSelect->select(
152
            $this->dbHandler->quoteColumn('contentobject_id')
153
        )->from(
154
            $this->dbHandler->quoteTable('ezcontentobject_attribute')
155
        );
156
157
        $whereExpressions = [];
158
        foreach ($fieldsInformation as $fieldTypeIdentifier => $fieldsInfo) {
159
            if ($fieldsInfo['column'] === false) {
160
                throw new NotImplementedException(
161
                    "A field of type '{$fieldTypeIdentifier}' is not searchable in the legacy search engine."
162
                );
163
            }
164
165
            $filter = $this->fieldValueConverter->convertCriteria(
166
                $fieldTypeIdentifier,
167
                $subSelect,
168
                $criterion,
169
                $fieldsInfo['column']
170
            );
171
172
            $whereExpressions[] = $subSelect->expr->lAnd(
173
                $subSelect->expr->in(
174
                    $this->dbHandler->quoteColumn('contentclassattribute_id'),
175
                    $fieldsInfo['ids']
176
                ),
177
                $filter
178
            );
179
        }
180
181
        $subSelect->where(
182
            $subSelect->expr->lAnd(
183
                $subSelect->expr->eq(
184
                    $this->dbHandler->quoteColumn('version', 'ezcontentobject_attribute'),
185
                    $this->dbHandler->quoteColumn('current_version', 'ezcontentobject')
186
                ),
187
                // Join conditions with a logical OR if several conditions exist
188
                count($whereExpressions) > 1 ? $subSelect->expr->lOr($whereExpressions) : $whereExpressions[0],
189
                $this->getFieldCondition($subSelect, $languageSettings)
190
            )
191
        );
192
193
        return $query->expr->in(
194
            $this->dbHandler->quoteColumn('id', 'ezcontentobject'),
195
            $subSelect
196
        );
197
    }
198
}
199