forked from opengento/magento2-gdpr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEraseEntityRepository.php
170 lines (141 loc) · 5.78 KB
/
EraseEntityRepository.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?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\Api\SearchCriteria\CollectionProcessorInterface;
use Magento\Framework\Api\SearchCriteriaInterface;
use Magento\Framework\Api\SearchResultsInterface;
use Magento\Framework\Exception\CouldNotDeleteException;
use Magento\Framework\Exception\CouldNotSaveException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Phrase;
use Opengento\Gdpr\Api\Data\EraseEntityInterface;
use Opengento\Gdpr\Api\Data\EraseEntityInterfaceFactory;
use Opengento\Gdpr\Api\Data\EraseEntitySearchResultsInterface;
use Opengento\Gdpr\Api\Data\EraseEntitySearchResultsInterfaceFactory;
use Opengento\Gdpr\Api\EraseEntityRepositoryInterface;
use Opengento\Gdpr\Model\ResourceModel\EraseEntity as EraseEntityResource;
use Opengento\Gdpr\Model\ResourceModel\EraseEntity\Collection;
use Opengento\Gdpr\Model\ResourceModel\EraseEntity\CollectionFactory;
final class EraseEntityRepository implements EraseEntityRepositoryInterface
{
/**
* @var EraseEntityResource
*/
private EraseEntityResource $eraseEntityResource;
private EraseEntityInterfaceFactory $eraseEntityFactory;
private CollectionFactory $collectionFactory;
private CollectionProcessorInterface $collectionProcessor;
/**
* @var EraseEntitySearchResultsInterfaceFactory
*/
private EraseEntitySearchResultsInterfaceFactory $searchResultsFactory;
/**
* @var EraseEntityInterface[]
*/
private array $instances = [];
/**
* @var EraseEntityInterface[]
*/
private array $instancesByEntity = [];
public function __construct(
EraseEntityResource $eraseEntityResource,
EraseEntityInterfaceFactory $eraseEntityFactory,
CollectionFactory $collectionFactory,
CollectionProcessorInterface $collectionProcessor,
EraseEntitySearchResultsInterfaceFactory $searchResultsFactory
) {
$this->eraseEntityResource = $eraseEntityResource;
$this->eraseEntityFactory = $eraseEntityFactory;
$this->collectionFactory = $collectionFactory;
$this->collectionProcessor = $collectionProcessor;
$this->searchResultsFactory = $searchResultsFactory;
}
public function save(EraseEntityInterface $eraseEntity): EraseEntityInterface
{
try {
$this->eraseEntityResource->save($eraseEntity);
$this->register($eraseEntity);
} catch (Exception $e) {
throw new CouldNotSaveException(new Phrase('Could not save the entity.'), $e);
}
return $eraseEntity;
}
public function getById(int $eraseId): EraseEntityInterface
{
if (!isset($this->instances[$eraseId])) {
/** @var EraseEntityInterface $eraseEntity */
$eraseEntity = $this->eraseEntityFactory->create();
$this->eraseEntityResource->load($eraseEntity, $eraseId, EraseEntityInterface::ID);
if (!$eraseEntity->getEraseId()) {
throw NoSuchEntityException::singleField(EraseEntityInterface::ID, $eraseId);
}
$this->register($eraseEntity);
}
return $this->instances[$eraseId];
}
public function getByEntity(int $entityId, string $entityType): EraseEntityInterface
{
$key = $entityType . '_' . $entityId;
if (!isset($this->instancesByEntity[$key])) {
/** @var EraseEntityInterface $eraseEntity */
$eraseEntity = $this->eraseEntityFactory->create();
$this->eraseEntityResource->load(
$eraseEntity,
[$entityId, $entityType],
[EraseEntityInterface::ENTITY_ID, EraseEntityInterface::ENTITY_TYPE]
);
if (!$eraseEntity->getEraseId()) {
throw NoSuchEntityException::doubleField(
EraseEntityInterface::ENTITY_ID,
$entityId,
EraseEntityInterface::ENTITY_TYPE,
$entityType
);
}
$this->register($eraseEntity);
}
return $this->instancesByEntity[$key];
}
public function getList(SearchCriteriaInterface $searchCriteria): SearchResultsInterface
{
/** @var Collection $collection */
$collection = $this->collectionFactory->create();
$this->collectionProcessor->process($searchCriteria, $collection);
/** @var EraseEntitySearchResultsInterface $searchResults */
$searchResults = $this->searchResultsFactory->create();
$searchResults->setSearchCriteria($searchCriteria);
$searchResults->setItems($collection->getItems());
$searchResults->setTotalCount($collection->getSize());
return $searchResults;
}
public function delete(EraseEntityInterface $eraseEntity): bool
{
try {
$this->remove($eraseEntity);
$this->eraseEntityResource->delete($eraseEntity);
} catch (Exception $e) {
throw new CouldNotDeleteException(
new Phrase('Could not delete entity with id "%1".', [$eraseEntity->getEraseId()]),
$e
);
}
return true;
}
private function register(EraseEntityInterface $eraseEntity): void
{
$this->instances[$eraseEntity->getEraseId()] = $eraseEntity;
$this->instancesByEntity[$eraseEntity->getEntityType() . '_' . $eraseEntity->getEntityId()] = $eraseEntity;
}
private function remove(EraseEntityInterface $eraseEntity): void
{
unset(
$this->instances[$eraseEntity->getEraseId()],
$this->instancesByEntity[$eraseEntity->getEntityType() . '_' . $eraseEntity->getEntityId()]
);
}
}