Skip to content

Commit 8c8db88

Browse files
committed
Add notifier decorator
1 parent 24eb487 commit 8c8db88

7 files changed

+216
-62
lines changed

Model/Erase/NotifierRepository.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
/**
3+
* Copyright © OpenGento, All rights reserved.
4+
* See LICENSE bundled with this library for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Opengento\Gdpr\Model\Erase;
9+
10+
use Magento\Framework\Exception\NoSuchEntityException;
11+
use Magento\Framework\Phrase;
12+
13+
/**
14+
* @api
15+
*/
16+
class NotifierRepository
17+
{
18+
public function __construct(private array $notifiers = []) {}
19+
20+
/**
21+
* @throws NoSuchEntityException
22+
*/
23+
public function get(string $entityType, string $action): NotifierInterface
24+
{
25+
return $this->notifiers[$action][$entityType] ?? throw $this->createException($entityType, $action);
26+
}
27+
28+
private function createException(string $entityType, string $action): NoSuchEntityException
29+
{
30+
return new NoSuchEntityException(
31+
new Phrase('No such notifier for entity type "' . $entityType . '" and action "' . $action . '"')
32+
);
33+
}
34+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
/**
3+
* Copyright © OpenGento, All rights reserved.
4+
* See LICENSE bundled with this library for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Opengento\Gdpr\Model\Erase;
9+
10+
use Opengento\Gdpr\Api\Data\EraseEntityInterface;
11+
use Opengento\Gdpr\Api\EraseEntityManagementInterface;
12+
use Opengento\Gdpr\Api\EraseEntityRepositoryInterface;
13+
14+
class NotifyEraseEntityManagement implements EraseEntityManagementInterface
15+
{
16+
public function __construct(
17+
private EraseEntityManagementInterface $eraseManagement,
18+
private EraseEntityRepositoryInterface $eraseRepository,
19+
private NotifierRepository $notifierRepository
20+
) {}
21+
22+
public function create(int $entityId, string $entityType): EraseEntityInterface
23+
{
24+
$eraseEntity = $this->eraseManagement->create($entityId, $entityType);
25+
$this->notifierRepository->get($entityType, 'pending')->notify($eraseEntity);
26+
27+
return $eraseEntity;
28+
}
29+
30+
public function cancel(int $entityId, string $entityType): bool
31+
{
32+
$canceled = $this->eraseManagement->cancel($entityId, $entityType);
33+
if ($canceled) {
34+
$this->notifierRepository->get($entityType, 'cancel')->notify($this->eraseRepository->getByEntity($entityId, $entityType));
35+
}
36+
37+
return $canceled;
38+
}
39+
40+
public function process(EraseEntityInterface $entity): EraseEntityInterface
41+
{
42+
$eraseEntity = $this->eraseManagement->process($entity);
43+
$this->notifierRepository->get($entity->getEntityType(), 'success')->notify($eraseEntity);
44+
45+
return $eraseEntity;
46+
}
47+
}

Model/EraseEntityManagement.php

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -25,29 +25,13 @@ class EraseEntityManagement implements EraseEntityManagementInterface
2525
{
2626
private const CONFIG_PATH_ERASURE_DELAY = 'gdpr/erasure/delay';
2727

28-
private EraseEntityInterfaceFactory $eraseEntityFactory;
29-
30-
private EraseEntityRepositoryInterface $eraseRepository;
31-
32-
private ProcessorFactory $processorFactory;
33-
34-
private ScopeConfigInterface $scopeConfig;
35-
36-
private DateTime $localeDate;
37-
3828
public function __construct(
39-
EraseEntityInterfaceFactory $eraseEntityFactory,
40-
EraseEntityRepositoryInterface $eraseRepository,
41-
ProcessorFactory $processorFactory,
42-
ScopeConfigInterface $scopeConfig,
43-
DateTime $localeDate
44-
) {
45-
$this->eraseEntityFactory = $eraseEntityFactory;
46-
$this->eraseRepository = $eraseRepository;
47-
$this->processorFactory = $processorFactory;
48-
$this->scopeConfig = $scopeConfig;
49-
$this->localeDate = $localeDate;
50-
}
29+
private EraseEntityInterfaceFactory $eraseEntityFactory,
30+
private EraseEntityRepositoryInterface $eraseRepository,
31+
private ProcessorFactory $processorFactory,
32+
private ScopeConfigInterface $scopeConfig,
33+
private DateTime $localeDate
34+
) {}
5135

5236
public function create(int $entityId, string $entityType): EraseEntityInterface
5337
{
@@ -83,8 +67,6 @@ public function process(EraseEntityInterface $entity): EraseEntityInterface
8367
}
8468

8569
/**
86-
* @param EraseEntityInterface $entity
87-
* @return EraseEntityInterface
8870
* @throws CouldNotSaveException
8971
*/
9072
private function success(EraseEntityInterface $entity): EraseEntityInterface
@@ -98,9 +80,6 @@ private function success(EraseEntityInterface $entity): EraseEntityInterface
9880
}
9981

