From 27ac6ac13d9c8bc85fa12d1353d71e9f54177908 Mon Sep 17 00:00:00 2001 From: Thomas Klein Date: Sat, 24 Aug 2024 23:08:23 +0200 Subject: [PATCH] Fix console command if entity already exists --- Console/Command/EraseCommand.php | 22 ++++++++++++++++++++-- Console/Command/ExportCommand.php | 22 +++++++++++++++++++++- 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/Console/Command/EraseCommand.php b/Console/Command/EraseCommand.php index ac8b4df..f6babe1 100644 --- a/Console/Command/EraseCommand.php +++ b/Console/Command/EraseCommand.php @@ -10,9 +10,13 @@ use Magento\Framework\App\Area; use Magento\Framework\App\State; use Magento\Framework\Console\Cli; +use Magento\Framework\Exception\CouldNotSaveException; use Magento\Framework\Exception\LocalizedException; +use Magento\Framework\Exception\NoSuchEntityException; use Magento\Framework\Registry; +use Opengento\Gdpr\Api\Data\EraseEntityInterface; use Opengento\Gdpr\Api\EraseEntityManagementInterface; +use Opengento\Gdpr\Api\EraseEntityRepositoryInterface; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Helper\ProgressBar; use Symfony\Component\Console\Input\InputArgument; @@ -27,7 +31,8 @@ class EraseCommand extends Command public function __construct( private State $appState, private Registry $registry, - private EraseEntityManagementInterface $eraseManagement, + private EraseEntityRepositoryInterface $eraseEntityRepository, + private EraseEntityManagementInterface $eraseEntityManagement, string $name = 'gdpr:entity:erase' ) { parent::__construct($name); @@ -72,7 +77,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int try { foreach ($entityIds as $entityId) { - $this->eraseManagement->process($this->eraseManagement->create($entityId, $entityType)); + $this->eraseEntityManagement->process($this->fetchEntity($entityType, $entityId)); $progressBar->advance(); } $progressBar->finish(); @@ -86,4 +91,17 @@ protected function execute(InputInterface $input, OutputInterface $output): int return $returnCode; } + + /** + * @throws CouldNotSaveException + * @throws LocalizedException + */ + private function fetchEntity(int $entityId, string $entityType): EraseEntityInterface + { + try { + return $this->eraseEntityRepository->getByEntity($entityId, $entityType); + } catch (NoSuchEntityException) { + return $this->eraseEntityManagement->create($entityId, $entityType); + } + } } diff --git a/Console/Command/ExportCommand.php b/Console/Command/ExportCommand.php index 2ad719a..ca1bd0c 100644 --- a/Console/Command/ExportCommand.php +++ b/Console/Command/ExportCommand.php @@ -11,8 +11,13 @@ use Magento\Framework\App\Area; use Magento\Framework\App\State; use Magento\Framework\Console\Cli; +use Magento\Framework\Exception\AlreadyExistsException; +use Magento\Framework\Exception\CouldNotSaveException; use Magento\Framework\Exception\LocalizedException; +use Magento\Framework\Exception\NoSuchEntityException; +use Opengento\Gdpr\Api\Data\ExportEntityInterface; use Opengento\Gdpr\Api\ExportEntityManagementInterface; +use Opengento\Gdpr\Api\ExportEntityRepositoryInterface; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Helper\ProgressBar; use Symfony\Component\Console\Input\InputArgument; @@ -26,6 +31,7 @@ class ExportCommand extends Command public function __construct( private State $appState, + private ExportEntityRepositoryInterface $exportEntityRepository, private ExportEntityManagementInterface $exportEntityManagement, string $name = 'gdpr:entity:export' ) { @@ -70,7 +76,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int try { foreach ($entityIds as $entityId) { - $exportEntity = $this->exportEntityManagement->create($entityId, $entityType); + $exportEntity = $this->fetchEntity($entityId, $entityType); $this->exportEntityManagement->export($exportEntity); $files[] = $exportEntity->getFilePath(); $progressBar->advance(); @@ -87,4 +93,18 @@ protected function execute(InputInterface $input, OutputInterface $output): int return $resultCode; } + + /** + * @throws AlreadyExistsException + * @throws CouldNotSaveException + * @throws LocalizedException + */ + private function fetchEntity(int $entityId, string $entityType): ExportEntityInterface + { + try { + return $this->exportEntityRepository->getByEntity($entityId, $entityType); + } catch (NoSuchEntityException) { + return $this->exportEntityManagement->create($entityId, $entityType); + } + } }