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