| Total Complexity | 80 |
| Total Lines | 431 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like CourseRecycler 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.
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 CourseRecycler, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 32 | final class CourseRecycler |
||
| 33 | { |
||
| 34 | public function __construct( |
||
| 39 | |||
| 40 | /** |
||
| 41 | * $type: 'full_backup' | 'select_items' ; $selected: [type => [id => true]]. |
||
| 42 | */ |
||
| 43 | public function recycle(string $type, array $selected): void |
||
| 86 | // If you keep cleaning course picture: |
||
| 87 | // CourseManager::deleteCoursePicture($this->courseCode); |
||
| 88 | } |
||
| 89 | }); |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Generic recycler for any AbstractResource-based entity. |
||
| 94 | * - If $isFull => deletes *all resources of that type* for the course. |
||
| 95 | * - If partial => deletes only the provided $ids. |
||
| 96 | * Options: |
||
| 97 | * - deleteFiles: physical files are already handled by hardDelete (if repo supports it). |
||
| 98 | * - cascadeHeavy: for heavy-relations types (forums, LPs). hardDelete should traverse. |
||
| 99 | * - autoClean: e.g. remove empty categories after deleting links/forums. |
||
| 100 | * - scormCleanup: if LP SCORM → hook storage service if needed. |
||
| 101 | */ |
||
| 102 | private function recycleGeneric( |
||
| 103 | bool $isFull, |
||
| 104 | string $entityClass, |
||
| 105 | array $idsMap, |
||
| 106 | bool $deleteFiles = false, |
||
| 107 | bool $cascadeHeavy = false, |
||
| 108 | bool $autoClean = false, |
||
| 109 | bool $scormCleanup = false |
||
| 110 | ): void { |
||
| 111 | $repo = $this->em->getRepository($entityClass); |
||
| 112 | $hasHardDelete = method_exists($repo, 'hardDelete'); |
||
| 113 | |||
| 114 | if ($isFull) { |
||
| 115 | $resources = $this->fetchResourcesForCourse($entityClass, null); |
||
| 116 | if ($resources) { |
||
|
|
|||
| 117 | $this->hardDeleteMany($entityClass, $resources); |
||
| 118 | |||
| 119 | // Physical delete fallback for documents if repo lacks hardDelete() |
||
| 120 | if ($deleteFiles && !$hasHardDelete && CDocument::class === $entityClass) { |
||
| 121 | foreach ($resources as $res) { |
||
| 122 | $this->physicallyDeleteDocumentFiles($res); |
||
| 123 | } |
||
| 124 | } |
||
| 125 | } |
||
| 126 | |||
| 127 | if ($autoClean) { |
||
| 128 | $this->autoCleanIfSupported($entityClass); |
||
| 129 | } |
||
| 130 | if ($scormCleanup && CLp::class === $entityClass) { |
||
| 131 | $this->cleanupScormDirsForAllLp(); |
||
| 132 | } |
||
| 133 | |||
| 134 | return; |
||
| 135 | } |
||
| 136 | |||
| 137 | $ids = $this->ids($idsMap); |
||
| 138 | if (!$ids) { |
||
| 139 | if ($autoClean) { |
||
| 140 | $this->autoCleanIfSupported($entityClass); |
||
| 141 | } |
||
| 142 | |||
| 143 | return; |
||
| 144 | } |
||
| 145 | |||
| 146 | $resources = $this->fetchResourcesForCourse($entityClass, $ids); |
||
| 147 | if ($resources) { |
||
| 148 | $this->hardDeleteMany($entityClass, $resources); |
||
| 149 | |||
| 150 | if ($deleteFiles && !$hasHardDelete && CDocument::class === $entityClass) { |
||
| 151 | foreach ($resources as $res) { |
||
| 152 | $this->physicallyDeleteDocumentFiles($res); |
||
| 153 | } |
||
| 154 | } |
||
| 155 | } |
||
| 156 | |||
| 157 | if ($autoClean) { |
||
| 158 | $this->autoCleanIfSupported($entityClass); |
||
| 159 | } |
||
| 160 | if ($scormCleanup && CLp::class === $entityClass) { |
||
| 161 | $this->cleanupScormDirsForLpIds($ids); |
||
| 162 | } |
||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * LP categories: detach LPs and then delete selected/all categories. |
||
| 167 | */ |
||
| 168 | private function recycleLpCategories(bool $isFull, array $idsMap): void |
||
| 169 | { |
||
| 170 | if ($isFull) { |
||
| 171 | // Detach all categories from LPs in course |
||
| 172 | $this->clearLpCategoriesForCourse(); |
||
| 173 | $this->deleteAllOfTypeForCourse(CLpCategory::class); |
||
| 174 | |||
| 175 | return; |
||
| 176 | } |
||
| 177 | |||
| 178 | $ids = $this->ids($idsMap); |
||
| 179 | if (!$ids) { |
||
| 180 | return; |
||
| 181 | } |
||
| 182 | |||
| 183 | // Detach LPs from these categories |
||
| 184 | $this->clearLpCategoriesForIds($ids); |
||
| 185 | $this->deleteSelectedOfTypeForCourse(CLpCategory::class, $ids); |
||
| 186 | } |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Normalizes IDs from [id => true] maps into int/string scalars. |
||
| 190 | */ |
||
| 191 | private function ids(array $map): array |
||
| 192 | { |
||
| 193 | return array_values(array_filter(array_map( |
||
| 194 | static fn ($k) => is_numeric($k) ? (int) $k : (string) $k, |
||
| 195 | array_keys($map) |
||
| 196 | ), static fn ($v) => '' !== $v && null !== $v)); |
||
| 197 | } |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Lightweight Course reference for query builders. |
||
| 201 | */ |
||
| 202 | private function courseRef(): CoreCourse |
||
| 206 | } |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Fetches resources by entity class within course, optionally filtering by resource iid. |
||
| 210 | * If the repository doesn't extend ResourceRepository, falls back to a generic QB. |
||
| 211 | * |
||
| 212 | * @return array<int, AbstractResource> |
||
| 213 | */ |
||
| 214 | private function fetchResourcesForCourse(string $entityClass, ?array $ids = null): array |
||
| 215 | { |
||
| 216 | $repo = $this->em->getRepository($entityClass); |
||
| 217 | |||
| 218 | // Path A: repository exposes ResourceRepository API |
||
| 219 | if (method_exists($repo, 'getResourcesByCourseIgnoreVisibility')) { |
||
| 220 | $qb = $repo->getResourcesByCourseIgnoreVisibility($this->courseRef()); |
||
| 221 | if ($ids && \count($ids) > 0) { |
||
| 222 | // Try iid first; if the entity has no iid, fall back to id |
||
| 223 | $meta = $this->em->getClassMetadata($entityClass); |
||
| 224 | $hasIid = $meta->hasField('iid'); |
||
| 225 | |||
| 226 | if ($hasIid) { |
||
| 227 | $qb->andWhere('resource.iid IN (:ids)'); |
||
| 228 | } else { |
||
| 229 | $qb->andWhere('resource.id IN (:ids)'); |
||
| 230 | } |
||
| 231 | $qb->setParameter('ids', $ids); |
||
| 232 | } |
||
| 233 | |||
| 234 | return $qb->getQuery()->getResult(); |
||
| 235 | } |
||
| 236 | |||
| 237 | // Path B: generic fallback (join to ResourceNode/ResourceLinks and filter by course) |
||
| 238 | $qb = $this->em->createQueryBuilder() |
||
| 239 | ->select('resource') |
||
| 240 | ->from($entityClass, 'resource') |
||
| 241 | ->innerJoin('resource.resourceNode', 'node') |
||
| 242 | ->innerJoin('node.resourceLinks', 'links') |
||
| 243 | ->andWhere('links.course = :course') |
||
| 244 | ->setParameter('course', $this->courseRef()) |
||
| 245 | ; |
||
| 246 | |||
| 247 | if ($ids && \count($ids) > 0) { |
||
| 248 | $meta = $this->em->getClassMetadata($entityClass); |
||
| 249 | $field = $meta->hasField('iid') ? 'resource.iid' : 'resource.id'; |
||
| 250 | $qb->andWhere("$field IN (:ids)")->setParameter('ids', $ids); |
||
| 251 | } |
||
| 252 | |||
| 253 | return $qb->getQuery()->getResult(); |
||
| 254 | } |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Force-unlink associations that can trigger cascade-persist on delete. |
||
| 258 | * We always null GradebookCategory->document before removing the category. |
||
| 259 | */ |
||
| 260 | private function preUnlinkBeforeDelete(array $entities): void |
||
| 261 | { |
||
| 262 | $changed = false; |
||
| 263 | |||
| 264 | foreach ($entities as $e) { |
||
| 265 | if ($e instanceof GradebookCategory |
||
| 266 | && method_exists($e, 'getDocument') |
||
| 267 | && method_exists($e, 'setDocument') |
||
| 268 | ) { |
||
| 269 | if (null !== $e->getDocument()) { |
||
| 270 | // Prevent "new entity found through relationship" on flush |
||
| 271 | $e->setDocument(null); |
||
| 272 | $this->em->persist($e); |
||
| 273 | $changed = true; |
||
| 274 | } |
||
| 275 | } |
||
| 276 | } |
||
| 277 | |||
| 278 | if ($changed) { |
||
| 279 | $this->em->flush(); |
||
| 280 | } |
||
| 281 | } |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Hard-deletes a list of resources. If repository doesn't provide hardDelete(), |
||
| 285 | * falls back to EM->remove() and a final flush (expect proper cascade mappings). |
||
| 286 | */ |
||
| 287 | private function hardDeleteMany(string $entityClass, array $resources): void |
||
| 308 | |||
| 309 | // Optional: clear EM to reduce memory in huge batches |
||
| 310 | // $this->em->clear(); |
||
| 311 | } |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Deletes all resources of a type in the course. |
||
| 315 | */ |
||
| 316 | private function deleteAllOfTypeForCourse(string $entityClass): void |
||
| 317 | { |
||
| 318 | $resources = $this->fetchResourcesForCourse($entityClass, null); |
||
| 319 | if ($resources) { |
||
| 320 | $this->hardDeleteMany($entityClass, $resources); |
||
| 321 | } |
||
| 322 | } |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Deletes selected resources (by iid) of a type in the course. |
||
| 326 | */ |
||
| 327 | private function deleteSelectedOfTypeForCourse(string $entityClass, array $ids): void |
||
| 328 | { |
||
| 329 | if (!$ids) { |
||
| 330 | return; |
||
| 331 | } |
||
| 332 | $resources = $this->fetchResourcesForCourse($entityClass, $ids); |
||
| 333 | if ($resources) { |
||
| 334 | $this->hardDeleteMany($entityClass, $resources); |
||
| 335 | } |
||
| 336 | } |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Optional post-clean for empty categories if repository supports it. |
||
| 340 | */ |
||
| 341 | private function autoCleanIfSupported(string $entityClass): void |
||
| 346 | } |
||
| 347 | } |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Detach categories from ALL LPs in course (repo-level bulk method preferred if available). |
||
| 351 | */ |
||
| 352 | private function clearLpCategoriesForCourse(): void |
||
| 353 | { |
||
| 354 | $lps = $this->fetchResourcesForCourse(CLp::class, null); |
||
| 355 | $changed = false; |
||
| 356 | foreach ($lps as $lp) { |
||
| 357 | if (method_exists($lp, 'getCategory') && method_exists($lp, 'setCategory')) { |
||
| 358 | if ($lp->getCategory()) { |
||
| 359 | $lp->setCategory(null); |
||
| 360 | $this->em->persist($lp); |
||
| 361 | $changed = true; |
||
| 362 | } |
||
| 363 | } |
||
| 364 | } |
||
| 365 | if ($changed) { |
||
| 366 | $this->em->flush(); |
||
| 367 | } |
||
| 368 | } |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Detach categories only for LPs that are linked to given category ids. |
||
| 372 | */ |
||
| 373 | private function clearLpCategoriesForIds(array $catIds): void |
||
| 374 | { |
||
| 375 | $lps = $this->fetchResourcesForCourse(CLp::class, null); |
||
| 376 | $changed = false; |
||
| 377 | foreach ($lps as $lp) { |
||
| 378 | $cat = method_exists($lp, 'getCategory') ? $lp->getCategory() : null; |
||
| 379 | $catId = $cat?->getId(); |
||
| 380 | if (null !== $catId && \in_array($catId, $catIds, true) && method_exists($lp, 'setCategory')) { |
||
| 381 | $lp->setCategory(null); |
||
| 382 | $this->em->persist($lp); |
||
| 383 | $changed = true; |
||
| 384 | } |
||
| 385 | } |
||
| 386 | if ($changed) { |
||
| 387 | $this->em->flush(); |
||
| 388 | } |
||
| 389 | } |
||
| 390 | |||
| 391 | private function unplugCertificateDocsForCourse(): void |
||
| 392 | { |
||
| 393 | // Detach any certificate-type document from gradebook categories of this course |
||
| 394 | // Reason: avoid "A new entity was found through the relationship ... #document" on flush. |
||
| 395 | $qb = $this->em->createQueryBuilder() |
||
| 396 | ->select('c', 'd') |
||
| 397 | ->from(GradebookCategory::class, 'c') |
||
| 398 | ->innerJoin('c.course', 'course') |
||
| 399 | ->leftJoin('c.document', 'd') |
||
| 400 | ->where('course.id = :cid') |
||
| 401 | ->andWhere('d IS NOT NULL') |
||
| 402 | ->andWhere('d.filetype = :ft') |
||
| 403 | ->setParameter('cid', $this->courseId) |
||
| 404 | ->setParameter('ft', 'certificate') |
||
| 405 | ; |
||
| 406 | |||
| 407 | /** @var GradebookCategory[] $cats */ |
||
| 408 | $cats = $qb->getQuery()->getResult(); |
||
| 409 | |||
| 410 | $changed = false; |
||
| 411 | foreach ($cats as $cat) { |
||
| 412 | $doc = $cat->getDocument(); |
||
| 413 | if ($doc instanceof CDocument) { |
||
| 414 | // Prevent transient Document from being cascaded/persisted during delete |
||
| 415 | $cat->setDocument(null); |
||
| 416 | $this->em->persist($cat); |
||
| 417 | $changed = true; |
||
| 418 | } |
||
| 419 | } |
||
| 420 | |||
| 421 | if ($changed) { |
||
| 422 | // Materialize unlink before any deletion happens |
||
| 423 | $this->em->flush(); |
||
| 424 | } |
||
| 425 | } |
||
| 426 | |||
| 427 | /** @param CDocument $doc */ |
||
| 428 | private function physicallyDeleteDocumentFiles(AbstractResource $doc): void |
||
| 429 | { |
||
| 430 | // This generic example traverses node->resourceFiles and removes them from disk. |
||
| 431 | $node = $doc->getResourceNode(); |
||
| 432 | if (!method_exists($node, 'getResourceFiles')) { |
||
| 433 | return; |
||
| 434 | } |
||
| 435 | |||
| 436 | foreach ($node->getResourceFiles() as $rf) { |
||
| 437 | // Example: if you have an absolute path getter or storage key |
||
| 438 | if (method_exists($rf, 'getAbsolutePath')) { |
||
| 439 | $path = (string) $rf->getAbsolutePath(); |
||
| 440 | if ($path && file_exists($path)) { |
||
| 441 | @unlink($path); |
||
| 442 | } |
||
| 443 | } |
||
| 444 | // If you use a storage service, call it here instead of unlink() |
||
| 445 | // $this->storage->delete($rf->getStorageKey()); |
||
| 446 | } |
||
| 447 | } |
||
| 448 | |||
| 449 | /** |
||
| 450 | * SCORM directory cleanup for ALL LPs (hook your storage service here if needed). |
||
| 451 | */ |
||
| 452 | private function cleanupScormDirsForAllLp(): void |
||
| 454 | // If you have a storage/scorm service, invoke it here. |
||
| 455 | // By default, nothing: hardDelete already deletes files linked to ResourceNode. |
||
| 456 | } |
||
| 457 | |||
| 458 | /** |
||
| 459 | * SCORM directory cleanup for selected LPs. |
||
| 460 | */ |
||
| 461 | private function cleanupScormDirsForLpIds(array $lpIds): void |
||
| 462 | { |
||
| 463 | // Same as above, but limited to provided LP ids. |
||
| 464 | } |
||
| 465 | } |
||
| 466 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.