Completed
Push — ezp-31088-refactor-content-mod... ( ab3ba3 )
by
unknown
13:24 queued 33s
created

QueryBuilder::createLoadContentInfoQueryBuilder()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 27
rs 9.488
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
5
 * @license For full copyright and license information view LICENSE file distributed with this source code.
6
 */
7
namespace eZ\Publish\Core\Persistence\Legacy\Content\Gateway\DoctrineDatabase;
8
9
use Doctrine\DBAL\Connection;
10
use Doctrine\DBAL\ParameterType;
11
use Doctrine\DBAL\Query\QueryBuilder as DoctrineQueryBuilder;
12
use eZ\Publish\Core\Persistence\Legacy\Content\Gateway;
13
use function time;
14
15
/**
16
 * @internal For internal use by the Content gateway.
17
 */
18
final class QueryBuilder
19
{
20
    /** @var \Doctrine\DBAL\Connection */
21
    private $connection;
22
23
    public function __construct(Connection $connection)
24
    {
25
        $this->connection = $connection;
26
    }
27
28
    /**
29
     * Create select query to query content name data.
30
     */
31 View Code Duplication
    public function createNamesQuery(): DoctrineQueryBuilder
32
    {
33
        $query = $this->connection->createQueryBuilder();
34
        $query
35
            ->select(
36
                'contentobject_id AS ezcontentobject_name_contentobject_id',
37
                'content_version AS ezcontentobject_name_content_version',
38
                'name AS ezcontentobject_name_name',
39
                'content_translation AS ezcontentobject_name_content_translation'
40
            )
41
            ->from(Gateway::CONTENT_NAME_TABLE);
42
43
        return $query;
44
    }
45
46
    /**
47
     * Create a select query for content relations.
48
     */
49 View Code Duplication
    public function createRelationFindQueryBuilder(): DoctrineQueryBuilder
50
    {
51
        $query = $this->connection->createQueryBuilder();
52
        $query
53
            ->select(
54
                'l.id AS ezcontentobject_link_id',
55
                'l.contentclassattribute_id AS ezcontentobject_link_contentclassattribute_id',
56
                'l.from_contentobject_id AS ezcontentobject_link_from_contentobject_id',
57
                'l.from_contentobject_version AS ezcontentobject_link_from_contentobject_version',
58
                'l.relation_type AS ezcontentobject_link_relation_type',
59
                'l.to_contentobject_id AS ezcontentobject_link_to_contentobject_id'
60
            )
61
            ->from(
62
                Gateway::CONTENT_RELATION_TABLE, 'l'
63
            );
64
65
        return $query;
66
    }
67
68
    /**
69
     * Create an update query for setting Content item Version status.
70
     */
71
    public function getSetVersionStatusQuery(
72
        int $contentId,
73
        int $versionNo,
74
        int $versionStatus
75
    ): DoctrineQueryBuilder {
76
        $query = $this->connection->createQueryBuilder();
77
        $query
78
            ->update(Gateway::CONTENT_VERSION_TABLE)
79
            ->set('status', ':status')
80
            ->set('modified', ':modified')
81
            ->where('contentobject_id = :contentId')
82
            ->andWhere('version = :versionNo')
83
            ->setParameter('status', $versionStatus, ParameterType::INTEGER)
84
            ->setParameter('modified', time(), ParameterType::INTEGER)
85
            ->setParameter('contentId', $contentId, ParameterType::INTEGER)
86
            ->setParameter('versionNo', $versionNo, ParameterType::INTEGER);
87
88
        return $query;
89
    }
90
91
    /**
92
     * Create a select query to load Content Info data.
93
     *
94
     * @see Gateway::loadContentInfo()
95
     * @see Gateway::loadContentInfoList()
96
     * @see Gateway::loadContentInfoByRemoteId()
97
     * @see Gateway::loadContentInfoByLocationId()
98
     */
99
    public function createLoadContentInfoQueryBuilder(
100
        bool $joinMainLocation = true
101
    ): DoctrineQueryBuilder {
102
        $queryBuilder = $this->connection->createQueryBuilder();
103
        $expr = $queryBuilder->expr();
104
105
        $joinCondition = $expr->eq('c.id', 't.contentobject_id');
106
        if ($joinMainLocation) {
107
            // wrap join condition with AND operator and join by a Main Location
108
            $joinCondition = $expr->andX(
109
                $joinCondition,
110
                $expr->eq('t.node_id', 't.main_node_id')
111
            );
112
        }
113
114
        $queryBuilder
115
            ->select('c.*', 't.main_node_id AS ezcontentobject_tree_main_node_id')
116
            ->from(Gateway::CONTENT_ITEM_TABLE, 'c')
117
            ->leftJoin(
118
                'c',
119
                'ezcontentobject_tree',
120
                't',
121
                $joinCondition
0 ignored issues
show
Bug introduced by
It seems like $joinCondition defined by $expr->andX($joinConditi...id', 't.main_node_id')) on line 108 can also be of type object<Doctrine\DBAL\Que...on\CompositeExpression>; however, Doctrine\DBAL\Query\QueryBuilder::leftJoin() does only seem to accept string|null, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
122
            );
123
124
        return $queryBuilder;
125
    }
126
127
    /**
128
     * Get query builder for content version objects, used for version loading w/o fields.
129
     *
130
     * Creates a select query with all necessary joins to fetch a complete
131
     * content object. Does not apply any WHERE conditions, and does not contain
132
     * name data as it will lead to large result set {@see createNamesQuery}.
133
     */
134
    public function createVersionInfoFindQueryBuilder(): DoctrineQueryBuilder
135
    {
136
        $query = $this->connection->createQueryBuilder();
137
        $expr = $query->expr();
138
139
        $query
140
            ->select(
141
                'v.id AS ezcontentobject_version_id',
142
                'v.version AS ezcontentobject_version_version',
143
                'v.modified AS ezcontentobject_version_modified',
144
                'v.creator_id AS ezcontentobject_version_creator_id',
145
                'v.created AS ezcontentobject_version_created',
146
                'v.status AS ezcontentobject_version_status',
147
                'v.contentobject_id AS ezcontentobject_version_contentobject_id',
148
                'v.initial_language_id AS ezcontentobject_version_initial_language_id',
149
                'v.language_mask AS ezcontentobject_version_language_mask',
150
                // Content main location
151
                't.main_node_id AS ezcontentobject_tree_main_node_id',
152
                // Content object
153
                'c.id AS ezcontentobject_id',
154
                'c.contentclass_id AS ezcontentobject_contentclass_id',
155
                'c.section_id AS ezcontentobject_section_id',
156
                'c.owner_id AS ezcontentobject_owner_id',
157
                'c.remote_id AS ezcontentobject_remote_id',
158
                'c.current_version AS ezcontentobject_current_version',
159
                'c.initial_language_id AS ezcontentobject_initial_language_id',
160
                'c.modified AS ezcontentobject_modified',
161
                'c.published AS ezcontentobject_published',
162
                'c.status AS ezcontentobject_status',
163
                'c.name AS ezcontentobject_name',
164
                'c.language_mask AS ezcontentobject_language_mask',
165
                'c.is_hidden AS ezcontentobject_is_hidden'
166
            )
167
            ->from(Gateway::CONTENT_VERSION_TABLE, 'v')
168
            ->innerJoin(
169
                'v',
170
                Gateway::CONTENT_ITEM_TABLE,
171
                'c',
172
                $expr->eq('c.id', 'v.contentobject_id')
173
            )
174
            ->leftJoin(
175
                'v',
176
                'ezcontentobject_tree',
177
                't',
178
                $expr->andX(
179
                    $expr->eq('t.contentobject_id', 'v.contentobject_id'),
180
                    $expr->eq('t.main_node_id', 't.node_id')
181
                )
182
            );
183
184
        return $query;
185
    }
186
}
187