10082
/**
101-
* @param EraseEntityInterface $entity
102-
* @param string|null $message [optional]
103-
* @return EraseEntityInterface
10483
* @throws CouldNotSaveException
10584
*/
10685
private function fail(EraseEntityInterface $entity, ?string $message = null): EraseEntityInterface

Model/Export/NotifierRepository.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
/**
3+
* Copyright © OpenGento, All rights reserved.
4+
* See LICENSE bundled with this library for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Opengento\Gdpr\Model\Export;
9+
10+
use Magento\Framework\Exception\NoSuchEntityException;
11+
use Magento\Framework\Phrase;
12+
13+
/**
14+
* @api
15+
*/
16+
class NotifierRepository
17+
{
18+
public function __construct(private array $notifiers = []) {}
19+
20+
/**
21+
* @throws NoSuchEntityException
22+
*/
23+
public function get(string $entityType, string $action): NotifierInterface
24+
{
25+
return $this->notifiers[$action][$entityType] ?? throw $this->createException($entityType, $action);
26+
}
27+
28+
private function createException(string $entityType, string $action): NoSuchEntityException
29+
{
30+
return new NoSuchEntityException(
31+
new Phrase('No such notifier for entity type "' . $entityType . '" and action "' . $action . '"')
32+
);
33+
}
34+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
/**
3+
* Copyright © OpenGento, All rights reserved.
4+
* See LICENSE bundled with this library for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Opengento\Gdpr\Model\Export;
9+
10+
use Opengento\Gdpr\Api\Data\ExportEntityInterface;
11+
use Opengento\Gdpr\Api\ExportEntityManagementInterface;
12+
13+
class NotifyExportEntityManagement implements ExportEntityManagementInterface
14+
{
15+
public function __construct(
16+
private ExportEntityManagementInterface $exportManagement,
17+
private NotifierRepository $notifierRepository
18+
) {}
19+
20+
public function create(int $entityId, string $entityType, ?string $fileName = null): ExportEntityInterface
21+
{
22+
$exportEntity = $this->exportManagement->create($entityId, $entityType, $fileName);
23+
$this->notifierRepository->get($entityType, 'pending')->notify($exportEntity);
24+
25+
return $exportEntity;
26+
}
27+
28+
public function export(ExportEntityInterface $exportEntity): ExportEntityInterface
29+
{
30+
$exportEntity = $this->exportManagement->export($exportEntity);
31+
$this->notifierRepository->get($exportEntity->getEntityType(), 'ready')->notify($exportEntity);
32+
33+
34+
return $exportEntity;
35+
}
36+
37+
public function invalidate(ExportEntityInterface $exportEntity): ExportEntityInterface
38+
{
39+
return $this->exportManagement->invalidate($exportEntity);
40+
}
41+
}

Model/ExportEntityManagement.php

Lines changed: 8 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -26,32 +26,13 @@ class ExportEntityManagement implements ExportEntityManagementInterface
2626
private const CONFIG_PATH_EXPORT_FILE_NAME = 'gdpr/export/file_name';
2727
private const CONFIG_PATH_EXPORT_LIFE_TIME = 'gdpr/export/life_time';
2828

29-
private ExportEntityInterfaceFactory $exportEntityFactory;
30-
31-
private ExportEntityRepositoryInterface $exportRepository;
32-
33-
private ExportEntityCheckerInterface $exportEntityChecker;
34-
35-
/**
36-
* @var ExportToFile
37-
*/
38-
private ExportToFile $exportToFile;
39-
40-
private ScopeConfigInterface $scopeConfig;
41-
4229
public function __construct(
43-
ExportEntityInterfaceFactory $exportEntityFactory,
44-
ExportEntityRepositoryInterface $exportRepository,
45-
ExportEntityCheckerInterface $exportEntityChecker,
46-
ExportToFile $exportToFile,
47-
ScopeConfigInterface $scopeConfig
48-
) {
49-
$this->exportEntityFactory = $exportEntityFactory;
50-
$this->exportRepository = $exportRepository;
51-
$this->exportEntityChecker = $exportEntityChecker;
52-
$this->exportToFile = $exportToFile;
53-
$this->scopeConfig = $scopeConfig;
54-
}
30+
private ExportEntityInterfaceFactory $exportEntityFactory,
31+
private ExportEntityRepositoryInterface $exportRepository,
32+
private ExportEntityCheckerInterface $exportEntityChecker,
33+
private ExportToFile $exportToFile,
34+
private ScopeConfigInterface $scopeConfig
35+
) {}
5536

