1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Transfer. |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE file located |
7
|
|
|
* in the root directory. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Transfer\EzPlatform\Repository\Manager; |
11
|
|
|
|
12
|
|
|
use eZ\Publish\API\Repository\ContentService; |
13
|
|
|
use eZ\Publish\API\Repository\ContentTypeService; |
14
|
|
|
use eZ\Publish\API\Repository\Exceptions\NotFoundException; |
15
|
|
|
use eZ\Publish\API\Repository\LocationService; |
16
|
|
|
use eZ\Publish\API\Repository\Repository; |
17
|
|
|
use eZ\Publish\API\Repository\Values\Content\Content; |
18
|
|
|
use eZ\Publish\API\Repository\Values\Content\Location; |
19
|
|
|
use Psr\Log\LoggerAwareInterface; |
20
|
|
|
use Psr\Log\LoggerInterface; |
21
|
|
|
use Transfer\Data\ObjectInterface; |
22
|
|
|
use Transfer\Data\ValueObject; |
23
|
|
|
use Transfer\EzPlatform\Exception\ObjectNotFoundException; |
24
|
|
|
use Transfer\EzPlatform\Repository\Values\ContentObject; |
25
|
|
|
use Transfer\EzPlatform\Repository\Values\LocationObject; |
26
|
|
|
use Transfer\EzPlatform\Exception\UnsupportedObjectOperationException; |
27
|
|
|
use Transfer\EzPlatform\Repository\Manager\Type\CreatorInterface; |
28
|
|
|
use Transfer\EzPlatform\Repository\Manager\Type\FinderInterface; |
29
|
|
|
use Transfer\EzPlatform\Repository\Manager\Type\RemoverInterface; |
30
|
|
|
use Transfer\EzPlatform\Repository\Manager\Type\UpdaterInterface; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Content manager. |
34
|
|
|
* |
35
|
|
|
* @internal |
36
|
|
|
*/ |
37
|
|
|
class ContentManager implements LoggerAwareInterface, CreatorInterface, UpdaterInterface, RemoverInterface, FinderInterface |
38
|
|
|
{ |
39
|
|
|
/** |
40
|
|
|
* @var LocationManager |
41
|
|
|
*/ |
42
|
|
|
private $locationManager; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var ContentService |
46
|
|
|
*/ |
47
|
|
|
protected $contentService; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var ContentTypeService |
51
|
|
|
*/ |
52
|
|
|
protected $contentTypeService; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var LocationService |
56
|
|
|
*/ |
57
|
|
|
protected $locationService; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var LoggerInterface |
61
|
|
|
*/ |
62
|
|
|
protected $logger; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param Repository $repository |
66
|
|
|
* @param LocationManager $locationManager |
67
|
|
|
*/ |
68
|
11 |
|
public function __construct(Repository $repository, LocationManager $locationManager) |
69
|
|
|
{ |
70
|
11 |
|
$this->locationManager = $locationManager; |
71
|
11 |
|
$this->contentService = $repository->getContentService(); |
72
|
11 |
|
$this->contentTypeService = $repository->getContentTypeService(); |
73
|
11 |
|
$this->locationService = $repository->getLocationService(); |
74
|
11 |
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* {@inheritdoc} |
78
|
|
|
*/ |
79
|
30 |
|
public function setLogger(LoggerInterface $logger) |
80
|
|
|
{ |
81
|
30 |
|
$this->logger = $logger; |
82
|
30 |
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* {@inheritdoc} |
86
|
|
|
*/ |
87
|
23 |
|
public function find(ValueObject $object) |
88
|
|
|
{ |
89
|
|
|
try { |
90
|
23 |
|
if ($object->getProperty('remote_id')) { |
91
|
23 |
|
$content = $this->contentService->loadContentByRemoteId($object->getProperty('remote_id')); |
92
|
17 |
|
} |
93
|
23 |
|
} catch (NotFoundException $notFoundException) { |
94
|
|
|
// We'll throw our own exception later instead. |
95
|
|
|
} |
96
|
|
|
|
97
|
23 |
|
if (!isset($content)) { |
98
|
10 |
|
throw new ObjectNotFoundException(Content::class, array('remote_id')); |
99
|
|
|
} |
100
|
|
|
|
101
|
17 |
|
return $content; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* {@inheritdoc} |
106
|
|
|
*/ |
107
|
9 |
|
public function create(ObjectInterface $object) |
108
|
|
|
{ |
109
|
9 |
|
if (!$object instanceof ContentObject) { |
110
|
1 |
|
throw new UnsupportedObjectOperationException(ContentObject::class, get_class($object)); |
111
|
|
|
} |
112
|
|
|
|
113
|
8 |
|
$createStruct = $this->contentService->newContentCreateStruct( |
114
|
8 |
|
$this->contentTypeService->loadContentTypeByIdentifier($object->getProperty('content_type_identifier')), |
115
|
8 |
|
$object->getProperty('language') |
116
|
8 |
|
); |
117
|
|
|
|
118
|
8 |
|
$object->getMapper()->mapObjectToCreateStruct($object, $createStruct); |
119
|
|
|
|
120
|
|
|
/** @var LocationObject[] $locationObjects */ |
121
|
8 |
|
$locationObjects = $object->getProperty('parent_locations'); |
122
|
8 |
|
$locationCreateStructs = []; |
123
|
8 |
|
if (is_array($locationObjects) && count($locationObjects) > 0) { |
124
|
1 |
|
foreach ($locationObjects as $locationObject) { |
125
|
1 |
|
$locationCreateStruct = $this->locationService->newLocationCreateStruct($locationObject->data['parent_location_id']); |
126
|
1 |
|
$locationObject->getMapper()->getNewLocationCreateStruct($locationCreateStruct); |
127
|
1 |
|
$locationCreateStructs[] = $locationCreateStruct; |
128
|
1 |
|
} |
129
|
1 |
|
} |
130
|
|
|
|
131
|
8 |
|
$content = $this->contentService->createContent($createStruct, $locationCreateStructs); |
132
|
8 |
|
$this->contentService->publishVersion($content->versionInfo); |
133
|
|
|
|
134
|
8 |
|
if ($this->logger) { |
135
|
8 |
|
$this->logger->info(sprintf('Published new version of %s', $object->getProperty('name')), array('ContentManager::create')); |
136
|
8 |
|
} |
137
|
|
|
|
138
|
8 |
|
$object->setProperty('id', $content->contentInfo->id); |
139
|
8 |
|
$object->setProperty('version_info', $content->versionInfo); |
140
|
8 |
|
$object->setProperty('content_info', $content->contentInfo); |
141
|
|
|
|
142
|
8 |
|
return $object; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* {@inheritdoc} |
147
|
|
|
*/ |
148
|
20 |
|
public function update(ObjectInterface $object) |
149
|
|
|
{ |
150
|
17 |
|
if (!$object instanceof ContentObject) { |
151
|
1 |
|
throw new UnsupportedObjectOperationException(ContentObject::class, get_class($object)); |
152
|
|
|
} |
153
|
|
|
|
154
|
16 |
|
$existingContent = $this->find($object); |
155
|
16 |
|
if (null === $object->getProperty('content_info')) { |
156
|
14 |
|
$object->setProperty('content_info', $existingContent->contentInfo); |
157
|
14 |
|
} |
158
|
|
|
|
159
|
16 |
|
$contentDraft = $this->contentService->createContentDraft($object->getProperty('content_info')); |
160
|
|
|
|
161
|
16 |
|
$contentUpdateStruct = $this->contentService->newContentUpdateStruct(); |
162
|
16 |
|
$object->getMapper()->mapObjectToUpdateStruct($object, $contentUpdateStruct); |
163
|
|
|
|
164
|
17 |
|
$contentDraft = $this->contentService->updateContent($contentDraft->versionInfo, $contentUpdateStruct); |
165
|
16 |
|
$content = $this->contentService->publishVersion($contentDraft->versionInfo); |
166
|
|
|
|
167
|
16 |
|
if ($this->logger) { |
168
|
16 |
|
$this->logger->info(sprintf('Published new version of %s', $object->getProperty('name')), array('ContentManager::update')); |
169
|
16 |
|
} |
170
|
|
|
|
171
|
16 |
|
$object->setProperty('id', $content->contentInfo->id); |
172
|
16 |
|
$object->setProperty('version_info', $content->versionInfo); |
173
|
16 |
|
$object->setProperty('content_info', $content->contentInfo); |
174
|
|
|
|
175
|
|
|
// Add/Update/Delete parent locations |
176
|
16 |
|
$this->locationManager->syncronizeLocationsFromContentObject($object); |
177
|
|
|
|
178
|
16 |
|
return $object; |
179
|
20 |
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* {@inheritdoc} |
183
|
|
|
*/ |
184
|
21 |
View Code Duplication |
public function createOrUpdate(ObjectInterface $object) |
|
|
|
|
185
|
|
|
{ |
186
|
21 |
|
if (!$object instanceof ContentObject) { |
187
|
1 |
|
throw new UnsupportedObjectOperationException(ContentObject::class, get_class($object)); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
try { |
191
|
20 |
|
$this->find($object); |
192
|
|
|
|
193
|
16 |
|
return $this->update($object); |
194
|
8 |
|
} catch (NotFoundException $notFound) { |
195
|
8 |
|
return $this->create($object); |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* {@inheritdoc} |
201
|
|
|
*/ |
202
|
3 |
View Code Duplication |
public function remove(ObjectInterface $object) |
|
|
|
|
203
|
|
|
{ |
204
|
3 |
|
if (!$object instanceof ContentObject) { |
205
|
1 |
|
throw new UnsupportedObjectOperationException(ContentObject::class, get_class($object)); |
206
|
|
|
} |
207
|
|
|
|
208
|
3 |
|
try { |
209
|
2 |
|
$content = $this->find($object); |
210
|
1 |
|
$this->contentService->deleteContent($content->contentInfo); |
211
|
|
|
|
212
|
1 |
|
return true; |
213
|
1 |
|
} catch (NotFoundException $notFound) { |
214
|
1 |
|
return false; |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Assigns a main location ID for a content object. |
220
|
|
|
* |
221
|
|
|
* @param ContentObject $object Content object |
222
|
|
|
* @param Location $location Location |
223
|
|
|
* |
224
|
|
|
* @return Content |
225
|
|
|
*/ |
226
|
3 |
|
public function setMainLocation(ContentObject $object, Location $location) |
227
|
|
|
{ |
228
|
3 |
|
$contentMetadataUpdateStruct = $this->contentService->newContentMetadataUpdateStruct(); |
229
|
|
|
|
230
|
3 |
|
$contentMetadataUpdateStruct->mainLocationId = $location->id; |
231
|
|
|
|
232
|
3 |
|
$object->setProperty('main_location_id', $location->id); |
233
|
|
|
|
234
|
3 |
|
return $this->contentService->updateContentMetadata($object->getProperty('content_info'), $contentMetadataUpdateStruct); |
235
|
|
|
} |
236
|
|
|
} |
237
|
|
|
|
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.