Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Cleanup/CleanupStrategyInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ interface CleanupStrategyInterface
/**
* Apply cleanup on given element
*/
public function doCleanup(ElementInterface $element): void;
public function doCleanup(ElementInterface $element): bool;
}
3 changes: 2 additions & 1 deletion src/Cleanup/DeleteStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@

class DeleteStrategy implements CleanupStrategyInterface
{
public function doCleanup(ElementInterface $element): void
public function doCleanup(ElementInterface $element): bool
{
$element->delete();
return true;
}
}
11 changes: 7 additions & 4 deletions src/Cleanup/UnpublishStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
45 changes: 24 additions & 21 deletions src/Processing/ImportProcessingService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
]);
}
}

Expand Down
Loading