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