1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Netgen\InformationCollection\Core\Service; |
6
|
|
|
|
7
|
|
|
use eZ\Publish\API\Repository\Exceptions\NotFoundException; |
8
|
|
|
use eZ\Publish\API\Repository\Repository; |
9
|
|
|
use eZ\Publish\API\Repository\Values\User\UserReference; |
10
|
|
|
use Netgen\InformationCollection\API\Exception\PersistingFailedException; |
11
|
|
|
use Netgen\InformationCollection\API\Exception\StoringAttributeFailedException; |
12
|
|
|
use Netgen\InformationCollection\API\Exception\StoringCollectionFailedException; |
13
|
|
|
use Netgen\InformationCollection\API\Service\InformationCollection; |
14
|
|
|
use Netgen\InformationCollection\API\Value\Attribute; |
15
|
|
|
use Netgen\InformationCollection\API\Value\Collection; |
16
|
|
|
use Netgen\InformationCollection\API\Value\CollectionCount; |
17
|
|
|
use Netgen\InformationCollection\API\Value\Collections; |
18
|
|
|
use Netgen\InformationCollection\API\Value\ContentsWithCollections; |
19
|
|
|
use Netgen\InformationCollection\API\Value\Filter\CollectionFields; |
20
|
|
|
use Netgen\InformationCollection\API\Value\Filter\CollectionId; |
21
|
|
|
use Netgen\InformationCollection\API\Value\Filter\Collections as FilterCollections; |
22
|
|
|
use Netgen\InformationCollection\API\Value\Filter\ContentId; |
23
|
|
|
use Netgen\InformationCollection\API\Value\Filter\Contents; |
24
|
|
|
use Netgen\InformationCollection\API\Value\Filter\Query; |
25
|
|
|
use Netgen\InformationCollection\API\Value\Filter\SearchCountQuery; |
26
|
|
|
use Netgen\InformationCollection\API\Value\Filter\SearchQuery; |
27
|
|
|
use Netgen\InformationCollection\API\Value\InformationCollectionStruct; |
28
|
|
|
use Netgen\InformationCollection\API\Value\ObjectCount; |
29
|
|
|
use Netgen\InformationCollection\API\Value\SearchCount; |
30
|
|
|
use Netgen\InformationCollection\Core\Factory\FieldDataFactory; |
31
|
|
|
use Netgen\InformationCollection\Core\Mapper\DomainObjectMapper; |
32
|
|
|
use Netgen\InformationCollection\Core\Persistence\Gateway\DoctrineDatabase; |
33
|
|
|
use Netgen\InformationCollection\Doctrine\Repository\EzInfoCollectionAttributeRepository; |
34
|
|
|
use Netgen\InformationCollection\Doctrine\Repository\EzInfoCollectionRepository; |
35
|
|
|
|
36
|
|
|
class InformationCollectionService implements InformationCollection |
37
|
|
|
{ |
38
|
|
|
/** |
39
|
|
|
* @var \Netgen\InformationCollection\Doctrine\Repository\EzInfoCollectionRepository |
40
|
|
|
*/ |
41
|
|
|
protected $ezInfoCollectionRepository; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var \Netgen\InformationCollection\Doctrine\Repository\EzInfoCollectionAttributeRepository |
45
|
|
|
*/ |
46
|
|
|
protected $ezInfoCollectionAttributeRepository; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var \eZ\Publish\API\Repository\Repository |
50
|
|
|
*/ |
51
|
|
|
protected $repository; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var \Netgen\InformationCollection\Core\Persistence\Gateway\DoctrineDatabase |
55
|
|
|
*/ |
56
|
|
|
protected $gateway; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var \Netgen\InformationCollection\Core\Factory\FieldDataFactory |
60
|
|
|
*/ |
61
|
|
|
protected $fieldsFactory; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @var \Netgen\InformationCollection\Core\Mapper\DomainObjectMapper |
65
|
|
|
*/ |
66
|
|
|
protected $objectMapper; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* InformationCollectionService constructor. |
70
|
|
|
* |
71
|
|
|
* @param \Netgen\InformationCollection\Doctrine\Repository\EzInfoCollectionRepository $ezInfoCollectionRepository |
72
|
|
|
* @param \Netgen\InformationCollection\Doctrine\Repository\EzInfoCollectionAttributeRepository $ezInfoCollectionAttributeRepository |
73
|
|
|
* @param \eZ\Publish\API\Repository\Repository $repository |
74
|
|
|
* @param \Netgen\InformationCollection\Core\Persistence\Gateway\DoctrineDatabase $gateway |
75
|
|
|
*/ |
76
|
|
|
public function __construct( |
77
|
|
|
EzInfoCollectionRepository $ezInfoCollectionRepository, |
78
|
|
|
EzInfoCollectionAttributeRepository $ezInfoCollectionAttributeRepository, |
79
|
|
|
Repository $repository, |
80
|
|
|
DoctrineDatabase $gateway, |
81
|
|
|
FieldDataFactory $factory, |
82
|
|
|
DomainObjectMapper $objectMapper |
83
|
|
|
) { |
84
|
|
|
$this->ezInfoCollectionRepository = $ezInfoCollectionRepository; |
85
|
|
|
$this->ezInfoCollectionAttributeRepository = $ezInfoCollectionAttributeRepository; |
86
|
|
|
$this->repository = $repository; |
87
|
|
|
$this->gateway = $gateway; |
88
|
|
|
$this->fieldsFactory = $factory; |
89
|
|
|
$this->objectMapper = $objectMapper; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function createCollection(InformationCollectionStruct $struct): void |
93
|
|
|
{ |
94
|
|
|
$contentType = $struct->getContentType(); |
95
|
|
|
$content = $struct->getContent(); |
96
|
|
|
|
97
|
|
|
$userReference = $this->repository |
98
|
|
|
->getPermissionResolver() |
99
|
|
|
->getCurrentUserReference(); |
100
|
|
|
|
101
|
|
|
$user = $this->getUser( |
102
|
|
|
$userReference->getUserId() |
103
|
|
|
); |
104
|
|
|
|
105
|
|
|
$ezInfo = $this->ezInfoCollectionRepository |
106
|
|
|
->createNewFromValues($content, $user); |
107
|
|
|
|
108
|
|
|
try { |
109
|
|
|
$this->ezInfoCollectionRepository->save($ezInfo); |
110
|
|
|
} catch (StoringCollectionFailedException $e) { |
111
|
|
|
throw new PersistingFailedException('collection', $e->getMessage()); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
foreach ($struct->getFieldsData() as $fieldDefIdentifier => $value) { |
115
|
|
|
if ($value->value === null) { |
116
|
|
|
continue; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
$value = $this->fieldsFactory->getLegacyValue($value->value, $contentType->getFieldDefinition($fieldDefIdentifier)); |
120
|
|
|
$ezInfoAttribute = $this->ezInfoCollectionAttributeRepository |
121
|
|
|
->createNewFromValues($content, $ezInfo, $value, $fieldDefIdentifier); |
122
|
|
|
|
123
|
|
|
try { |
124
|
|
|
$this->ezInfoCollectionAttributeRepository->save($ezInfoAttribute); |
125
|
|
|
} catch (StoringAttributeFailedException $e) { |
126
|
|
|
throw new PersistingFailedException('attribute', $e->getMessage()); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function getObjectsWithCollectionsCount(): ObjectCount |
132
|
|
|
{ |
133
|
|
|
return new ObjectCount( |
134
|
|
|
$this->gateway->getContentsWithCollectionsCount() |
135
|
|
|
); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* {@inheritdoc} |
140
|
|
|
*/ |
141
|
|
|
public function getObjectsWithCollections(Query $query): ContentsWithCollections |
142
|
|
|
{ |
143
|
|
|
$objects = $this->gateway->getObjectsWithCollections($query->getLimit(), $query->getOffset()); |
144
|
|
|
|
145
|
|
|
$contents = []; |
146
|
|
|
foreach ($objects as $object) { |
147
|
|
|
$contentId = (int) $object['content_id']; |
148
|
|
|
|
149
|
|
|
$childCount = $this->ezInfoCollectionRepository->getChildrenCount($contentId); |
150
|
|
|
|
151
|
|
|
$contents[] = $this->objectMapper |
152
|
|
|
->mapContent( |
153
|
|
|
$object, |
154
|
|
|
$this->ezInfoCollectionRepository->getFirstCollection($contentId), |
155
|
|
|
$this->ezInfoCollectionRepository->getLastCollection($contentId), |
156
|
|
|
$childCount |
157
|
|
|
); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
return new ContentsWithCollections($contents, count($contents)); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
public function getCollectionsCount(ContentId $contentId): CollectionCount |
164
|
|
|
{ |
165
|
|
|
return new CollectionCount( |
166
|
|
|
$this->ezInfoCollectionRepository->getChildrenCount($contentId->getContentId()) |
167
|
|
|
); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
public function getCollections(ContentId $contentId): Collections |
171
|
|
|
{ |
172
|
|
|
$collections = $this->ezInfoCollectionRepository->findBy( |
173
|
|
|
[ |
174
|
|
|
'contentObjectId' => $contentId->getContentId(), |
175
|
|
|
], |
176
|
|
|
[], |
177
|
|
|
$contentId->getLimit(), |
178
|
|
|
$contentId->getOffset() |
179
|
|
|
); |
180
|
|
|
|
181
|
|
|
$objects = []; |
182
|
|
|
foreach ($collections as $collection) { |
183
|
|
|
$objects[] = $this->loadCollection($collection->getId()); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
return new Collections($objects, count($objects)); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
public function searchCount(SearchCountQuery $query): SearchCount |
190
|
|
|
{ |
191
|
|
|
$collections = $this->ezInfoCollectionAttributeRepository |
192
|
|
|
->search($query->getContentId(), $query->getSearchText()); |
193
|
|
|
|
194
|
|
|
// needs rewrite |
195
|
|
|
$collections = $this->ezInfoCollectionRepository->findBy( |
196
|
|
|
[ |
197
|
|
|
'id' => $collections, |
198
|
|
|
] |
199
|
|
|
); |
200
|
|
|
|
201
|
|
|
return new SearchCount(count($collections)); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* {@inheritdoc} |
206
|
|
|
*/ |
207
|
|
|
public function search(SearchQuery $query): Collections |
208
|
|
|
{ |
209
|
|
|
$collections = $this->ezInfoCollectionAttributeRepository |
210
|
|
|
->search($query->getContentId(), $query->getSearchText()); |
211
|
|
|
|
212
|
|
|
$collections = $this->ezInfoCollectionRepository->findBy( |
213
|
|
|
[ |
214
|
|
|
'id' => $collections, |
215
|
|
|
], |
216
|
|
|
[], |
217
|
|
|
$query->getLimit(), |
218
|
|
|
$query->getOffset() |
219
|
|
|
); |
220
|
|
|
|
221
|
|
|
$objects = []; |
222
|
|
|
foreach ($collections as $collection) { |
223
|
|
|
$objects[] = $this->loadCollection($collection->getId()); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
return new Collections($objects, count($objects)); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* {@inheritdoc} |
231
|
|
|
*/ |
232
|
|
|
public function getCollection(CollectionId $collectionId): Collection |
233
|
|
|
{ |
234
|
|
|
return $this->loadCollection($collectionId->getCollectionId()); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* {@inheritdoc} |
239
|
|
|
*/ |
240
|
|
|
public function deleteCollectionFields(CollectionFields $collectionFields): void |
241
|
|
|
{ |
242
|
|
|
$attributes = $this->ezInfoCollectionAttributeRepository |
243
|
|
|
->findBy( |
244
|
|
|
[ |
245
|
|
|
'contentObjectId' => $collectionFields->getContentId(), |
246
|
|
|
'informationCollectionId' => $collectionFields->getCollectionId(), |
247
|
|
|
'contentClassAttributeId' => $collectionFields->getFields(), |
248
|
|
|
] |
249
|
|
|
); |
250
|
|
|
|
251
|
|
|
$this->ezInfoCollectionAttributeRepository->remove($attributes); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* {@inheritdoc} |
256
|
|
|
*/ |
257
|
|
View Code Duplication |
public function deleteCollections(FilterCollections $collections): void |
|
|
|
|
258
|
|
|
{ |
259
|
|
|
$collections = $this->ezInfoCollectionRepository |
260
|
|
|
->findBy([ |
261
|
|
|
'contentObjectId' => $collections->getContentId(), |
262
|
|
|
'id' => $collections->getCollectionIds(), |
263
|
|
|
]); |
264
|
|
|
|
265
|
|
|
foreach ($collections as $collection) { |
266
|
|
|
$attributes = $this->ezInfoCollectionAttributeRepository->findBy(['informationCollectionId' => $collection->getId()]); |
267
|
|
|
$this->ezInfoCollectionAttributeRepository->remove($attributes); |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
$this->ezInfoCollectionRepository->remove($collections); |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* {@inheritdoc} |
275
|
|
|
*/ |
276
|
|
View Code Duplication |
public function deleteCollectionByContent(Contents $contents): void |
|
|
|
|
277
|
|
|
{ |
278
|
|
|
$collections = $this->ezInfoCollectionRepository |
279
|
|
|
->findBy([ |
280
|
|
|
'contentObjectId' => $contents->getContentIds(), |
281
|
|
|
]); |
282
|
|
|
|
283
|
|
|
foreach ($collections as $collection) { |
284
|
|
|
$attributes = $this->ezInfoCollectionAttributeRepository->findBy(['informationCollectionId' => $collection->getId()]); |
285
|
|
|
$this->ezInfoCollectionAttributeRepository->remove($attributes); |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
$this->ezInfoCollectionRepository->remove($collections); |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* {@inheritdoc} |
293
|
|
|
*/ |
294
|
|
|
public function updateCollectionAttribute(CollectionId $collectionId, Attribute $attribute): void |
295
|
|
|
{ |
296
|
|
|
$this->ezInfoCollectionAttributeRepository->updateByCollectionId($collectionId, $attribute); |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
/** |
300
|
|
|
* @param int $userId |
301
|
|
|
* @param mixed $userId |
302
|
|
|
* |
303
|
|
|
* @return \eZ\Publish\API\Repository\Values\User\User |
304
|
|
|
*/ |
305
|
|
|
protected function getUser($userId) |
306
|
|
|
{ |
307
|
|
|
try { |
308
|
|
|
return $this->repository |
309
|
|
|
->getUserService() |
310
|
|
|
->loadUser($userId); |
311
|
|
|
} catch (NotFoundException $exception) { |
|
|
|
|
312
|
|
|
} |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
/** |
316
|
|
|
* @param int $collectionId |
317
|
|
|
* |
318
|
|
|
* @return \Netgen\InformationCollection\API\Value\Collection |
319
|
|
|
*/ |
320
|
|
|
protected function loadCollection($collectionId) |
321
|
|
|
{ |
322
|
|
|
$collection = $this->ezInfoCollectionRepository->loadCollection($collectionId); |
323
|
|
|
$attributes = $this->ezInfoCollectionAttributeRepository->findBy( |
324
|
|
|
[ |
325
|
|
|
'informationCollectionId' => $collectionId, |
326
|
|
|
] |
327
|
|
|
); |
328
|
|
|
|
329
|
|
|
return $this->objectMapper->mapCollection($collection, $attributes); |
|
|
|
|
330
|
|
|
} |
331
|
|
|
} |
332
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.