Complex classes like DoctrineDatabase often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use DoctrineDatabase, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
33 | final class DoctrineDatabase extends Gateway |
||
34 | { |
||
35 | private const SORT_CLAUSE_TARGET_MAP = [ |
||
36 | 'location_depth' => 'depth', |
||
37 | 'location_priority' => 'priority', |
||
38 | 'location_path' => 'path_string', |
||
39 | 'trashed' => 'trashed', |
||
40 | ]; |
||
41 | |||
42 | /** @var \Doctrine\DBAL\Connection */ |
||
43 | private $connection; |
||
44 | |||
45 | /** @var \eZ\Publish\Core\Persistence\Legacy\Content\Language\MaskGenerator */ |
||
46 | private $languageMaskGenerator; |
||
47 | |||
48 | /** @var \Doctrine\DBAL\Platforms\AbstractPlatform */ |
||
49 | private $dbPlatform; |
||
50 | |||
51 | /** |
||
52 | * @throws \Doctrine\DBAL\DBALException |
||
53 | */ |
||
54 | public function __construct(Connection $connection, MaskGenerator $languageMaskGenerator) |
||
55 | { |
||
56 | $this->connection = $connection; |
||
57 | $this->dbPlatform = $this->connection->getDatabasePlatform(); |
||
58 | $this->languageMaskGenerator = $languageMaskGenerator; |
||
59 | } |
||
60 | |||
61 | public function getBasicNodeData( |
||
62 | int $nodeId, |
||
63 | array $translations = null, |
||
64 | bool $useAlwaysAvailable = true |
||
65 | ): array { |
||
66 | $query = $this->createNodeQueryBuilder(['t.*'], $translations, $useAlwaysAvailable); |
||
67 | $query->andWhere( |
||
68 | $query->expr()->eq('t.node_id', $query->createNamedParameter($nodeId, ParameterType::INTEGER)) |
||
69 | ); |
||
70 | |||
71 | if ($row = $query->execute()->fetch(FetchMode::ASSOCIATIVE)) { |
||
72 | return $row; |
||
73 | } |
||
74 | |||
75 | throw new NotFound('location', $nodeId); |
||
76 | } |
||
77 | |||
78 | public function getNodeDataList(array $locationIds, array $translations = null, bool $useAlwaysAvailable = true): iterable |
||
79 | { |
||
80 | $query = $this->createNodeQueryBuilder(['t.*'], $translations, $useAlwaysAvailable); |
||
81 | $query->andWhere( |
||
82 | $query->expr()->in( |
||
83 | 't.node_id', |
||
84 | $query->createNamedParameter($locationIds, Connection::PARAM_INT_ARRAY) |
||
85 | ) |
||
86 | ); |
||
87 | |||
88 | return $query->execute()->fetchAll(FetchMode::ASSOCIATIVE); |
||
89 | } |
||
90 | |||
91 | public function getBasicNodeDataByRemoteId( |
||
92 | string $remoteId, |
||
93 | array $translations = null, |
||
94 | bool $useAlwaysAvailable = true |
||
95 | ): array { |
||
96 | $query = $this->createNodeQueryBuilder(['t.*'], $translations, $useAlwaysAvailable); |
||
97 | $query->andWhere( |
||
98 | $query->expr()->eq('t.remote_id', $query->createNamedParameter($remoteId, ParameterType::STRING)) |
||
99 | ); |
||
100 | |||
101 | if ($row = $query->execute()->fetch(FetchMode::ASSOCIATIVE)) { |
||
102 | return $row; |
||
103 | } |
||
104 | |||
105 | throw new NotFound('location', $remoteId); |
||
106 | } |
||
107 | |||
108 | public function loadLocationDataByContent(int $contentId, ?int $rootLocationId = null): array |
||
109 | { |
||
110 | $query = $this->connection->createQueryBuilder(); |
||
111 | $query |
||
112 | ->select('*') |
||
113 | ->from(self::CONTENT_TREE_TABLE, 't') |
||
114 | ->where( |
||
115 | $query->expr()->eq( |
||
116 | 't.contentobject_id', |
||
117 | $query->createPositionalParameter($contentId, ParameterType::INTEGER) |
||
118 | ) |
||
119 | ); |
||
120 | |||
121 | if ($rootLocationId !== null) { |
||
122 | $query |
||
123 | ->andWhere( |
||
124 | $this->getSubtreeLimitationExpression($query, $rootLocationId) |
||
125 | ) |
||
126 | ; |
||
127 | } |
||
128 | |||
129 | $statement = $query->execute(); |
||
130 | |||
131 | return $statement->fetchAll(FetchMode::ASSOCIATIVE); |
||
132 | } |
||
133 | |||
134 | public function loadParentLocationsDataForDraftContent(int $contentId): array |
||
135 | { |
||
136 | $query = $this->connection->createQueryBuilder(); |
||
137 | $expr = $query->expr(); |
||
138 | $query |
||
139 | ->select('DISTINCT t.*') |
||
140 | ->from(self::CONTENT_TREE_TABLE, 't') |
||
141 | ->innerJoin( |
||
142 | 't', |
||
143 | 'eznode_assignment', |
||
144 | 'a', |
||
145 | $expr->andX( |
||
146 | $expr->eq( |
||
147 | 't.node_id', |
||
148 | 'a.parent_node' |
||
149 | ), |
||
150 | $expr->eq( |
||
151 | 'a.contentobject_id', |
||
152 | $query->createPositionalParameter($contentId, ParameterType::INTEGER) |
||
153 | ), |
||
154 | $expr->eq( |
||
155 | 'a.op_code', |
||
156 | $query->createPositionalParameter( |
||
157 | self::NODE_ASSIGNMENT_OP_CODE_CREATE, |
||
158 | ParameterType::INTEGER |
||
159 | ) |
||
160 | ) |
||
161 | ) |
||
162 | ) |
||
163 | ->innerJoin( |
||
164 | 'a', |
||
165 | 'ezcontentobject', |
||
166 | 'c', |
||
167 | $expr->andX( |
||
168 | $expr->eq( |
||
169 | 'a.contentobject_id', |
||
170 | 'c.id' |
||
171 | ), |
||
172 | $expr->eq( |
||
173 | 'c.status', |
||
174 | $query->createPositionalParameter( |
||
175 | ContentInfo::STATUS_DRAFT, |
||
176 | ParameterType::INTEGER |
||
177 | ) |
||
178 | ) |
||
179 | ) |
||
180 | ); |
||
181 | |||
182 | $statement = $query->execute(); |
||
183 | |||
184 | return $statement->fetchAll(FetchMode::ASSOCIATIVE); |
||
185 | } |
||
186 | |||
187 | public function getSubtreeContent(int $sourceId, bool $onlyIds = false): array |
||
188 | { |
||
189 | $query = $this->connection->createQueryBuilder(); |
||
190 | $query |
||
191 | ->select($onlyIds ? 'node_id, contentobject_id, depth' : '*') |
||
192 | ->from(self::CONTENT_TREE_TABLE, 't') |
||
193 | ->where($this->getSubtreeLimitationExpression($query, $sourceId)) |
||
194 | ->orderBy('t.depth') |
||
195 | ->addOrderBy('t.node_id'); |
||
196 | $statement = $query->execute(); |
||
197 | |||
198 | $results = $statement->fetchAll($onlyIds ? (FetchMode::COLUMN | PDO::FETCH_GROUP) : FetchMode::ASSOCIATIVE); |
||
199 | // array_map() is used to to map all elements stored as $results[$i][0] to $results[$i] |
||
200 | return $onlyIds ? array_map('reset', $results) : $results; |
||
201 | } |
||
202 | |||
203 | /** |
||
204 | * Return constraint which limits the given $query to the subtree starting at $rootLocationId. |
||
205 | */ |
||
206 | private function getSubtreeLimitationExpression( |
||
207 | QueryBuilder $query, |
||
208 | int $rootLocationId |
||
209 | ): string { |
||
210 | return $query->expr()->like( |
||
211 | 't.path_string', |
||
212 | $query->createPositionalParameter( |
||
213 | '%/' . ((string)$rootLocationId) . '/%', |
||
214 | ParameterType::STRING |
||
215 | ) |
||
216 | ); |
||
217 | } |
||
218 | |||
219 | public function getChildren(int $locationId): array |
||
220 | { |
||
221 | $query = $this->connection->createQueryBuilder(); |
||
222 | $query->select('*')->from( |
||
223 | self::CONTENT_TREE_TABLE |
||
224 | )->where( |
||
225 | $query->expr()->eq( |
||
226 | 'ezcontentobject_tree.parent_node_id', |
||
227 | $query->createPositionalParameter($locationId, ParameterType::INTEGER) |
||
228 | ) |
||
229 | ); |
||
230 | $statement = $query->execute(); |
||
231 | |||
232 | return $statement->fetchAll(FetchMode::ASSOCIATIVE); |
||
233 | } |
||
234 | |||
235 | private function getSubtreeNodesData(string $pathString): array |
||
236 | { |
||
237 | $query = $this->connection->createQueryBuilder(); |
||
238 | $query |
||
239 | ->select( |
||
240 | 'node_id', |
||
241 | 'parent_node_id', |
||
242 | 'path_string', |
||
243 | 'path_identification_string', |
||
244 | 'is_hidden' |
||
245 | ) |
||
246 | ->from(self::CONTENT_TREE_TABLE) |
||
247 | ->where( |
||
248 | $query->expr()->like( |
||
249 | 'path_string', |
||
250 | $query->createPositionalParameter($pathString . '%', ParameterType::STRING) |
||
251 | ) |
||
252 | ); |
||
253 | $statement = $query->execute(); |
||
254 | |||
255 | return $statement->fetchAll(); |
||
256 | } |
||
257 | |||
258 | public function moveSubtreeNodes(array $sourceNodeData, array $destinationNodeData): void |
||
259 | { |
||
260 | $fromPathString = $sourceNodeData['path_string']; |
||
261 | |||
262 | $rows = $this->getSubtreeNodesData($fromPathString); |
||
263 | |||
264 | $oldParentPathString = implode('/', array_slice(explode('/', $fromPathString), 0, -2)) . '/'; |
||
265 | $oldParentPathIdentificationString = implode( |
||
266 | '/', |
||
267 | array_slice(explode('/', $sourceNodeData['path_identification_string']), 0, -1) |
||
268 | ); |
||
269 | |||
270 | $hiddenNodeIds = $this->getHiddenNodeIds($rows); |
||
271 | foreach ($rows as $row) { |
||
272 | // Prefixing ensures correct replacement when old parent is root node |
||
273 | $newPathString = str_replace( |
||
274 | 'prefix' . $oldParentPathString, |
||
275 | $destinationNodeData['path_string'], |
||
276 | 'prefix' . $row['path_string'] |
||
277 | ); |
||
278 | $replace = rtrim($destinationNodeData['path_identification_string'], '/'); |
||
279 | if (empty($oldParentPathIdentificationString)) { |
||
280 | $replace .= '/'; |
||
281 | } |
||
282 | $newPathIdentificationString = str_replace( |
||
283 | 'prefix' . $oldParentPathIdentificationString, |
||
284 | $replace, |
||
285 | 'prefix' . $row['path_identification_string'] |
||
286 | ); |
||
287 | $newParentId = $row['parent_node_id']; |
||
288 | if ($row['path_string'] === $fromPathString) { |
||
289 | $newParentId = (int)implode('', array_slice(explode('/', $newPathString), -3, 1)); |
||
290 | } |
||
291 | |||
292 | $this->moveSingleSubtreeNode( |
||
293 | (int)$row['node_id'], |
||
294 | $sourceNodeData, |
||
295 | $destinationNodeData, |
||
296 | $newPathString, |
||
297 | $newPathIdentificationString, |
||
298 | $newParentId, |
||
299 | $hiddenNodeIds |
||
300 | ); |
||
301 | } |
||
302 | } |
||
303 | |||
304 | private function getHiddenNodeIds(array $rows): array |
||
305 | { |
||
306 | return array_map( |
||
307 | static function (array $row) { |
||
308 | return (int)$row['node_id']; |
||
309 | }, |
||
310 | array_filter( |
||
311 | $rows, |
||
312 | static function (array $row) { |
||
313 | return !empty($row['is_hidden']); |
||
314 | } |
||
315 | ) |
||
316 | ); |
||
317 | } |
||
318 | |||
319 | /** |
||
320 | * @param int[] $hiddenNodeIds |
||
321 | */ |
||
322 | private function isHiddenByParent(string $pathString, array $hiddenNodeIds): bool |
||
323 | { |
||
324 | $parentNodeIds = array_map('intval', explode('/', trim($pathString, '/'))); |
||
325 | array_pop($parentNodeIds); // remove self |
||
326 | foreach ($parentNodeIds as $parentNodeId) { |
||
327 | if (in_array($parentNodeId, $hiddenNodeIds, true)) { |
||
328 | return true; |
||
329 | } |
||
330 | } |
||
331 | |||
332 | return false; |
||
333 | } |
||
334 | |||
335 | /** |
||
336 | * @param array $sourceNodeData |
||
337 | * @param array $destinationNodeData |
||
338 | * @param int[] $hiddenNodeIds |
||
339 | */ |
||
340 | private function moveSingleSubtreeNode( |
||
341 | int $nodeId, |
||
342 | array $sourceNodeData, |
||
343 | array $destinationNodeData, |
||
344 | string $newPathString, |
||
345 | string $newPathIdentificationString, |
||
346 | int $newParentId, |
||
347 | array $hiddenNodeIds |
||
348 | ): void { |
||
349 | $query = $this->connection->createQueryBuilder(); |
||
350 | $query |
||
351 | ->update(self::CONTENT_TREE_TABLE) |
||
352 | ->set( |
||
353 | 'path_string', |
||
354 | $query->createPositionalParameter($newPathString, ParameterType::STRING) |
||
355 | ) |
||
356 | ->set( |
||
357 | 'path_identification_string', |
||
358 | $query->createPositionalParameter( |
||
359 | $newPathIdentificationString, |
||
360 | ParameterType::STRING |
||
361 | ) |
||
362 | ) |
||
363 | ->set( |
||
364 | 'depth', |
||
365 | $query->createPositionalParameter( |
||
366 | substr_count($newPathString, '/') - 2, |
||
367 | ParameterType::INTEGER |
||
368 | ) |
||
369 | ) |
||
370 | ->set( |
||
371 | 'parent_node_id', |
||
372 | $query->createPositionalParameter($newParentId, ParameterType::INTEGER) |
||
373 | ); |
||
374 | |||
375 | if ($destinationNodeData['is_hidden'] || $destinationNodeData['is_invisible']) { |
||
376 | // CASE 1: Mark whole tree as invisible if destination is invisible and/or hidden |
||
377 | $query->set( |
||
378 | 'is_invisible', |
||
379 | $query->createPositionalParameter(1, ParameterType::INTEGER) |
||
380 | ); |
||
381 | } elseif (!$sourceNodeData['is_hidden'] && $sourceNodeData['is_invisible']) { |
||
382 | // CASE 2: source is only invisible, we will need to re-calculate whole moved tree visibility |
||
383 | $query->set( |
||
384 | 'is_invisible', |
||
385 | $query->createPositionalParameter( |
||
386 | $this->isHiddenByParent($newPathString, $hiddenNodeIds) ? 1 : 0, |
||
387 | ParameterType::INTEGER |
||
388 | ) |
||
389 | ); |
||
390 | } |
||
391 | |||
392 | $query->where( |
||
393 | $query->expr()->eq( |
||
394 | 'node_id', |
||
395 | $query->createPositionalParameter($nodeId, ParameterType::INTEGER) |
||
396 | ) |
||
397 | ); |
||
398 | $query->execute(); |
||
399 | } |
||
400 | |||
401 | public function updateSubtreeModificationTime(string $pathString, ?int $timestamp = null): void |
||
402 | { |
||
403 | $nodes = array_filter(explode('/', $pathString)); |
||
404 | $query = $this->connection->createQueryBuilder(); |
||
405 | $query |
||
406 | ->update(self::CONTENT_TREE_TABLE) |
||
407 | ->set( |
||
408 | 'modified_subnode', |
||
409 | $query->createPositionalParameter( |
||
410 | $timestamp ?: time(), ParameterType::INTEGER |
||
411 | ) |
||
412 | ) |
||
413 | ->where( |
||
414 | $query->expr()->in( |
||
415 | 'node_id', |
||
416 | $nodes |
||
417 | ) |
||
418 | ); |
||
419 | $query->execute(); |
||
420 | } |
||
421 | |||
422 | public function hideSubtree(string $pathString): void |
||
427 | |||
428 | public function setNodeWithChildrenInvisible(string $pathString): void |
||
429 | { |
||
430 | $query = $this->connection->createQueryBuilder(); |
||
431 | $query |
||
432 | ->update(self::CONTENT_TREE_TABLE) |
||
433 | ->set( |
||
434 | 'is_invisible', |
||
435 | $query->createPositionalParameter(1, ParameterType::INTEGER) |
||
436 | ) |
||
437 | ->set( |
||
438 | 'modified_subnode', |
||
439 | $query->createPositionalParameter(time(), ParameterType::INTEGER) |
||
440 | ) |
||
441 | ->where( |
||
442 | $query->expr()->like( |
||
450 | |||
451 | public function setNodeHidden(string $pathString): void |
||
455 | |||
456 | private function setNodeHiddenStatus(string $pathString, bool $isHidden): void |
||
474 | |||
475 | public function unHideSubtree(string $pathString): void |
||
480 | |||
481 | public function setNodeWithChildrenVisible(string $pathString): void |
||
532 | |||
533 | private function isAnyNodeInPathExplicitlyHidden(string $pathString): bool |
||
553 | |||
554 | /** |
||
555 | * @return array list of path strings |
||
556 | */ |
||
557 | private function loadHiddenSubtreesByPath(string $pathString): array |
||
575 | |||
576 | private function buildHiddenSubtreeQuery(string $selectExpr): QueryBuilder |
||
599 | |||
600 | public function setNodeUnhidden(string $pathString): void |
||
604 | |||
605 | public function swap(int $locationId1, int $locationId2): bool |
||
682 | |||
683 | public function create(CreateStruct $createStruct, array $parentNode): Location |
||
711 | |||
712 | public function createNodeAssignment( |
||
776 | |||
777 | public function deleteNodeAssignment(int $contentId, ?int $versionNo = null): void |
||
798 | |||
799 | public function updateNodeAssignment( |
||
836 | |||
837 | public function createLocationsFromNodeAssignments(int $contentId, int $versionNo): void |
||
903 | |||
904 | public function updateLocationsContentVersionNo(int $contentId, int $versionNo): void |
||
920 | |||
921 | /** |
||
922 | * Search for the main nodeId of $contentId. |
||
923 | */ |
||
924 | private function getMainNodeId(int $contentId): ?int |
||
948 | |||
949 | /** |
||
950 | * Updates an existing location. |
||
951 | * |
||
952 | * Will not throw anything if location id is invalid or no entries are affected. |
||
953 | * |
||
954 | * @param \eZ\Publish\SPI\Persistence\Content\Location\UpdateStruct $location |
||
955 | * @param int $locationId |
||
956 | */ |
||
957 | public function update(UpdateStruct $location, $locationId): void |
||
987 | |||
988 | public function updatePathIdentificationString($locationId, $parentLocationId, $text): void |
||
1010 | |||
1011 | /** |
||
1012 | * Deletes ezcontentobject_tree row for given $locationId (node_id). |
||
1013 | * |
||
1014 | * @param mixed $locationId |
||
1015 | */ |
||
1016 | public function removeLocation($locationId): void |
||
1029 | |||
1030 | /** |
||
1031 | * Return data of the next in line node to be set as a new main node. |
||
1032 | * |
||
1033 | * This returns lowest node id for content identified by $contentId, and not of |
||
1034 | * the node identified by given $locationId (current main node). |
||
1035 | * Assumes that content has more than one location. |
||
1036 | * |
||
1037 | * @param mixed $contentId |
||
1038 | * @param mixed $locationId |
||
1039 | * |
||
1040 | * @return array |
||
1041 | */ |
||
1042 | public function getFallbackMainNodeData($contentId, $locationId): array |
||
1078 | |||
1079 | public function trashLocation(int $locationId): void |
||
1097 | |||
1098 | public function untrashLocation(int $locationId, ?int $newParentId = null): Location |
||
1124 | |||
1125 | private function setContentStatus(int $contentId, int $status): void |
||
1141 | |||
1142 | public function loadTrashByLocation(int $locationId): array |
||
1162 | |||
1163 | public function listTrashed(int $offset, ?int $limit, array $sort = null): array |
||
1194 | |||
1195 | public function countTrashed(): int |
||
1203 | |||
1204 | /** |
||
1205 | * Removes every entries in the trash. |
||
1206 | * Will NOT remove associated content objects nor attributes. |
||
1207 | * |
||
1208 | * Basically truncates ezcontentobject_trash table. |
||
1209 | */ |
||
1210 | public function cleanupTrash(): void |
||
1216 | |||
1217 | public function removeElementFromTrash(int $id): void |
||
1230 | |||
1231 | public function setSectionForSubtree(string $pathString, int $sectionId): bool |
||
1270 | |||
1271 | public function countLocationsByContentId(int $contentId): int |
||
1289 | |||
1290 | public function changeMainLocation( |
||
1320 | |||
1321 | public function countAllLocations(): int |
||
1331 | |||
1332 | public function loadAllLocationsData(int $offset, int $limit): array |
||
1364 | |||
1365 | /** |
||
1366 | * Create QueryBuilder for selecting Location (node) data. |
||
1367 | * |
||
1368 | * @param array $columns column or expression list |
||
1369 | * @param array|null $translations Filters on language mask of content if provided. |
||
1370 | * @param bool $useAlwaysAvailable Respect always available flag on content when filtering on $translations. |
||
1371 | * |
||
1372 | * @return \Doctrine\DBAL\Query\QueryBuilder |
||
1373 | */ |
||
1374 | private function createNodeQueryBuilder( |
||
1391 | |||
1392 | private function appendContentItemTranslationsConstraint( |
||
1427 | |||
1428 | /** |
||
1429 | * Mark eznode_assignment entry, identified by Content ID and Version ID, as main for the given |
||
1430 | * parent Location ID. |
||
1431 | * |
||
1432 | * **NOTE**: The method erases is_main from the other entries related to Content and Version IDs |
||
1433 | */ |
||
1434 | private function setIsMainForContentVersionParentNodeAssignment( |
||
1455 | |||
1456 | /** |
||
1457 | * @param array $parentNode raw Location data |
||
1458 | */ |
||
1459 | private function insertLocationIntoContentTree( |
||
1522 | } |
||
1523 |