Completed
Push — ezp-30936-edit-content-new-req... ( 953d44...df9d75 )
by
unknown
19:03
created

FieldBase::getInExpressionWithFieldConditions()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 2
nop 5
dl 0
loc 34
rs 9.376
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * This file is part of the eZ Publish Kernel package.
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\API\Repository\Exceptions\NotImplementedException;
12
use eZ\Publish\Core\Search\Legacy\Content\Common\Gateway\CriterionHandler;
13
use eZ\Publish\Core\Persistence\Database\DatabaseHandler;
14
use eZ\Publish\SPI\Persistence\Content\Type\Handler as ContentTypeHandler;
15
use eZ\Publish\SPI\Persistence\Content\Language\Handler as LanguageHandler;
16
use eZ\Publish\Core\Persistence\Database\SelectQuery;
17
use PDO;
18
19
/**
20
 * Base criterion handler for field criteria.
21
 */
22
abstract class FieldBase extends CriterionHandler
23
{
24
    /**
25
     * Content Type handler.
26
     *
27
     * @var \eZ\Publish\SPI\Persistence\Content\Type\Handler
28
     */
29
    protected $contentTypeHandler;
30
31
    /**
32
     * Language handler.
33
     *
34
     * @var \eZ\Publish\SPI\Persistence\Content\Language\Handler
35
     */
36
    protected $languageHandler;
37
38
    /**
39
     * Construct from handler handler.
40
     *
41
     * @param \eZ\Publish\Core\Persistence\Database\DatabaseHandler $dbHandler
42
     * @param \eZ\Publish\SPI\Persistence\Content\Type\Handler $contentTypeHandler
43
     * @param \eZ\Publish\SPI\Persistence\Content\Language\Handler $languageHandler
44
     */
45
    public function __construct(
46
        DatabaseHandler $dbHandler,
47
        ContentTypeHandler $contentTypeHandler,
48
        LanguageHandler $languageHandler
49
    ) {
50
        parent::__construct($dbHandler);
51
52
        $this->contentTypeHandler = $contentTypeHandler;
53
        $this->languageHandler = $languageHandler;
54
    }
55
56
    /**
57
     * Returns a field language join condition for the given $languageSettings.
58
     *
59
     * @param \eZ\Publish\Core\Persistence\Database\SelectQuery $query
60
     * @param array $languageSettings
61
     *
62
     * @return string
63
     */
64
    protected function getFieldCondition(SelectQuery $query, array $languageSettings)
65
    {
66
        // 1. Use main language(s) by default
67 View Code Duplication
        if (empty($languageSettings['languages'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
68
            return $query->expr->gt(
69
                $query->expr->bitAnd(
70
                    $this->dbHandler->quoteColumn('initial_language_id', 'ezcontentobject'),
71
                    $this->dbHandler->quoteColumn('language_id', 'ezcontentobject_attribute')
72
                ),
73
                $query->bindValue(0, null, PDO::PARAM_INT)
74
            );
75
        }
76
77
        // 2. Otherwise use prioritized languages
78
        $leftSide = $query->expr->bitAnd(
79
            $query->expr->sub(
80
                $this->dbHandler->quoteColumn('language_mask', 'ezcontentobject'),
81
                $query->expr->bitAnd(
82
                    $this->dbHandler->quoteColumn('language_mask', 'ezcontentobject'),
83
                    $this->dbHandler->quoteColumn('language_id', 'ezcontentobject_attribute')
84
                )
85
            ),
86
            $query->bindValue(1, null, PDO::PARAM_INT)
87
        );
88
        $rightSide = $query->expr->bitAnd(
89
            $this->dbHandler->quoteColumn('language_id', 'ezcontentobject_attribute'),
90
            $query->bindValue(1, null, PDO::PARAM_INT)
91
        );
92
93
        for ($index = count($languageSettings['languages']) - 1, $multiplier = 2; $index >= 0; $index--, $multiplier *= 2) {
94
            $languageId = $this->languageHandler
95
                ->loadByLanguageCode($languageSettings['languages'][$index])->id;
96
97
            $addToLeftSide = $query->expr->bitAnd(
98
                $query->expr->sub(
99
                    $this->dbHandler->quoteColumn('language_mask', 'ezcontentobject'),
100
                    $query->expr->bitAnd(
101
                        $this->dbHandler->quoteColumn('language_mask', 'ezcontentobject'),
102
                        $this->dbHandler->quoteColumn('language_id', 'ezcontentobject_attribute')
103
                    )
104
                ),
105
                $languageId
106
            );
107
            $addToRightSide = $query->expr->bitAnd(
108
                $this->dbHandler->quoteColumn('language_id', 'ezcontentobject_attribute'),
109
                $languageId
110
            );
111
112 View Code Duplication
            if ($multiplier > $languageId) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
113
                $factor = $multiplier / $languageId;
114
                for ($shift = 0; $factor > 1; $factor = $factor / 2, $shift++);
115
                $factorTerm = ' << ' . $shift;
116
                $addToLeftSide .= $factorTerm;
117
                $addToRightSide .= $factorTerm;
118
            } elseif ($multiplier < $languageId) {
119
                $factor = $languageId / $multiplier;
120
                for ($shift = 0; $factor > 1; $factor = $factor / 2, $shift++);
121
                $factorTerm = ' >> ' . $shift;
122
                $addToLeftSide .= $factorTerm;
123
                $addToRightSide .= $factorTerm;
124
            }
125
126
            $leftSide = $query->expr->add($leftSide, "($addToLeftSide)");
127
            $rightSide = $query->expr->add($rightSide, "($addToRightSide)");
128
        }
129
130
        return $query->expr->lAnd(
131
            $query->expr->gt(
132
                $query->expr->bitAnd(
133
                    $this->dbHandler->quoteColumn('language_mask', 'ezcontentobject'),
134
                    $this->dbHandler->quoteColumn('language_id', 'ezcontentobject_attribute')
135
                ),
136
                $query->bindValue(0, null, PDO::PARAM_INT)
137
            ),
138
            $query->expr->lt($leftSide, $rightSide)
139
        );
140
    }
141
142
    /**
143
     * @param \eZ\Publish\Core\Persistence\Database\SelectQuery $query
144
     * @param \eZ\Publish\Core\Persistence\Database\SelectQuery $subSelect
145
     * @param array $languageSettings
146
     * @param array $fieldWhereExpressions
147
     * @param array $fieldsInformation
148
     *
149
     * @return string
150
     *
151
     * @throws \eZ\Publish\API\Repository\Exceptions\NotImplementedException
152
     */
153
    protected function getInExpressionWithFieldConditions(
154
        SelectQuery $query,
155
        SelectQuery $subSelect,
156
        array $languageSettings,
157
        array $fieldWhereExpressions,
158
        array $fieldsInformation
159
    ): string {
160
        if (empty($fieldWhereExpressions)) {
161
            throw new NotImplementedException(
162
                sprintf(
163
                    'Following fieldtypes are not searchable in the legacy search engine: %s',
164
                    implode(', ', array_keys($fieldsInformation))
165
                )
166
            );
167
        }
168
169
        $subSelect->where(
170
            $subSelect->expr->lAnd(
171
                $subSelect->expr->eq(
172
                    $this->dbHandler->quoteColumn('version', 'ezcontentobject_attribute'),
173
                    $this->dbHandler->quoteColumn('current_version', 'ezcontentobject')
174
                ),
175
                count($fieldWhereExpressions) > 1 ? $subSelect->expr->lOr(
176
                    $fieldWhereExpressions
177
                ) : $fieldWhereExpressions[0],
178
                $this->getFieldCondition($subSelect, $languageSettings)
179
            )
180
        );
181
182
        return $query->expr->in(
183
            $this->dbHandler->quoteColumn('id', 'ezcontentobject'),
184
            $subSelect
185
        );
186
    }
187
}
188