forked from opengento/magento2-gdpr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExportEntityRepository.php
173 lines (143 loc) · 6.12 KB
/
ExportEntityRepository.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
171
172
173
<?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\App\Filesystem\DirectoryList;
use Magento\Framework\Exception\CouldNotDeleteException;
use Magento\Framework\Exception\CouldNotSaveException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Filesystem;
use Magento\Framework\Phrase;
use Opengento\Gdpr\Api\Data\ExportEntityInterface;
use Opengento\Gdpr\Api\Data\ExportEntityInterfaceFactory;
use Opengento\Gdpr\Api\Data\ExportEntitySearchResultsInterface;
use Opengento\Gdpr\Api\Data\ExportEntitySearchResultsInterfaceFactory;
use Opengento\Gdpr\Api\ExportEntityRepositoryInterface;
use Opengento\Gdpr\Model\ResourceModel\ExportEntity as ExportEntityResource;
use Opengento\Gdpr\Model\ResourceModel\ExportEntity\Collection;
use Opengento\Gdpr\Model\ResourceModel\ExportEntity\CollectionFactory;
final class ExportEntityRepository implements ExportEntityRepositoryInterface
{
private ExportEntityResource $exportEntityResource;
private ExportEntityInterfaceFactory $exportEntityFactory;
private CollectionFactory $collectionFactory;
private CollectionProcessorInterface $collectionProcessor;
private ExportEntitySearchResultsInterfaceFactory $searchResultsFactory;
private Filesystem $fileSystem;
/**
* @var ExportEntityInterface[]
*/
private array $instances = [];
/**
* @var ExportEntityInterface[]
*/
private array $instancesByEntity = [];
public function __construct(
ExportEntityResource $exportEntityResource,
ExportEntityInterfaceFactory $exportEntityFactory,
CollectionFactory $collectionFactory,
CollectionProcessorInterface $collectionProcessor,
ExportEntitySearchResultsInterfaceFactory $searchResultsFactory,
Filesystem $filesystem
) {
$this->exportEntityResource = $exportEntityResource;
$this->exportEntityFactory = $exportEntityFactory;
$this->collectionFactory = $collectionFactory;
$this->collectionProcessor = $collectionProcessor;
$this->searchResultsFactory = $searchResultsFactory;
$this->fileSystem = $filesystem;
}
public function save(ExportEntityInterface $exportEntity): ExportEntityInterface
{
try {
$this->exportEntityResource->save($exportEntity);
$this->register($exportEntity);
} catch (Exception $e) {
throw new CouldNotSaveException(new Phrase('Could not save the entity.'), $e);
}
return $exportEntity;
}
public function getById(int $exportId): ExportEntityInterface
{
if (!isset($this->instances[$exportId])) {
/** @var ExportEntityInterface $exportEntity */
$exportEntity = $this->exportEntityFactory->create();
$this->exportEntityResource->load($exportEntity, $exportId, ExportEntityInterface::ID);
if (!$exportEntity->getExportId()) {
throw NoSuchEntityException::singleField(ExportEntityInterface::ID, $exportId);
}
$this->register($exportEntity);
}
return $this->instances[$exportId];
}
public function getByEntity(int $entityId, string $entityType): ExportEntityInterface
{
$key = $entityType . '_' . $entityId;
if (!isset($this->instancesByEntity[$key])) {
/** @var ExportEntityInterface $exportEntity */
$exportEntity = $this->exportEntityFactory->create();
$this->exportEntityResource->load(
$exportEntity,
[$entityId, $entityType],
[ExportEntityInterface::ENTITY_ID, ExportEntityInterface::ENTITY_TYPE]
);
if (!$exportEntity->getExportId()) {
throw NoSuchEntityException::doubleField(
ExportEntityInterface::ENTITY_ID,
$entityId,
ExportEntityInterface::ENTITY_TYPE,
$entityType
);
}
$this->register($exportEntity);
}
return $this->instancesByEntity[$key];
}
public function getList(SearchCriteriaInterface $searchCriteria): SearchResultsInterface
{
/** @var Collection $collection */
$collection = $this->collectionFactory->create();
$this->collectionProcessor->process($searchCriteria, $collection);
/** @var ExportEntitySearchResultsInterface $searchResults */
$searchResults = $this->searchResultsFactory->create();
$searchResults->setSearchCriteria($searchCriteria);
$searchResults->setItems($collection->getItems());
$searchResults->setTotalCount($collection->getSize());
return $searchResults;
}
public function delete(ExportEntityInterface $exportEntity): bool
{
try {
if ($exportEntity->getFilePath()) {
$this->fileSystem->getDirectoryWrite(DirectoryList::TMP)->delete($exportEntity->getFilePath());
}
$this->remove($exportEntity);
$this->exportEntityResource->delete($exportEntity);
} catch (Exception $e) {
throw new CouldNotDeleteException(
new Phrase('Could not delete entity with id "%1".', [$exportEntity->getExportId()]),
$e
);
}
return true;
}
private function register(ExportEntityInterface $exportEntity): void
{
$this->instances[$exportEntity->getExportId()] = $exportEntity;
$this->instancesByEntity[$exportEntity->getEntityType() . '_' . $exportEntity->getEntityId()] = $exportEntity;
}
private function remove(ExportEntityInterface $exportEntity): void
{
unset(
$this->instances[$exportEntity->getExportId()],
$this->instancesByEntity[$exportEntity->getEntityType() . '_' . $exportEntity->getEntityId()]
);
}
}