forked from opengento/magento2-gdpr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEraseEntityManagement.php
127 lines (107 loc) · 4.49 KB
/
EraseEntityManagement.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
/**
* Copyright © OpenGento, All rights reserved.
* See LICENSE bundled with this library for license details.
*/
declare(strict_types=1);
namespace Opengento\Gdpr\Model;
use Exception;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Exception\CouldNotSaveException;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Phrase;
use Magento\Framework\Stdlib\DateTime as DateTimeFormat;
use Magento\Framework\Stdlib\DateTime\DateTime;
use Magento\Store\Model\ScopeInterface;
use Opengento\Gdpr\Api\Data\EraseEntityInterface;
use Opengento\Gdpr\Api\Data\EraseEntityInterfaceFactory;
use Opengento\Gdpr\Api\EraseEntityManagementInterface;
use Opengento\Gdpr\Api\EraseEntityRepositoryInterface;
use Opengento\Gdpr\Service\Erase\ProcessorFactory;
final class EraseEntityManagement implements EraseEntityManagementInterface
{
private const CONFIG_PATH_ERASURE_DELAY = 'gdpr/erasure/delay';
private EraseEntityInterfaceFactory $eraseEntityFactory;
private EraseEntityRepositoryInterface $eraseRepository;
private ProcessorFactory $processorFactory;
private ScopeConfigInterface $scopeConfig;
private DateTime $localeDate;
public function __construct(
EraseEntityInterfaceFactory $eraseEntityFactory,
EraseEntityRepositoryInterface $eraseRepository,
ProcessorFactory $processorFactory,
ScopeConfigInterface $scopeConfig,
DateTime $localeDate
) {
$this->eraseEntityFactory = $eraseEntityFactory;
$this->eraseRepository = $eraseRepository;
$this->processorFactory = $processorFactory;
$this->scopeConfig = $scopeConfig;
$this->localeDate = $localeDate;
}
public function create(int $entityId, string $entityType): EraseEntityInterface
{
/** @var EraseEntityInterface $entity */
$entity = $this->eraseEntityFactory->create();
$entity->setEntityId($entityId);
$entity->setEntityType($entityType);
$entity->setState(EraseEntityInterface::STATE_PENDING);
$entity->setStatus(EraseEntityInterface::STATUS_READY);
$entity->setScheduledAt($this->retrieveScheduledAt());
return $this->eraseRepository->save($entity);
}
public function cancel(int $entityId, string $entityType): bool
{
return $this->eraseRepository->delete($this->eraseRepository->getByEntity($entityId, $entityType));
}
public function process(EraseEntityInterface $entity): EraseEntityInterface
{
$entity->setState(EraseEntityInterface::STATE_PROCESSING);
$entity->setStatus(EraseEntityInterface::STATUS_RUNNING);
$entity = $this->eraseRepository->save($entity);
$eraser = $this->processorFactory->get($entity->getEntityType());
try {
return $eraser->execute($entity->getEntityId()) ? $this->success($entity) : $this->fail($entity);
} catch (Exception $e) {
$this->fail($entity, $e->getMessage());
throw new LocalizedException(new Phrase('Impossible to process the erasure: %1', [$e->getMessage()]));
}
}
/**
* @param EraseEntityInterface $entity
* @return EraseEntityInterface
* @throws CouldNotSaveException
*/
private function success(EraseEntityInterface $entity): EraseEntityInterface
{
$entity->setState(EraseEntityInterface::STATE_COMPLETE);
$entity->setStatus(EraseEntityInterface::STATUS_SUCCEED);
$entity->setErasedAt($this->localeDate->gmtDate());
$entity->setMessage(null);
return $this->eraseRepository->save($entity);
}
/**
* @param EraseEntityInterface $entity
* @param string|null $message [optional]
* @return EraseEntityInterface
* @throws CouldNotSaveException
*/
private function fail(EraseEntityInterface $entity, ?string $message = null): EraseEntityInterface
{
$entity->setState(EraseEntityInterface::STATE_PROCESSING);
$entity->setStatus(EraseEntityInterface::STATUS_FAILED);
$entity->setMessage($message);
return $this->eraseRepository->save($entity);
}
private function retrieveScheduledAt(): string
{
return $this->localeDate->gmtDate(
DateTimeFormat::DATETIME_PHP_FORMAT,
$this->resolveErasureDelay() * 60 + $this->localeDate->gmtTimestamp()
);
}
private function resolveErasureDelay(): int
{
return (int) $this->scopeConfig->getValue(self::CONFIG_PATH_ERASURE_DELAY, ScopeInterface::SCOPE_STORE);
}
}