Completed
Push — ezp-30616 ( 3c2fb4...b64679 )
by
unknown
30:04 queued 14:26
created

ContentService::createContentDraft()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 27

Duplication

Lines 27
Ratio 100 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 3
dl 27
loc 27
rs 9.488
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
5
 * @license For full copyright and license information view LICENSE file distributed with this source code.
6
 */
7
declare(strict_types=1);
8
9
namespace eZ\Publish\Core\Event;
10
11
use eZ\Publish\API\Repository\Values\Content\Content;
12
use eZ\Publish\API\Repository\Values\Content\Relation;
13
use eZ\Publish\SPI\Repository\Decorator\ContentServiceDecorator;
14
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
15
use eZ\Publish\API\Repository\ContentService as ContentServiceInterface;
16
use eZ\Publish\API\Repository\Values\Content\ContentCreateStruct;
17
use eZ\Publish\API\Repository\Values\Content\ContentInfo;
18
use eZ\Publish\API\Repository\Values\Content\ContentMetadataUpdateStruct;
19
use eZ\Publish\API\Repository\Values\Content\ContentUpdateStruct;
20
use eZ\Publish\API\Repository\Values\Content\LocationCreateStruct;
21
use eZ\Publish\API\Repository\Values\Content\VersionInfo;
22
use eZ\Publish\API\Repository\Values\User\User;
23
use eZ\Publish\Core\Event\Content\AddRelationEvent;
24
use eZ\Publish\Core\Event\Content\BeforeAddRelationEvent;
25
use eZ\Publish\Core\Event\Content\BeforeCopyContentEvent;
26
use eZ\Publish\Core\Event\Content\BeforeCreateContentDraftEvent;
27
use eZ\Publish\Core\Event\Content\BeforeCreateContentEvent;
28
use eZ\Publish\Core\Event\Content\BeforeDeleteContentEvent;
29
use eZ\Publish\Core\Event\Content\BeforeDeleteRelationEvent;
30
use eZ\Publish\Core\Event\Content\BeforeDeleteTranslationEvent;
31
use eZ\Publish\Core\Event\Content\BeforeDeleteVersionEvent;
32
use eZ\Publish\Core\Event\Content\BeforeHideContentEvent;
33
use eZ\Publish\Core\Event\Content\BeforePublishVersionEvent;
34
use eZ\Publish\Core\Event\Content\BeforeRevealContentEvent;
35
use eZ\Publish\Core\Event\Content\BeforeUpdateContentEvent;
36
use eZ\Publish\Core\Event\Content\BeforeUpdateContentMetadataEvent;
37
use eZ\Publish\Core\Event\Content\ContentEvents;
38
use eZ\Publish\Core\Event\Content\CopyContentEvent;
39
use eZ\Publish\Core\Event\Content\CreateContentDraftEvent;
40
use eZ\Publish\Core\Event\Content\CreateContentEvent;
41
use eZ\Publish\Core\Event\Content\DeleteContentEvent;
42
use eZ\Publish\Core\Event\Content\DeleteRelationEvent;
43
use eZ\Publish\Core\Event\Content\DeleteTranslationEvent;
44
use eZ\Publish\Core\Event\Content\DeleteVersionEvent;
45
use eZ\Publish\Core\Event\Content\HideContentEvent;
46
use eZ\Publish\Core\Event\Content\PublishVersionEvent;
47
use eZ\Publish\Core\Event\Content\RevealContentEvent;
48
use eZ\Publish\Core\Event\Content\UpdateContentEvent;
49
use eZ\Publish\Core\Event\Content\UpdateContentMetadataEvent;
50
51
class ContentService extends ContentServiceDecorator implements ContentServiceInterface
52
{
53
    /**
54
     * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
55
     */
56
    protected $eventDispatcher;
57
58
    public function __construct(
59
        ContentServiceInterface $innerService,
60
        EventDispatcherInterface $eventDispatcher
61
    ) {
62
        parent::__construct($innerService);
63
64
        $this->eventDispatcher = $eventDispatcher;
65
    }
66
67 View Code Duplication
    public function createContent(
68
        ContentCreateStruct $contentCreateStruct,
69
        array $locationCreateStructs = []
70
    ): Content {
71
        $eventData = [
72
            $contentCreateStruct,
73
            $locationCreateStructs,
74
        ];
75
76
        $beforeEvent = new BeforeCreateContentEvent(...$eventData);
0 ignored issues
show
Bug introduced by
The call to BeforeCreateContentEvent::__construct() misses a required argument $locationCreateStructs.

This check looks for function calls that miss required arguments.

Loading history...
77
        if ($this->eventDispatcher->dispatch(ContentEvents::BEFORE_CREATE_CONTENT, $beforeEvent)->isPropagationStopped()) {
78
            return $beforeEvent->getContent();
79
        }
80
81
        $content = $beforeEvent->hasContent()
82
            ? $beforeEvent->getContent()
83
            : parent::createContent($contentCreateStruct, $locationCreateStructs);
84
85
        $this->eventDispatcher->dispatch(
86
            ContentEvents::CREATE_CONTENT,
87
            new CreateContentEvent($content, ...$eventData)
0 ignored issues
show
Bug introduced by
The call to CreateContentEvent::__construct() misses a required argument $locationCreateStructs.

This check looks for function calls that miss required arguments.

Loading history...
88
        );
89
90
        return $content;
91
    }
92
93 View Code Duplication
    public function updateContentMetadata(
94
        ContentInfo $contentInfo,
95
        ContentMetadataUpdateStruct $contentMetadataUpdateStruct
96
    ): Content {
97
        $eventData = [
98
            $contentInfo,
99
            $contentMetadataUpdateStruct,
100
        ];
101
102
        $beforeEvent = new BeforeUpdateContentMetadataEvent(...$eventData);
0 ignored issues
show
Bug introduced by
The call to BeforeUpdateContentMetadataEvent::__construct() misses a required argument $contentMetadataUpdateStruct.

This check looks for function calls that miss required arguments.

Loading history...
103
        if ($this->eventDispatcher->dispatch(ContentEvents::BEFORE_UPDATE_CONTENT_METADATA, $beforeEvent)->isPropagationStopped()) {
104
            return $beforeEvent->getContent();
105
        }
106
107
        $content = $beforeEvent->hasContent()
108
            ? $beforeEvent->getContent()
109
            : parent::updateContentMetadata($contentInfo, $contentMetadataUpdateStruct);
110
111
        $this->eventDispatcher->dispatch(
112
            ContentEvents::UPDATE_CONTENT_METADATA,
113
            new UpdateContentMetadataEvent($content, ...$eventData)
0 ignored issues
show
Bug introduced by
The call to UpdateContentMetadataEvent::__construct() misses a required argument $contentMetadataUpdateStruct.

This check looks for function calls that miss required arguments.

Loading history...
114
        );
115
116
        return $content;
117
    }
118
119 View Code Duplication
    public function deleteContent(ContentInfo $contentInfo): array
120
    {
121
        $eventData = [$contentInfo];
122
123
        $beforeEvent = new BeforeDeleteContentEvent(...$eventData);
124
        if ($this->eventDispatcher->dispatch(ContentEvents::BEFORE_DELETE_CONTENT, $beforeEvent)->isPropagationStopped()) {
125
            return $beforeEvent->getLocations();
126
        }
127
128
        $locations = $beforeEvent->hasLocations()
129
            ? $beforeEvent->getLocations()
130
            : parent::deleteContent($contentInfo);
131
132
        $this->eventDispatcher->dispatch(
133
            ContentEvents::DELETE_CONTENT,
134
            new DeleteContentEvent($locations, ...$eventData)
135
        );
136
137
        return $locations;
138
    }
139
140 View Code Duplication
    public function createContentDraft(
141
        ContentInfo $contentInfo,
142
        VersionInfo $versionInfo = null,
143
        User $creator = null
144
    ): Content {
145
        $eventData = [
146
            $contentInfo,
147
            $versionInfo,
148
            $creator,
149
        ];
150
151
        $beforeEvent = new BeforeCreateContentDraftEvent(...$eventData);
0 ignored issues
show
Bug introduced by
The call to BeforeCreateContentDraftEvent::__construct() misses some required arguments starting with $versionInfo.
Loading history...
152
        if ($this->eventDispatcher->dispatch(ContentEvents::BEFORE_CREATE_CONTENT_DRAFT, $beforeEvent)->isPropagationStopped()) {
153
            return $beforeEvent->getContentDraft();
154
        }
155
156
        $contentDraft = $beforeEvent->hasContentDraft()
157
            ? $beforeEvent->getContentDraft()
158
            : parent::createContentDraft($contentInfo, $versionInfo, $creator);
159
160
        $this->eventDispatcher->dispatch(
161
            ContentEvents::CREATE_CONTENT_DRAFT,
162
            new CreateContentDraftEvent($contentDraft, ...$eventData)
0 ignored issues
show
Bug introduced by
The call to CreateContentDraftEvent::__construct() misses some required arguments starting with $versionInfo.
Loading history...
163
        );
164
165
        return $contentDraft;
166
    }
167
168 View Code Duplication
    public function updateContent(
169
        VersionInfo $versionInfo,
170
        ContentUpdateStruct $contentUpdateStruct
171
    ): Content {
172
        $eventData = [
173
            $versionInfo,
174
            $contentUpdateStruct,
175
        ];
176
177
        $beforeEvent = new BeforeUpdateContentEvent(...$eventData);
0 ignored issues
show
Bug introduced by
The call to BeforeUpdateContentEvent::__construct() misses a required argument $contentUpdateStruct.

This check looks for function calls that miss required arguments.

Loading history...
178
        if ($this->eventDispatcher->dispatch(ContentEvents::BEFORE_UPDATE_CONTENT, $beforeEvent)->isPropagationStopped()) {
179
            return $beforeEvent->getContent();
180
        }
181
182
        $content = $beforeEvent->hasContent()
183
            ? $beforeEvent->getContent()
184
            : parent::updateContent($versionInfo, $contentUpdateStruct);
185
186
        $this->eventDispatcher->dispatch(
187
            ContentEvents::UPDATE_CONTENT,
188
            new UpdateContentEvent($content, ...$eventData)
0 ignored issues
show
Bug introduced by
The call to UpdateContentEvent::__construct() misses a required argument $contentUpdateStruct.

This check looks for function calls that miss required arguments.

Loading history...
189
        );
190
191
        return $content;
192
    }
193
194 View Code Duplication
    public function publishVersion(VersionInfo $versionInfo): Content
195
    {
196
        $eventData = [$versionInfo];
197
198
        $beforeEvent = new BeforePublishVersionEvent(...$eventData);
199
        if ($this->eventDispatcher->dispatch(ContentEvents::BEFORE_PUBLISH_VERSION, $beforeEvent)->isPropagationStopped()) {
200
            return $beforeEvent->getContent();
201
        }
202
203
        $content = $beforeEvent->hasContent()
204
            ? $beforeEvent->getContent()
205
            : parent::publishVersion($versionInfo);
206
207
        $this->eventDispatcher->dispatch(
208
            ContentEvents::PUBLISH_VERSION,
209
            new PublishVersionEvent($content, ...$eventData)
210
        );
211
212
        return $content;
213
    }
214
215
    public function deleteVersion(VersionInfo $versionInfo): void
216
    {
217
        $eventData = [$versionInfo];
218
219
        $beforeEvent = new BeforeDeleteVersionEvent(...$eventData);
220
        if ($this->eventDispatcher->dispatch(ContentEvents::BEFORE_DELETE_VERSION, $beforeEvent)->isPropagationStopped()) {
221
            return;
222
        }
223
224
        parent::deleteVersion($versionInfo);
225
226
        $this->eventDispatcher->dispatch(
227
            ContentEvents::DELETE_VERSION,
228
            new DeleteVersionEvent(...$eventData)
229
        );
230
    }
231
232 View Code Duplication
    public function copyContent(
233
        ContentInfo $contentInfo,
234
        LocationCreateStruct $destinationLocationCreateStruct,
235
        VersionInfo $versionInfo = null
236
    ): Content {
237
        $eventData = [
238
            $contentInfo,
239
            $destinationLocationCreateStruct,
240
            $versionInfo,
241
        ];
242
243
        $beforeEvent = new BeforeCopyContentEvent(...$eventData);
0 ignored issues
show
Bug introduced by
The call to BeforeCopyContentEvent::__construct() misses some required arguments starting with $destinationLocationCreateStruct.
Loading history...
244
        if ($this->eventDispatcher->dispatch(ContentEvents::BEFORE_COPY_CONTENT, $beforeEvent)->isPropagationStopped()) {
245
            return $beforeEvent->getContent();
246
        }
247
248
        $content = $beforeEvent->hasContent()
249
            ? $beforeEvent->getContent()
250
            : parent::copyContent($contentInfo, $destinationLocationCreateStruct, $versionInfo);
251
252
        $this->eventDispatcher->dispatch(
253
            ContentEvents::COPY_CONTENT,
254
            new CopyContentEvent($content, ...$eventData)
0 ignored issues
show
Bug introduced by
The call to CopyContentEvent::__construct() misses some required arguments starting with $destinationLocationCreateStruct.
Loading history...
255
        );
256
257
        return $content;
258
    }
259
260 View Code Duplication
    public function addRelation(
261
        VersionInfo $sourceVersion,
262
        ContentInfo $destinationContent
263
    ): Relation {
264
        $eventData = [
265
            $sourceVersion,
266
            $destinationContent,
267
        ];
268
269
        $beforeEvent = new BeforeAddRelationEvent(...$eventData);
0 ignored issues
show
Bug introduced by
The call to BeforeAddRelationEvent::__construct() misses a required argument $destinationContent.

This check looks for function calls that miss required arguments.

Loading history...
270
        if ($this->eventDispatcher->dispatch(ContentEvents::BEFORE_ADD_RELATION, $beforeEvent)->isPropagationStopped()) {
271
            return $beforeEvent->getRelation();
272
        }
273
274
        $relation = $beforeEvent->hasRelation()
275
            ? $beforeEvent->getRelation()
276
            : parent::addRelation($sourceVersion, $destinationContent);
277
278
        $this->eventDispatcher->dispatch(
279
            ContentEvents::ADD_RELATION,
280
            new AddRelationEvent($relation, ...$eventData)
0 ignored issues
show
Bug introduced by
The call to AddRelationEvent::__construct() misses a required argument $destinationContent.

This check looks for function calls that miss required arguments.

Loading history...
281
        );
282
283
        return $relation;
284
    }
285
286 View Code Duplication
    public function deleteRelation(
287
        VersionInfo $sourceVersion,
288
        ContentInfo $destinationContent
289
    ): void {
290
        $eventData = [
291
            $sourceVersion,
292
            $destinationContent,
293
        ];
294
295
        $beforeEvent = new BeforeDeleteRelationEvent(...$eventData);
0 ignored issues
show
Bug introduced by
The call to BeforeDeleteRelationEvent::__construct() misses a required argument $destinationContent.

This check looks for function calls that miss required arguments.

Loading history...
296
        if ($this->eventDispatcher->dispatch(ContentEvents::BEFORE_DELETE_RELATION, $beforeEvent)->isPropagationStopped()) {
297
            return;
298
        }
299
300
        parent::deleteRelation($sourceVersion, $destinationContent);
301
302
        $this->eventDispatcher->dispatch(
303
            ContentEvents::DELETE_RELATION,
304
            new DeleteRelationEvent(...$eventData)
0 ignored issues
show
Bug introduced by
The call to DeleteRelationEvent::__construct() misses a required argument $destinationContent.

This check looks for function calls that miss required arguments.

Loading history...
305
        );
306
    }
307
308 View Code Duplication
    public function deleteTranslation(
309
        ContentInfo $contentInfo,
310
        $languageCode
311
    ): void {
312
        $eventData = [
313
            $contentInfo,
314
            $languageCode,
315
        ];
316
317
        $beforeEvent = new BeforeDeleteTranslationEvent(...$eventData);
0 ignored issues
show
Bug introduced by
The call to BeforeDeleteTranslationEvent::__construct() misses a required argument $languageCode.

This check looks for function calls that miss required arguments.

Loading history...
318
        if ($this->eventDispatcher->dispatch(ContentEvents::BEFORE_DELETE_TRANSLATION, $beforeEvent)->isPropagationStopped()) {
319
            return;
320
        }
321
322
        parent::deleteTranslation($contentInfo, $languageCode);
323
324
        $this->eventDispatcher->dispatch(
325
            ContentEvents::DELETE_TRANSLATION,
326
            new DeleteTranslationEvent(...$eventData)
0 ignored issues
show
Bug introduced by
The call to DeleteTranslationEvent::__construct() misses a required argument $languageCode.

This check looks for function calls that miss required arguments.

Loading history...
327
        );
328
    }
329
330
    public function hideContent(ContentInfo $contentInfo): void
331
    {
332
        $eventData = [$contentInfo];
333
334
        $beforeEvent = new BeforeHideContentEvent(...$eventData);
335
        if ($this->eventDispatcher->dispatch(ContentEvents::BEFORE_HIDE_CONTENT, $beforeEvent)->isPropagationStopped()) {
336
            return;
337
        }
338
339
        parent::hideContent($contentInfo);
340
341
        $this->eventDispatcher->dispatch(
342
            ContentEvents::HIDE_CONTENT,
343
            new HideContentEvent(...$eventData)
344
        );
345
    }
346
347
    public function revealContent(ContentInfo $contentInfo): void
348
    {
349
        $eventData = [$contentInfo];
350
351
        $beforeEvent = new BeforeRevealContentEvent(...$eventData);
352
        if ($this->eventDispatcher->dispatch(ContentEvents::BEFORE_REVEAL_CONTENT, $beforeEvent)->isPropagationStopped()) {
353
            return;
354
        }
355
356
        parent::revealContent($contentInfo);
357
358
        $this->eventDispatcher->dispatch(
359
            ContentEvents::REVEAL_CONTENT,
360
            new RevealContentEvent(...$eventData)
361
        );
362
    }
363
}
364