Skip to content

Commit

Permalink
Fix console command if entity already exists
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas-kl1 committed Aug 24, 2024
1 parent 7bb6f02 commit 27ac6ac
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
22 changes: 20 additions & 2 deletions Console/Command/EraseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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();
Expand All @@ -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);
}
}
}
22 changes: 21 additions & 1 deletion Console/Command/ExportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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'
) {
Expand Down Expand Up @@ -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();
Expand All @@ -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);
}
}
}

0 comments on commit 27ac6ac

Please sign in to comment.