From 4c188ad125afb1fddacb703bfa5703bafcbf6073 Mon Sep 17 00:00:00 2001 From: Jumping-Beaver <> Date: Tue, 18 Nov 2025 09:57:10 +0100 Subject: [PATCH] Only log and save changes in the cleanup --- src/Cleanup/CleanupStrategyInterface.php | 2 +- src/Cleanup/DeleteStrategy.php | 3 +- src/Cleanup/UnpublishStrategy.php | 11 ++++-- src/Processing/ImportProcessingService.php | 45 ++++++++++++---------- 4 files changed, 34 insertions(+), 27 deletions(-) diff --git a/src/Cleanup/CleanupStrategyInterface.php b/src/Cleanup/CleanupStrategyInterface.php index 086a2d22..fc813660 100644 --- a/src/Cleanup/CleanupStrategyInterface.php +++ b/src/Cleanup/CleanupStrategyInterface.php @@ -19,5 +19,5 @@ interface CleanupStrategyInterface /** * Apply cleanup on given element */ - public function doCleanup(ElementInterface $element): void; + public function doCleanup(ElementInterface $element): bool; } diff --git a/src/Cleanup/DeleteStrategy.php b/src/Cleanup/DeleteStrategy.php index cc884e11..38e14e22 100644 --- a/src/Cleanup/DeleteStrategy.php +++ b/src/Cleanup/DeleteStrategy.php @@ -16,8 +16,9 @@ class DeleteStrategy implements CleanupStrategyInterface { - public function doCleanup(ElementInterface $element): void + public function doCleanup(ElementInterface $element): bool { $element->delete(); + return true; } } diff --git a/src/Cleanup/UnpublishStrategy.php b/src/Cleanup/UnpublishStrategy.php index bda75054..97c93674 100644 --- a/src/Cleanup/UnpublishStrategy.php +++ b/src/Cleanup/UnpublishStrategy.php @@ -16,11 +16,14 @@ class UnpublishStrategy implements CleanupStrategyInterface { - public function doCleanup(ElementInterface $element): void + public function doCleanup(ElementInterface $element): bool { - if (method_exists($element, 'setPublished')) { - $element->setPublished(false); - $element->save(); + if (!method_exists($element, 'setPublished') || !method_exists($element, 'isPublished') || + !$element->isPublished()) { + return false; } + $element->setPublished(false); + $element->save(); + return true; } } diff --git a/src/Processing/ImportProcessingService.php b/src/Processing/ImportProcessingService.php index d1319e07..67856381 100644 --- a/src/Processing/ImportProcessingService.php +++ b/src/Processing/ImportProcessingService.php @@ -349,28 +349,31 @@ public function processElementTransformations( protected function cleanupElement(string $configName, string $identifier, Resolver $resolver, array $cleanupConfig) { - if ($cleanupConfig['doCleanup'] ?? false) { - $element = null; - - try { - $element = $resolver->loadElementByIdentifier($identifier); - if ($element) { - $cleanupStrategy = $this->cleanupStrategyFactory->loadCleanupStrategy($cleanupConfig['strategy']); - $cleanupStrategy->doCleanup($element); - - $message = "Element {$identifier} cleaned up ({$cleanupConfig['strategy']}) successfully."; - $this->logInfo($configName, $message, [ - 'component' => PimcoreDataImporterBundle::LOGGER_COMPONENT_PREFIX . $configName, - 'relatedObject' => $element - ]); - } - } catch (\Exception $e) { - $message = 'Error cleaning up element: '; - $this->logError($configName, $message . $e->getMessage(), [ - 'component' => PimcoreDataImporterBundle::LOGGER_COMPONENT_PREFIX . $configName, - 'relatedObject' => $element, - ]); + if (!($cleanupConfig['doCleanup'] ?? false)) { + return; + } + try { + $element = $resolver->loadElementByIdentifier($identifier); + if ($element === null) { + return; } + $cleanupStrategy = $this->cleanupStrategyFactory->loadCleanupStrategy($cleanupConfig['strategy']); + if ($cleanupStrategy->doCleanup($element) === false) { + return; + } + $message = "Element {$identifier} cleaned up ({$cleanupConfig['strategy']}) successfully."; + $this->logger->info($message); + $this->applicationLogger->info($message, [ + 'component' => PimcoreDataImporterBundle::LOGGER_COMPONENT_PREFIX . $configName, + 'relatedObject' => $element + ]); + } catch (\Exception $e) { + $message = 'Error cleaning up element: '; + $this->logger->error($message . $e); + $this->applicationLogger->error($message . $e->getMessage(), [ + 'component' => PimcoreDataImporterBundle::LOGGER_COMPONENT_PREFIX . $configName, + 'relatedObject' => $element, + ]); } }