-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDocumentTypeRepository.php
170 lines (147 loc) · 6.01 KB
/
DocumentTypeRepository.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\Document\Model;
use Exception;
use Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface;
use Magento\Framework\Api\SearchCriteria\CollectionProcessor;
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\Document\Api\Data\DocumentTypeInterface;
use Opengento\Document\Api\Data\DocumentTypeInterfaceFactory;
use Opengento\Document\Api\Data\DocumentTypeSearchResultsInterface;
use Opengento\Document\Api\Data\DocumentTypeSearchResultsInterfaceFactory;
use Opengento\Document\Api\DocumentTypeRepositoryInterface;
use Opengento\Document\Model\Document\Filesystem\File;
use Opengento\Document\Model\ResourceModel\DocumentType as DocumentTypeDb;
use Opengento\Document\Model\ResourceModel\DocumentType\Collection;
use Opengento\Document\Model\ResourceModel\DocumentType\CollectionFactory;
class DocumentTypeRepository implements DocumentTypeRepositoryInterface
{
/**
* @var DocumentTypeInterfaceFactory
*/
private $documentTypeFactory;
/**
* @var DocumentTypeDb
*/
private $documentTypeDb;
/**
* @var CollectionFactory
*/
private $collectionFactory;
/**
* @var JoinProcessorInterface
*/
private $joinProcessor;
/**
* @var CollectionProcessor
*/
private $collectionProcessor;
/**
* @var DocumentTypeSearchResultsInterfaceFactory
*/
private $searchResultsFactory;
/**
* @var File
*/
private $fileHelper;
/**
* @var DocumentTypeInterface[]
*/
private $instances = ['code' => [], 'id' => []];
public function __construct(
DocumentTypeInterfaceFactory $documentTypeFactory,
DocumentTypeDb $documentTypeDb,
CollectionFactory $collectionFactory,
JoinProcessorInterface $joinProcessor,
CollectionProcessor $collectionProcessor,
DocumentTypeSearchResultsInterfaceFactory $searchResultsFactory,
File $fileHelper
) {
$this->documentTypeFactory = $documentTypeFactory;
$this->documentTypeDb = $documentTypeDb;
$this->collectionFactory = $collectionFactory;
$this->joinProcessor = $joinProcessor;
$this->collectionProcessor = $collectionProcessor;
$this->searchResultsFactory = $searchResultsFactory;
$this->fileHelper = $fileHelper;
}
public function getById(int $documentTypeId): DocumentTypeInterface
{
if (!isset($this->instances['id'][$documentTypeId])) {
/** @var DocumentTypeInterface|DocumentType $documentType */
$documentType = $this->documentTypeFactory->create();
$this->documentTypeDb->load($documentType, $documentTypeId);
if (!$documentType->getId()) {
throw new NoSuchEntityException(new Phrase('No such entity exists with id "%1".', [$documentTypeId]));
}
$this->instances['id'][$documentTypeId] = $documentType;
$this->instances['code'][$documentType->getCode()] = $documentType;
}
return $this->instances['id'][$documentTypeId];
}
public function getByCode(string $documentTypeCode): DocumentTypeInterface
{
if (!isset($this->instances['code'][$documentTypeCode])) {
/** @var DocumentTypeInterface|DocumentType $documentType */
$documentType = $this->documentTypeFactory->create();
$this->documentTypeDb->load($documentType, $documentTypeCode, 'code');
if (!$documentType->getId()) {
throw new NoSuchEntityException(
new Phrase('No such entity exists with code "%1".', [$documentTypeCode])
);
}
$this->instances['id'][$documentType->getId()] = $documentType;
$this->instances['code'][$documentTypeCode] = $documentType;
}
return $this->instances['code'][$documentTypeCode];
}
public function save(DocumentTypeInterface $documentType): DocumentTypeInterface
{
try {
/** @var DocumentType $documentType */
$this->documentTypeDb->save($documentType);
} catch (Exception $e) {
throw new CouldNotSaveException(
new Phrase('Could not save entity with id "%1".', [$documentType->getId()]),
$e
);
}
$this->instances['id'][$documentType->getId()] = $documentType;
$this->instances['code'][$documentType->getCode()] = $documentType;
return $documentType;
}
public function delete(DocumentTypeInterface $documentType): void
{
try {
/** @var DocumentType $documentType */
$this->documentTypeDb->delete($documentType);
$this->fileHelper->deleteFile($documentType->getDefaultImageFileName());
} catch (Exception $e) {
throw new CouldNotDeleteException(
new Phrase('Could not save entity with ID "%1".', [$documentType->getId()]), $e
);
}
unset($this->instances['id'][$documentType->getId()], $this->instances['code'][$documentType->getCode()]);
}
public function getList(SearchCriteriaInterface $searchCriteria): SearchResultsInterface
{
/** @var Collection $collection */
$collection = $this->collectionFactory->create();
$this->joinProcessor->process($collection);
$this->collectionProcessor->process($searchCriteria, $collection);
/** @var DocumentTypeSearchResultsInterface $searchResults */
$searchResults = $this->searchResultsFactory->create();
$searchResults->setItems($collection->getItems());
$searchResults->setTotalCount($collection->getSize());
return $searchResults;
}
}