Skip to content

Commit 22d90fd

Browse files
committed
WIP fix scopable config
1 parent 41784af commit 22d90fd

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+327
-449
lines changed

Console/Command/EraseCommand.php

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,14 @@
77

88
namespace Opengento\Gdpr\Console\Command;
99

10-
use Magento\Framework\Api\SearchCriteriaBuilder;
1110
use Magento\Framework\App\Area;
1211
use Magento\Framework\App\State;
1312
use Magento\Framework\Console\Cli;
1413
use Magento\Framework\Exception\LocalizedException;
1514
use Magento\Framework\Registry;
16-
use Opengento\Gdpr\Api\Data\EraseEntityInterface;
1715
use Opengento\Gdpr\Api\EraseEntityManagementInterface;
18-
use Opengento\Gdpr\Api\EraseEntityRepositoryInterface;
1916
use Symfony\Component\Console\Command\Command;
17+
use Symfony\Component\Console\Helper\ProgressBar;
2018
use Symfony\Component\Console\Input\InputArgument;
2119
use Symfony\Component\Console\Input\InputInterface;
2220
use Symfony\Component\Console\Output\OutputInterface;
@@ -30,8 +28,6 @@ public function __construct(
3028
private State $appState,
3129
private Registry $registry,
3230
private EraseEntityManagementInterface $eraseManagement,
33-
private EraseEntityRepositoryInterface $eraseEntityRepository,
34-
private SearchCriteriaBuilder $searchCriteriaBuilder,
3531
string $name = 'gdpr:entity:erase'
3632
) {
3733
parent::__construct($name);
@@ -71,17 +67,16 @@ protected function execute(InputInterface $input, OutputInterface $output): int
7167
$entityIds = $input->getArgument(self::INPUT_ARGUMENT_ENTITY_ID);
7268
$entityType = $input->getArgument(self::INPUT_ARGUMENT_ENTITY_TYPE);
7369

74-
try {
75-
$this->searchCriteriaBuilder->addFilter(EraseEntityInterface::ENTITY_ID, $entityIds, 'in');
76-
$this->searchCriteriaBuilder->addFilter(EraseEntityInterface::ENTITY_TYPE, $entityType);
77-
$eraseEntityList = $this->eraseEntityRepository->getList($this->searchCriteriaBuilder->create());
78-
foreach ($eraseEntityList->getItems() as $eraseEntity) {
79-
$this->eraseManagement->process($eraseEntity);
70+
$progressBar = new ProgressBar($output, count($entityIds));
71+
$progressBar->start();
8072

81-
$output->writeln(
82-
'<info>Entity\'s (' . $entityType . ') with ID "' . $eraseEntity->getEntityId() . '" has been erased.</info>'
83-
);
73+
try {
74+
foreach ($entityIds as $entityId) {
75+
$this->eraseManagement->process($this->eraseManagement->create($entityId, $entityType));
76+
$progressBar->advance();
8477
}
78+
$progressBar->finish();
79+
$output->writeln('<info>Entities has been erased.</info>');
8580
} catch (LocalizedException $e) {
8681
$output->writeln('<error>' . $e->getMessage() . '</error>');
8782
$returnCode = Cli::RETURN_FAILURE;

Console/Command/ExportCommand.php

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,25 @@
88
namespace Opengento\Gdpr\Console\Command;
99

1010
use Exception;
11-
use Magento\Framework\Api\SearchCriteriaBuilder;
1211
use Magento\Framework\App\Area;
1312
use Magento\Framework\App\State;
1413
use Magento\Framework\Console\Cli;
1514
use Magento\Framework\Exception\LocalizedException;
16-
use Opengento\Gdpr\Api\Data\ExportEntityInterface;
1715
use Opengento\Gdpr\Api\ExportEntityManagementInterface;
18-
use Opengento\Gdpr\Api\ExportEntityRepositoryInterface;
1916
use Symfony\Component\Console\Command\Command;
17+
use Symfony\Component\Console\Helper\ProgressBar;
2018
use Symfony\Component\Console\Input\InputArgument;
2119
use Symfony\Component\Console\Input\InputInterface;
22-
use Symfony\Component\Console\Input\InputOption;
2320
use Symfony\Component\Console\Output\OutputInterface;
2421

2522
class ExportCommand extends Command
2623
{
2724
private const INPUT_ARGUMENT_ENTITY_ID = 'entity_id';
2825
private const INPUT_ARGUMENT_ENTITY_TYPE = 'entity_type';
29-
private const INPUT_OPTION_FILENAME = 'filename';
3026

3127
public function __construct(
3228
private State $appState,
3329
private ExportEntityManagementInterface $exportEntityManagement,
34-
private ExportEntityRepositoryInterface $exportEntityRepository,
35-
private SearchCriteriaBuilder $searchCriteriaBuilder,
3630
string $name = 'gdpr:entity:export'
3731
) {
3832
parent::__construct($name);
@@ -53,13 +47,6 @@ protected function configure(): void
5347
self::INPUT_ARGUMENT_ENTITY_ID,
5448
InputArgument::REQUIRED + InputArgument::IS_ARRAY,
5549
'Entity ID'
56-
),
57-
new InputOption(
58-
self::INPUT_OPTION_FILENAME,
59-
'-f',
60-
InputOption::VALUE_OPTIONAL,
61-
'Export file name',
62-
'personal_data'
6350
)
6451
]);
6552
}
@@ -75,18 +62,23 @@ protected function execute(InputInterface $input, OutputInterface $output): int
7562
$resultCode = Cli::RETURN_SUCCESS;
7663
$entityIds = $input->getArgument(self::INPUT_ARGUMENT_ENTITY_ID);
7764
$entityType = $input->getArgument(self::INPUT_ARGUMENT_ENTITY_TYPE);
78-
$fileName = $input->getOption(self::INPUT_OPTION_FILENAME);
65+
66+
$progressBar = new ProgressBar($output, count($entityIds));
67+
$progressBar->start();
68+
69+
$files = [];
7970

8071
try {
81-
$this->searchCriteriaBuilder->addFilter(ExportEntityInterface::ENTITY_ID, $entityIds, 'in');
82-
$this->searchCriteriaBuilder->addFilter(ExportEntityInterface::ENTITY_TYPE, $entityType);
83-
$exportEntityList = $this->exportEntityRepository->getList($this->searchCriteriaBuilder->create());
84-
foreach ($exportEntityList->getItems() as $exportEntity) {
72+
foreach ($entityIds as $entityId) {
73+
$exportEntity = $this->exportEntityManagement->create($entityId, $entityType);
8574
$this->exportEntityManagement->export($exportEntity);
86-
87-
$output->writeln(
88-
'<info>Entity\'s related data have been exported to: ' . $exportEntity->getFilePath() . '.</info>'
89-
);
75+
$files[] = $exportEntity->getFilePath();
76+
$progressBar->advance();
77+
}
78+
$progressBar->finish();
79+
$output->writeln('<info>Entities data have been exported to:</info>');
80+
foreach ($files as $file) {
81+
$output->writeln($file);
9082
}
9183
} catch (Exception $e) {
9284
$output->writeln('<error>' . $e->getMessage() . '</error>');

Controller/Guest/Download.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function __construct(
5353

5454
protected function isAllowed(): bool
5555
{
56-
return parent::isAllowed() && $this->config->isExportEnabled();
56+
return $this->config->isExportEnabled();
5757
}
5858

5959
protected function executeAction(): ResultInterface|ResponseInterface|Redirect

Controller/Guest/Erase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function __construct(
3737

3838
protected function isAllowed(): bool
3939
{
40-
return parent::isAllowed() && $this->config->isErasureEnabled();
40+
return $this->config->isErasureEnabled();
4141
}
4242

4343
protected function executeAction(): Redirect

Controller/Guest/Export.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function __construct(
3838

3939
protected function isAllowed(): bool
4040
{
41-
return parent::isAllowed() && $this->config->isExportEnabled();
41+
return $this->config->isExportEnabled();
4242
}
4343

4444
protected function executeAction(): Redirect

Controller/Guest/UndoErase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function __construct(
3737

3838
protected function isAllowed(): bool
3939
{
40-
return parent::isAllowed() && $this->config->isErasureEnabled();
40+
return $this->config->isErasureEnabled();
4141
}
4242

4343
protected function executeAction(): Redirect

Controller/Privacy/Download.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function __construct(
4343

4444
protected function isAllowed(): bool
4545
{
46-
return parent::isAllowed() && $this->config->isExportEnabled();
46+
return $this->config->isExportEnabled();
4747
}
4848

4949
protected function executeAction(): ResultInterface|ResponseInterface|Redirect

Controller/Privacy/Erase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function __construct(
3636

3737
protected function isAllowed(): bool
3838
{
39-
return parent::isAllowed() && $this->config->isErasureEnabled();
39+
return $this->config->isErasureEnabled();
4040
}
4141

4242
protected function executeAction(): Page|Redirect

Controller/Privacy/ErasePost.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function __construct(
4242

4343
protected function isAllowed(): bool
4444
{
45-
return parent::isAllowed() && $this->config->isErasureEnabled();
45+
return $this->config->isErasureEnabled();
4646
}
4747

4848
protected function executeAction(): Redirect

Controller/Privacy/Export.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function __construct(
3838

3939
protected function isAllowed(): bool
4040
{
41-
return parent::isAllowed() && $this->config->isExportEnabled();
41+
return $this->config->isExportEnabled();
4242
}
4343

4444
protected function executeAction(): Redirect

Controller/Privacy/UndoErase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function __construct(
3737

3838
protected function isAllowed(): bool
3939
{
40-
return parent::isAllowed() && $this->config->isErasureEnabled();
40+
return $this->config->isErasureEnabled();
4141
}
4242

4343
protected function executeAction(): Redirect

Cron/EraseEntity.php

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
use Opengento\Gdpr\Api\Data\EraseEntitySearchResultsInterface;
1818
use Opengento\Gdpr\Api\EraseEntityManagementInterface;
1919
use Opengento\Gdpr\Api\EraseEntityRepositoryInterface;
20-
use Opengento\Gdpr\Model\Config;
2120
use Psr\Log\LoggerInterface;
2221

2322
/**
@@ -27,7 +26,6 @@ class EraseEntity
2726
{
2827
public function __construct(
2928
private LoggerInterface $logger,
30-
private Config $config,
3129
private Registry $registry,
3230
private EraseEntityManagementInterface $eraseManagement,
3331
private EraseEntityRepositoryInterface $eraseRepository,
@@ -37,24 +35,27 @@ public function __construct(
3735

3836
public function execute(): void
3937
{
40-
if ($this->config->isModuleEnabled() && $this->config->isErasureEnabled()) {
41-
$oldValue = $this->registry->registry('isSecureArea');
42-
$this->registry->register('isSecureArea', true, true);
38+
$oldValue = $this->registry->registry('isSecureArea');
39+
$this->registry->register('isSecureArea', true, true);
4340

41+
try {
4442
foreach ($this->retrieveEraseEntityList()->getItems() as $eraseEntity) {
4543
try {
4644
$this->eraseManagement->process($eraseEntity);
4745
} catch (Exception $e) {
48-
$this->logger->error($e->getMessage(), $e->getTrace());
46+
$this->logger->error($e->getMessage(), ['exception' => $e]);
4947
}
5048
}
51-
52-
$this->registry->register('isSecureArea', $oldValue, true);
49+
} catch (LocalizedException $e) {
50+
$this->logger->error($e->getLogMessage(), ['exception' => $e]);
5351
}
52+
53+
$this->registry->register('isSecureArea', $oldValue, true);
5454
}
5555

5656
/**
5757
* @return EraseEntitySearchResultsInterface
58+
* @throws LocalizedException
5859
*/
5960
private function retrieveEraseEntityList(): SearchResultsInterface
6061
{
@@ -74,12 +75,6 @@ private function retrieveEraseEntityList(): SearchResultsInterface
7475
'in'
7576
);
7677

77-
try {
78-
$eraseCustomerList = $this->eraseRepository->getList($this->criteriaBuilder->create());
79-
} catch (LocalizedException) {
80-
$eraseCustomerList = [];
81-
}
82-
83-
return $eraseCustomerList;
78+
return $this->eraseRepository->getList($this->criteriaBuilder->create());
8479
}
8580
}

Cron/EraseEntityScheduler.php

Lines changed: 12 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,8 @@
77

88
namespace Opengento\Gdpr\Cron;
99

10-
use DateTime;
1110
use Exception;
12-
use Magento\Framework\Api\FilterBuilder;
13-
use Magento\Framework\App\Config\ScopeConfigInterface;
14-
use Magento\Framework\Exception\CouldNotSaveException;
15-
use Magento\Framework\Exception\LocalizedException;
16-
use Magento\Store\Model\ScopeInterface;
11+
use Magento\Store\Model\StoreManagerInterface;
1712
use Opengento\Gdpr\Model\Config;
1813
use Opengento\Gdpr\Model\Entity\EntityTypeList;
1914
use Opengento\Gdpr\Model\Erase\EraseEntityScheduler as EraseEntitySchedulerService;
@@ -24,44 +19,26 @@
2419
*/
2520
class EraseEntityScheduler
2621
{
27-
private const CONFIG_PATH_ERASURE_MAX_AGE = 'gdpr/erasure/entity_max_age';
2822

2923
public function __construct(
3024
private LoggerInterface $logger,
31-
private ScopeConfigInterface $scopeConfig,
3225
private Config $config,
3326
private EraseEntitySchedulerService $eraseEntityScheduler,
34-
private FilterBuilder $filterBuilder,
35-
private EntityTypeList $entityTypeList
36-
) {
37-
}
27+
private EntityTypeList $entityTypeList,
28+
private StoreManagerInterface $storeManager
29+
) {}
3830

3931
public function execute(): void
4032
{
41-
if ($this->config->isModuleEnabled() && $this->config->isErasureEnabled()) {
42-
try {
43-
$this->scheduleEntitiesErasure();
44-
} catch (Exception $e) {
45-
$this->logger->error($e->getMessage(), $e->getTrace());
33+
foreach ($this->storeManager->getWebsites() as $website) {
34+
$websiteId = $website->getId();
35+
if ($this->config->isErasureEnabled($websiteId)) {
36+
try {
37+
$this->eraseEntityScheduler->schedule($this->entityTypeList->getEntityTypes(), $website);
38+
} catch (Exception $e) {
39+
$this->logger->error($e->getMessage(), ['exception' => $e]);
40+
}
4641
}
4742
}
4843
}
49-
50-
/**
51-
* @throws CouldNotSaveException
52-
* @throws LocalizedException
53-
* @throws Exception
54-
*/
55-
private function scheduleEntitiesErasure(): void
56-
{
57-
$this->filterBuilder->setField('created_at');
58-
$this->filterBuilder->setValue(new DateTime('-' . $this->resolveErasureMaxAge() . 'days'));
59-
$this->filterBuilder->setConditionType('lteq');
60-
$this->eraseEntityScheduler->schedule($this->entityTypeList->getEntityTypes(), $this->filterBuilder->create());
61-
}
62-
63-
private function resolveErasureMaxAge(): int
64-
{
65-
return (int)$this->scopeConfig->getValue(self::CONFIG_PATH_ERASURE_MAX_AGE, ScopeInterface::SCOPE_STORE);
66-
}
6744
}

0 commit comments

Comments
 (0)