5637
public function create(int $entityId, string $entityType, ?string $fileName = null): ExportEntityInterface
5738
{
@@ -69,15 +50,10 @@ public function create(int $entityId, string $entityType, ?string $fileName = nu
6950
$exportEntity->setEntityId($entityId);
7051
$exportEntity->setEntityType($entityType);
7152
$exportEntity->setFileName($fileName ?? $this->resolveDefaultFileName());
72-
$exportEntity = $this->exportRepository->save($exportEntity);
7353

74-
return $exportEntity;
54+
return $this->exportRepository->save($exportEntity);
7555
}
7656

77-
/**
78-
* @inheritdoc
79-
* @throws Exception
80-
*/
8157
public function export(ExportEntityInterface $exportEntity): ExportEntityInterface
8258
{
8359
$lifeTime = (int)$this->scopeConfig->getValue(self::CONFIG_PATH_EXPORT_LIFE_TIME, ScopeInterface::SCOPE_STORE);//Todo scope website
@@ -86,9 +62,8 @@ public function export(ExportEntityInterface $exportEntity): ExportEntityInterfa
8662
(new DateTime('+' . $lifeTime . 'minutes'))->format(DateTimeFormat::DATETIME_PHP_FORMAT)
8763
);
8864
$exportEntity->setExportedAt((new DateTime())->format(DateTimeFormat::DATETIME_PHP_FORMAT));
89-
$this->exportRepository->save($exportEntity);
9065

91-
return $exportEntity;
66+
return $this->exportRepository->save($exportEntity);
9267
}
9368

9469
public function invalidate(ExportEntityInterface $exportEntity): ExportEntityInterface

