Skip to content

Commit 27ac6ac

Browse files
committedAug 24, 2024··
Fix console command if entity already exists
1 parent 7bb6f02 commit 27ac6ac

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed
 

‎Console/Command/EraseCommand.php

+20-2
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,13 @@
1010
use Magento\Framework\App\Area;
1111
use Magento\Framework\App\State;
1212
use Magento\Framework\Console\Cli;
13+
use Magento\Framework\Exception\CouldNotSaveException;
1314
use Magento\Framework\Exception\LocalizedException;
15+
use Magento\Framework\Exception\NoSuchEntityException;
1416
use Magento\Framework\Registry;
17+
use Opengento\Gdpr\Api\Data\EraseEntityInterface;
1518
use Opengento\Gdpr\Api\EraseEntityManagementInterface;
19+
use Opengento\Gdpr\Api\EraseEntityRepositoryInterface;
1620
use Symfony\Component\Console\Command\Command;
1721
use Symfony\Component\Console\Helper\ProgressBar;
1822
use Symfony\Component\Console\Input\InputArgument;
@@ -27,7 +31,8 @@ class EraseCommand extends Command
2731
public function __construct(
2832
private State $appState,
2933
private Registry $registry,
30-
private EraseEntityManagementInterface $eraseManagement,
34+
private EraseEntityRepositoryInterface $eraseEntityRepository,
35+
private EraseEntityManagementInterface $eraseEntityManagement,
3136
string $name = 'gdpr:entity:erase'
3237
) {
3338
parent::__construct($name);
@@ -72,7 +77,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
7277

7378
try {
7479
foreach ($entityIds as $entityId) {
75-
$this->eraseManagement->process($this->eraseManagement->create($entityId, $entityType));
80+
$this->eraseEntityManagement->process($this->fetchEntity($entityType, $entityId));
7681
$progressBar->advance();
7782
}
7883
$progressBar->finish();
@@ -86,4 +91,17 @@ protected function execute(InputInterface $input, OutputInterface $output): int
8691

8792
return $returnCode;
8893
}
94+
95+
/**
96+
* @throws CouldNotSaveException
97+
* @throws LocalizedException
98+
*/
99+
private function fetchEntity(int $entityId, string $entityType): EraseEntityInterface
100+
{
101+
try {
102+
return $this->eraseEntityRepository->getByEntity($entityId, $entityType);
103+
} catch (NoSuchEntityException) {
104+
return $this->eraseEntityManagement->create($entityId, $entityType);
105+
}
106+
}
89107
}

‎Console/Command/ExportCommand.php

+21-1
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,13 @@
1111
use Magento\Framework\App\Area;
1212
use Magento\Framework\App\State;
1313
use Magento\Framework\Console\Cli;
14+
use Magento\Framework\Exception\AlreadyExistsException;
15+
use Magento\Framework\Exception\CouldNotSaveException;
1416
use Magento\Framework\Exception\LocalizedException;
17+
use Magento\Framework\Exception\NoSuchEntityException;
18+
use Opengento\Gdpr\Api\Data\ExportEntityInterface;
1519
use Opengento\Gdpr\Api\ExportEntityManagementInterface;
20+
use Opengento\Gdpr\Api\ExportEntityRepositoryInterface;
1621
use Symfony\Component\Console\Command\Command;
1722
use Symfony\Component\Console\Helper\ProgressBar;
1823
use Symfony\Component\Console\Input\InputArgument;
@@ -26,6 +31,7 @@ class ExportCommand extends Command
2631

2732
public function __construct(
2833
private State $appState,
34+
private ExportEntityRepositoryInterface $exportEntityRepository,
2935
private ExportEntityManagementInterface $exportEntityManagement,
3036
string $name = 'gdpr:entity:export'
3137
) {
@@ -70,7 +76,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
7076

7177
try {
7278
foreach ($entityIds as $entityId) {
73-
$exportEntity = $this->exportEntityManagement->create($entityId, $entityType);
79+
$exportEntity = $this->fetchEntity($entityId, $entityType);
7480
$this->exportEntityManagement->export($exportEntity);
7581
$files[] = $exportEntity->getFilePath();
7682
$progressBar->advance();
@@ -87,4 +93,18 @@ protected function execute(InputInterface $input, OutputInterface $output): int
8793

8894
return $resultCode;
8995
}
96+
97+
/**
98+
* @throws AlreadyExistsException
99+
* @throws CouldNotSaveException
100+
* @throws LocalizedException
101+
*/
102+
private function fetchEntity(int $entityId, string $entityType): ExportEntityInterface
103+
{
104+
try {
105+
return $this->exportEntityRepository->getByEntity($entityId, $entityType);
106+
} catch (NoSuchEntityException) {
107+
return $this->exportEntityManagement->create($entityId, $entityType);
108+
}
109+
}
90110
}

0 commit comments

Comments
 (0)
Please sign in to comment.