Skip to content

Commit bfa20c0

Browse files
author
Andreas Becker
committed
Only log and save changes in the cleanup
1 parent eed49bb commit bfa20c0

4 files changed

Lines changed: 35 additions & 27 deletions

File tree

src/Cleanup/CleanupStrategyInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@ interface CleanupStrategyInterface
1919
/**
2020
* Apply cleanup on given element
2121
*/
22-
public function doCleanup(ElementInterface $element): void;
22+
public function doCleanup(ElementInterface $element): bool;
2323
}

src/Cleanup/DeleteStrategy.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@
1616

1717
class DeleteStrategy implements CleanupStrategyInterface
1818
{
19-
public function doCleanup(ElementInterface $element): void
19+
public function doCleanup(ElementInterface $element): bool
2020
{
2121
$element->delete();
22+
return true;
2223
}
2324
}

src/Cleanup/UnpublishStrategy.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,15 @@
1616

1717
class UnpublishStrategy implements CleanupStrategyInterface
1818
{
19-
public function doCleanup(ElementInterface $element): void
19+
public function doCleanup(ElementInterface $element): bool
2020
{
21-
if (method_exists($element, 'setPublished')) {
22-
$element->setPublished(false);
23-
$element->save();
21+
if (!method_exists($element, 'setPublished') || !method_exists($element, 'isPublished') ||
22+
!$element->isPublished())
23+
{
24+
return false;
2425
}
26+
$element->setPublished(false);
27+
$element->save();
28+
return true;
2529
}
2630
}

src/Processing/ImportProcessingService.php

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -349,28 +349,31 @@ public function processElementTransformations(
349349

350350
protected function cleanupElement(string $configName, string $identifier, Resolver $resolver, array $cleanupConfig)
351351
{
352-
if ($cleanupConfig['doCleanup'] ?? false) {
353-
$element = null;
354-
355-
try {
356-
$element = $resolver->loadElementByIdentifier($identifier);
357-
if ($element) {
358-
$cleanupStrategy = $this->cleanupStrategyFactory->loadCleanupStrategy($cleanupConfig['strategy']);
359-
$cleanupStrategy->doCleanup($element);
360-
361-
$message = "Element {$identifier} cleaned up ({$cleanupConfig['strategy']}) successfully.";
362-
$this->logInfo($configName, $message, [
363-
'component' => PimcoreDataImporterBundle::LOGGER_COMPONENT_PREFIX . $configName,
364-
'relatedObject' => $element
365-
]);
366-
}
367-
} catch (\Exception $e) {
368-
$message = 'Error cleaning up element: ';
369-
$this->logError($configName, $message . $e->getMessage(), [
370-
'component' => PimcoreDataImporterBundle::LOGGER_COMPONENT_PREFIX . $configName,
371-
'relatedObject' => $element,
372-
]);
352+
if (!($cleanupConfig['doCleanup'] ?? false)) {
353+
return;
354+
}
355+
try {
356+
$element = $resolver->loadElementByIdentifier($identifier);
357+
if ($element === null) {
358+
return;
373359
}
360+
$cleanupStrategy = $this->cleanupStrategyFactory->loadCleanupStrategy($cleanupConfig['strategy']);
361+
if ($cleanupStrategy->doCleanup($element) === false) {
362+
return;
363+
}
364+
$message = "Element {$identifier} cleaned up ({$cleanupConfig['strategy']}) successfully.";
365+
$this->logger->info($message);
366+
$this->applicationLogger->info($message, [
367+
'component' => PimcoreDataImporterBundle::LOGGER_COMPONENT_PREFIX . $configName,
368+
'relatedObject' => $element
369+
]);
370+
} catch (\Exception $e) {
371+
$message = 'Error cleaning up element: ';
372+
$this->logger->error($message . $e);
373+
$this->applicationLogger->error($message . $e->getMessage(), [
374+
'component' => PimcoreDataImporterBundle::LOGGER_COMPONENT_PREFIX . $configName,
375+
'relatedObject' => $element,
376+
]);
374377
}
375378
}
376379

0 commit comments

Comments
 (0)