etc/di.xml

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,14 @@
1010
<preference for="Opengento\Gdpr\Api\Data\EraseEntityInterface" type="Opengento\Gdpr\Model\EraseEntity"/>
1111
<preference for="Opengento\Gdpr\Api\Data\EraseEntitySearchResultsInterface" type="Magento\Framework\Api\SearchResults"/>
1212
<preference for="Opengento\Gdpr\Api\EraseEntityRepositoryInterface" type="Opengento\Gdpr\Model\EraseEntityRepository"/>
13-
<preference for="Opengento\Gdpr\Api\EraseEntityManagementInterface" type="Opengento\Gdpr\Model\EraseEntityManagement"/>
13+
<preference for="Opengento\Gdpr\Api\EraseEntityManagementInterface" type="Opengento\Gdpr\Model\Erase\NotifyEraseEntityManagement"/>
1414
<preference for="Opengento\Gdpr\Api\EraseEntityCheckerInterface" type="Opengento\Gdpr\Model\EraseEntityChecker"/>
1515
<type name="Opengento\Gdpr\Model\Erase\SecureEraseEntityManagement">
16+
<arguments>
17+
<argument name="eraseManagement" xsi:type="object">Opengento\Gdpr\Model\Erase\NotifyEraseEntityManagement</argument>
18+
</arguments>
19+
</type>
20+
<type name="Opengento\Gdpr\Model\Erase\NotifyEraseEntityManagement">
1621
<arguments>
1722
<argument name="eraseManagement" xsi:type="object">Opengento\Gdpr\Model\EraseEntityManagement</argument>
1823
</arguments>
@@ -22,8 +27,13 @@
2227
<preference for="Opengento\Gdpr\Api\Data\ExportEntityInterface" type="Opengento\Gdpr\Model\ExportEntity"/>
2328
<preference for="Opengento\Gdpr\Api\Data\ExportEntitySearchResultsInterface" type="Magento\Framework\Api\SearchResults"/>
2429
<preference for="Opengento\Gdpr\Api\ExportEntityRepositoryInterface" type="Opengento\Gdpr\Model\ExportEntityRepository"/>
25-
<preference for="Opengento\Gdpr\Api\ExportEntityManagementInterface" type="Opengento\Gdpr\Model\ExportEntityManagement"/>
30+
<preference for="Opengento\Gdpr\Api\ExportEntityManagementInterface" type="Opengento\Gdpr\Model\Export\NotifyExportEntityManagement"/>
2631
<preference for="Opengento\Gdpr\Api\ExportEntityCheckerInterface" type="Opengento\Gdpr\Model\ExportEntityChecker"/>
32+
<type name="Opengento\Gdpr\Model\Export\NotifyExportEntityManagement">
33+
<arguments>
34+
<argument name="exportManagement" xsi:type="object">Opengento\Gdpr\Model\ExportEntityManagement</argument>
35+
</arguments>
36+
</type>
2737
<!-- Service Contract -->
2838
<type name="Magento\Framework\Model\Entity\RepositoryFactory">
2939
<arguments>
@@ -1525,6 +1535,25 @@
15251535
</argument>
15261536
</arguments>
15271537
</virtualType>
1538+
<!-- Erase Notifier Repository -->
1539+
<type name="Opengento\Gdpr\Model\Erase\NotifierRepository">
1540+
<arguments>
1541+
<argument name="notifiers" xsi:type="array">
1542+
<item name="pending" xsi:type="array">
1543+
<item name="customer" xsi:type="object">Opengento\Gdpr\Model\Customer\Erase\Notifier\Pending\Proxy</item>
1544+
<item name="order" xsi:type="object">Opengento\Gdpr\Model\Order\Erase\Notifier\Pending\Proxy</item>
1545+
</item>
1546+
<item name="success" xsi:type="array">
1547+
<item name="customer" xsi:type="object">Opengento\Gdpr\Model\Customer\Erase\Notifier\Succeeded\Proxy</item>
1548+
<item name="order" xsi:type="object">Opengento\Gdpr\Model\Order\Erase\Notifier\Succeeded\Proxy</item>
1549+
</item>
1550+
<item name="cancel" xsi:type="array">
1551+
<item name="customer" xsi:type="object">Opengento\Gdpr\Model\Customer\Erase\Notifier\Canceled\Proxy</item>
1552+
<item name="order" xsi:type="object">Opengento\Gdpr\Model\Order\Erase\Notifier\Canceled\Proxy</item>
1553+
</item>
1554+
</argument>
1555+
</arguments>
1556+
</type>
15281557
<!-- Export Logged In Customer Notifier Management -->
15291558
<virtualType name="Opengento\Gdpr\Model\Customer\Export\Notifier\Pending\MailSender" type="Opengento\Gdpr\Model\Customer\Notifier\MailSender">
15301559
<arguments>
@@ -1599,6 +1628,21 @@
15991628
</argument>
16001629
</arguments>
16011630
</virtualType>
1631+
<!-- Export Notifier Repository -->
1632+
<type name="Opengento\Gdpr\Model\Export\NotifierRepository">
1633+
<arguments>
1634+
<argument name="notifiers" xsi:type="array">
1635+
<item name="pending" xsi:type="array">
1636+
<item name="customer" xsi:type="object">Opengento\Gdpr\Model\Customer\Export\Notifier\Pending\Proxy</item>
1637+
<item name="order" xsi:type="object">Opengento\Gdpr\Model\Order\Export\Notifier\Pending\Proxy</item>
1638+
</item>
1639+
<item name="ready" xsi:type="array">
1640+
<item name="customer" xsi:type="object">Opengento\Gdpr\Model\Customer\Export\Notifier\Ready\Proxy</item>
1641+
<item name="order" xsi:type="object">Opengento\Gdpr\Model\Order\Export\Notifier\Ready\Proxy</item>
1642+
</item>
1643+
</argument>
1644+
</arguments>
1645+
</type>
16021646
<!-- Console Commands Management -->
16031647
<type name="Opengento\Gdpr\Console\Command\EraseCommand">
16041648
<arguments>

0 commit comments

Comments
 (0)