Completed
Push — ezp-30616-follow-up ( 2e0607...b74676 )
by
unknown
35:08 queued 20:12
created

ContentService::addRelation()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 2
dl 0
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\ContentService as ContentServiceInterface;
12
use eZ\Publish\API\Repository\Events\Content\AddRelationEvent as AddRelationEventInterface;
13
use eZ\Publish\API\Repository\Events\Content\BeforeAddRelationEvent as BeforeAddRelationEventInterface;
14
use eZ\Publish\API\Repository\Events\Content\BeforeCopyContentEvent as BeforeCopyContentEventInterface;
15
use eZ\Publish\API\Repository\Events\Content\BeforeCreateContentDraftEvent as BeforeCreateContentDraftEventInterface;
16
use eZ\Publish\API\Repository\Events\Content\BeforeCreateContentEvent as BeforeCreateContentEventInterface;
17
use eZ\Publish\API\Repository\Events\Content\BeforeDeleteContentEvent as BeforeDeleteContentEventInterface;
18
use eZ\Publish\API\Repository\Events\Content\BeforeDeleteRelationEvent as BeforeDeleteRelationEventInterface;
19
use eZ\Publish\API\Repository\Events\Content\BeforeDeleteTranslationEvent as BeforeDeleteTranslationEventInterface;
20
use eZ\Publish\API\Repository\Events\Content\BeforeDeleteVersionEvent as BeforeDeleteVersionEventInterface;
21
use eZ\Publish\API\Repository\Events\Content\BeforeHideContentEvent as BeforeHideContentEventInterface;
22
use eZ\Publish\API\Repository\Events\Content\BeforePublishVersionEvent as BeforePublishVersionEventInterface;
23
use eZ\Publish\API\Repository\Events\Content\BeforeRevealContentEvent as BeforeRevealContentEventInterface;
24
use eZ\Publish\API\Repository\Events\Content\BeforeUpdateContentEvent as BeforeUpdateContentEventInterface;
25
use eZ\Publish\API\Repository\Events\Content\BeforeUpdateContentMetadataEvent as BeforeUpdateContentMetadataEventInterface;
26
use eZ\Publish\API\Repository\Events\Content\CopyContentEvent as CopyContentEventInterface;
27
use eZ\Publish\API\Repository\Events\Content\CreateContentDraftEvent as CreateContentDraftEventInterface;
28
use eZ\Publish\API\Repository\Events\Content\CreateContentEvent as CreateContentEventInterface;
29
use eZ\Publish\API\Repository\Events\Content\DeleteContentEvent as DeleteContentEventInterface;
30
use eZ\Publish\API\Repository\Events\Content\DeleteRelationEvent as DeleteRelationEventInterface;
31
use eZ\Publish\API\Repository\Events\Content\DeleteTranslationEvent as DeleteTranslationEventInterface;
32
use eZ\Publish\API\Repository\Events\Content\DeleteVersionEvent as DeleteVersionEventInterface;
33
use eZ\Publish\API\Repository\Events\Content\HideContentEvent as HideContentEventInterface;
34
use eZ\Publish\API\Repository\Events\Content\PublishVersionEvent as PublishVersionEventInterface;
35
use eZ\Publish\API\Repository\Events\Content\RevealContentEvent as RevealContentEventInterface;
36
use eZ\Publish\API\Repository\Events\Content\UpdateContentEvent as UpdateContentEventInterface;
37
use eZ\Publish\API\Repository\Events\Content\UpdateContentMetadataEvent as UpdateContentMetadataEventInterface;
38
use eZ\Publish\API\Repository\Values\Content\Content;
39
use eZ\Publish\API\Repository\Values\Content\ContentCreateStruct;
40
use eZ\Publish\API\Repository\Values\Content\ContentInfo;
41
use eZ\Publish\API\Repository\Values\Content\ContentMetadataUpdateStruct;
42
use eZ\Publish\API\Repository\Values\Content\ContentUpdateStruct;
43
use eZ\Publish\API\Repository\Values\Content\Language;
44
use eZ\Publish\API\Repository\Values\Content\LocationCreateStruct;
45
use eZ\Publish\API\Repository\Values\Content\Relation;
46
use eZ\Publish\API\Repository\Values\Content\VersionInfo;
47
use eZ\Publish\API\Repository\Values\User\User;
48
use eZ\Publish\API\Repository\Events\Content\AddRelationEvent;
49
use eZ\Publish\API\Repository\Events\Content\BeforeAddRelationEvent;
50
use eZ\Publish\API\Repository\Events\Content\BeforeCopyContentEvent;
51
use eZ\Publish\API\Repository\Events\Content\BeforeCreateContentDraftEvent;
52
use eZ\Publish\API\Repository\Events\Content\BeforeCreateContentEvent;
53
use eZ\Publish\API\Repository\Events\Content\BeforeDeleteContentEvent;
54
use eZ\Publish\API\Repository\Events\Content\BeforeDeleteRelationEvent;
55
use eZ\Publish\API\Repository\Events\Content\BeforeDeleteTranslationEvent;
56
use eZ\Publish\API\Repository\Events\Content\BeforeDeleteVersionEvent;
57
use eZ\Publish\API\Repository\Events\Content\BeforeHideContentEvent;
58
use eZ\Publish\API\Repository\Events\Content\BeforePublishVersionEvent;
59
use eZ\Publish\API\Repository\Events\Content\BeforeRevealContentEvent;
60
use eZ\Publish\API\Repository\Events\Content\BeforeUpdateContentEvent;
61
use eZ\Publish\API\Repository\Events\Content\BeforeUpdateContentMetadataEvent;
62
use eZ\Publish\API\Repository\Events\Content\CopyContentEvent;
63
use eZ\Publish\API\Repository\Events\Content\CreateContentDraftEvent;
64
use eZ\Publish\API\Repository\Events\Content\CreateContentEvent;
65
use eZ\Publish\API\Repository\Events\Content\DeleteContentEvent;
66
use eZ\Publish\API\Repository\Events\Content\DeleteRelationEvent;
67
use eZ\Publish\API\Repository\Events\Content\DeleteTranslationEvent;
68
use eZ\Publish\API\Repository\Events\Content\DeleteVersionEvent;
69
use eZ\Publish\API\Repository\Events\Content\HideContentEvent;
70
use eZ\Publish\API\Repository\Events\Content\PublishVersionEvent;
71
use eZ\Publish\API\Repository\Events\Content\RevealContentEvent;
72
use eZ\Publish\API\Repository\Events\Content\UpdateContentEvent;
73
use eZ\Publish\API\Repository\Events\Content\UpdateContentMetadataEvent;
74
use eZ\Publish\SPI\Repository\Decorator\ContentServiceDecorator;
75
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
76
77
class ContentService extends ContentServiceDecorator
78
{
79
    /** @var \Symfony\Contracts\EventDispatcher\EventDispatcherInterface */
80
    protected $eventDispatcher;
81
82
    public function __construct(
83
        ContentServiceInterface $innerService,
84
        EventDispatcherInterface $eventDispatcher
85
    ) {
86
        parent::__construct($innerService);
87
88
        $this->eventDispatcher = $eventDispatcher;
89
    }
90
91
    public function createContent(
92
        ContentCreateStruct $contentCreateStruct,
93
        array $locationCreateStructs = []
94
    ): Content {
95
        $eventData = [
96
            $contentCreateStruct,
97
            $locationCreateStructs,
98
        ];
99
100
        $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...
101
102
        $this->eventDispatcher->dispatch($beforeEvent, BeforeCreateContentEventInterface::class);
103
        if ($beforeEvent->isPropagationStopped()) {
104
            return $beforeEvent->getContent();
105
        }
106
107
        $content = $beforeEvent->hasContent()
108
            ? $beforeEvent->getContent()
109
            : $this->innerService->createContent($contentCreateStruct, $locationCreateStructs);
110
111
        $this->eventDispatcher->dispatch(
112
            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...
113
            CreateContentEventInterface::class
114
        );
115
116
        return $content;
117
    }
118
119
    public function updateContentMetadata(
120
        ContentInfo $contentInfo,
121
        ContentMetadataUpdateStruct $contentMetadataUpdateStruct
122
    ): Content {
123
        $eventData = [
124
            $contentInfo,
125
            $contentMetadataUpdateStruct,
126
        ];
127
128
        $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...
129
130
        $this->eventDispatcher->dispatch($beforeEvent, BeforeUpdateContentMetadataEventInterface::class);
131
        if ($beforeEvent->isPropagationStopped()) {
132
            return $beforeEvent->getContent();
133
        }
134
135
        $content = $beforeEvent->hasContent()
136
            ? $beforeEvent->getContent()
137
            : $this->innerService->updateContentMetadata($contentInfo, $contentMetadataUpdateStruct);
138
139
        $this->eventDispatcher->dispatch(
140
            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...
141
            UpdateContentMetadataEventInterface::class
142
        );
143
144
        return $content;
145
    }
146
147
    public function deleteContent(ContentInfo $contentInfo): array
148
    {
149
        $eventData = [$contentInfo];
150
151
        $beforeEvent = new BeforeDeleteContentEvent(...$eventData);
152
153
        $this->eventDispatcher->dispatch($beforeEvent, BeforeDeleteContentEventInterface::class);
154
        if ($beforeEvent->isPropagationStopped()) {
155
            return $beforeEvent->getLocations();
156
        }
157
158
        $locations = $beforeEvent->hasLocations()
159
            ? $beforeEvent->getLocations()
160
            : $this->innerService->deleteContent($contentInfo);
161
162
        $this->eventDispatcher->dispatch(
163
            new DeleteContentEvent($locations, ...$eventData),
164
            DeleteContentEventInterface::class
165
        );
166
167
        return $locations;
168
    }
169
170 View Code Duplication
    public function createContentDraft(
171
        ContentInfo $contentInfo,
172
        VersionInfo $versionInfo = null,
173
        User $creator = null
174
    ): Content {
175
        $eventData = [
176
            $contentInfo,
177
            $versionInfo,
178
            $creator,
179
        ];
180
181
        $beforeEvent = new BeforeCreateContentDraftEvent(...$eventData);
182
183
        $this->eventDispatcher->dispatch($beforeEvent, BeforeCreateContentDraftEventInterface::class);
184
        if ($beforeEvent->isPropagationStopped()) {
185
            return $beforeEvent->getContentDraft();
186
        }
187
188
        $contentDraft = $beforeEvent->hasContentDraft()
189
            ? $beforeEvent->getContentDraft()
190
            : $this->innerService->createContentDraft($contentInfo, $versionInfo, $creator);
191
192
        $this->eventDispatcher->dispatch(
193
            new CreateContentDraftEvent($contentDraft, ...$eventData),
194
            CreateContentDraftEventInterface::class
195
        );
196
197
        return $contentDraft;
198
    }
199
200
    public function updateContent(
201
        VersionInfo $versionInfo,
202
        ContentUpdateStruct $contentUpdateStruct
203
    ): Content {
204
        $eventData = [
205
            $versionInfo,
206
            $contentUpdateStruct,
207
        ];
208
209
        $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...
210
211
        $this->eventDispatcher->dispatch($beforeEvent, BeforeUpdateContentEventInterface::class);
212
        if ($beforeEvent->isPropagationStopped()) {
213
            return $beforeEvent->getContent();
214
        }
215
216
        $content = $beforeEvent->hasContent()
217
            ? $beforeEvent->getContent()
218
            : $this->innerService->updateContent($versionInfo, $contentUpdateStruct);
219
220
        $this->eventDispatcher->dispatch(
221
            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...
222
            UpdateContentEventInterface::class
223
        );
224
225
        return $content;
226
    }
227
228
    public function publishVersion(VersionInfo $versionInfo, array $translations = Language::ALL): Content
229
    {
230
        $eventData = [
231
            $versionInfo,
232
            $translations,
233
        ];
234
235
        $beforeEvent = new BeforePublishVersionEvent(...$eventData);
0 ignored issues
show
Bug introduced by
The call to BeforePublishVersionEvent::__construct() misses a required argument $translations.

This check looks for function calls that miss required arguments.

Loading history...
236
237
        $this->eventDispatcher->dispatch($beforeEvent, BeforePublishVersionEventInterface::class);
238
        if ($beforeEvent->isPropagationStopped()) {
239
            return $beforeEvent->getContent();
240
        }
241
242
        $content = $beforeEvent->hasContent()
243
            ? $beforeEvent->getContent()
244
            : $this->innerService->publishVersion($versionInfo, $translations);
245
246
        $this->eventDispatcher->dispatch(
247
            new PublishVersionEvent($content, ...$eventData),
0 ignored issues
show
Bug introduced by
The call to PublishVersionEvent::__construct() misses a required argument $translations.

This check looks for function calls that miss required arguments.

Loading history...
248
            PublishVersionEventInterface::class
249
        );
250
251
        return $content;
252
    }
253
254
    public function deleteVersion(VersionInfo $versionInfo): void
255
    {
256
        $eventData = [$versionInfo];
257
258
        $beforeEvent = new BeforeDeleteVersionEvent(...$eventData);
259
260
        $this->eventDispatcher->dispatch($beforeEvent, BeforeDeleteVersionEventInterface::class);
261
        if ($beforeEvent->isPropagationStopped()) {
262
            return;
263
        }
264
265
        $this->innerService->deleteVersion($versionInfo);
266
267
        $this->eventDispatcher->dispatch(
268
            new DeleteVersionEvent(...$eventData),
269
            DeleteVersionEventInterface::class
270
        );
271
    }
272
273 View Code Duplication
    public function copyContent(
274
        ContentInfo $contentInfo,
275
        LocationCreateStruct $destinationLocationCreateStruct,
276
        VersionInfo $versionInfo = null
277
    ): Content {
278
        $eventData = [
279
            $contentInfo,
280
            $destinationLocationCreateStruct,
281
            $versionInfo,
282
        ];
283
284
        $beforeEvent = new BeforeCopyContentEvent(...$eventData);
0 ignored issues
show
Bug introduced by
The call to BeforeCopyContentEvent::__construct() misses a required argument $destinationLocationCreateStruct.

This check looks for function calls that miss required arguments.

Loading history...
285
286
        $this->eventDispatcher->dispatch($beforeEvent, BeforeCopyContentEventInterface::class);
287
        if ($beforeEvent->isPropagationStopped()) {
288
            return $beforeEvent->getContent();
289
        }
290
291
        $content = $beforeEvent->hasContent()
292
            ? $beforeEvent->getContent()
293
            : $this->innerService->copyContent($contentInfo, $destinationLocationCreateStruct, $versionInfo);
294
295
        $this->eventDispatcher->dispatch(
296
            new CopyContentEvent($content, ...$eventData),
0 ignored issues
show
Bug introduced by
The call to CopyContentEvent::__construct() misses a required argument $destinationLocationCreateStruct.

This check looks for function calls that miss required arguments.

Loading history...
297
            CopyContentEventInterface::class
298
        );
299
300
        return $content;
301
    }
302
303
    public function addRelation(
304
        VersionInfo $sourceVersion,
305
        ContentInfo $destinationContent
306
    ): Relation {
307
        $eventData = [
308
            $sourceVersion,
309
            $destinationContent,
310
        ];
311
312
        $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...
313
314
        $this->eventDispatcher->dispatch($beforeEvent, BeforeAddRelationEventInterface::class);
315
        if ($beforeEvent->isPropagationStopped()) {
316
            return $beforeEvent->getRelation();
317
        }
318
319
        $relation = $beforeEvent->hasRelation()
320
            ? $beforeEvent->getRelation()
321
            : $this->innerService->addRelation($sourceVersion, $destinationContent);
322
323
        $this->eventDispatcher->dispatch(
324
            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...
325
            AddRelationEventInterface::class
326
        );
327
328
        return $relation;
329
    }
330
331 View Code Duplication
    public function deleteRelation(
332
        VersionInfo $sourceVersion,
333
        ContentInfo $destinationContent
334
    ): void {
335
        $eventData = [
336
            $sourceVersion,
337
            $destinationContent,
338
        ];
339
340
        $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...
341
342
        $this->eventDispatcher->dispatch($beforeEvent, BeforeDeleteRelationEventInterface::class);
343
        if ($beforeEvent->isPropagationStopped()) {
344
            return;
345
        }
346
347
        $this->innerService->deleteRelation($sourceVersion, $destinationContent);
348
349
        $this->eventDispatcher->dispatch(
350
            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...
351
            DeleteRelationEventInterface::class
352
        );
353
    }
354
355 View Code Duplication
    public function deleteTranslation(
356
        ContentInfo $contentInfo,
357
        $languageCode
358
    ): void {
359
        $eventData = [
360
            $contentInfo,
361
            $languageCode,
362
        ];
363
364
        $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...
365
366
        $this->eventDispatcher->dispatch($beforeEvent, BeforeDeleteTranslationEventInterface::class);
367
        if ($beforeEvent->isPropagationStopped()) {
368
            return;
369
        }
370
371
        $this->innerService->deleteTranslation($contentInfo, $languageCode);
372
373
        $this->eventDispatcher->dispatch(
374
            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...
375
            DeleteTranslationEventInterface::class
376
        );
377
    }
378
379
    public function hideContent(ContentInfo $contentInfo): void
380
    {
381
        $eventData = [$contentInfo];
382
383
        $beforeEvent = new BeforeHideContentEvent(...$eventData);
384
385
        $this->eventDispatcher->dispatch($beforeEvent, BeforeHideContentEventInterface::class);
386
        if ($beforeEvent->isPropagationStopped()) {
387
            return;
388
        }
389
390
        $this->innerService->hideContent($contentInfo);
391
392
        $this->eventDispatcher->dispatch(
393
            new HideContentEvent(...$eventData),
394
            HideContentEventInterface::class
395
        );
396
    }
397
398
    public function revealContent(ContentInfo $contentInfo): void
399
    {
400
        $eventData = [$contentInfo];
401
402
        $beforeEvent = new BeforeRevealContentEvent(...$eventData);
403
404
        $this->eventDispatcher->dispatch($beforeEvent, BeforeRevealContentEventInterface::class);
405
        if ($beforeEvent->isPropagationStopped()) {
406
            return;
407
        }
408
409
        $this->innerService->revealContent($contentInfo);
410
411
        $this->eventDispatcher->dispatch(
412
            new RevealContentEvent(...$eventData),
413
            RevealContentEventInterface::class
414
        );
415
    }
416
}
417