Completed
Push — master ( 92e050...06a065 )
by
unknown
19:39
created

ContentService::addRelation()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 2
dl 0
loc 22
rs 9.568
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\Values\Content\Content;
13
use eZ\Publish\API\Repository\Values\Content\ContentCreateStruct;
14
use eZ\Publish\API\Repository\Values\Content\ContentInfo;
15
use eZ\Publish\API\Repository\Values\Content\ContentMetadataUpdateStruct;
16
use eZ\Publish\API\Repository\Values\Content\ContentUpdateStruct;
17
use eZ\Publish\API\Repository\Values\Content\Language;
18
use eZ\Publish\API\Repository\Values\Content\LocationCreateStruct;
19
use eZ\Publish\API\Repository\Values\Content\Relation;
20
use eZ\Publish\API\Repository\Values\Content\VersionInfo;
21
use eZ\Publish\API\Repository\Values\User\User;
22
use eZ\Publish\Core\Event\Content\AddRelationEvent;
23
use eZ\Publish\Core\Event\Content\BeforeAddRelationEvent;
24
use eZ\Publish\Core\Event\Content\BeforeCopyContentEvent;
25
use eZ\Publish\Core\Event\Content\BeforeCreateContentDraftEvent;
26
use eZ\Publish\Core\Event\Content\BeforeCreateContentEvent;
27
use eZ\Publish\Core\Event\Content\BeforeDeleteContentEvent;
28
use eZ\Publish\Core\Event\Content\BeforeDeleteRelationEvent;
29
use eZ\Publish\Core\Event\Content\BeforeDeleteTranslationEvent;
30
use eZ\Publish\Core\Event\Content\BeforeDeleteVersionEvent;
31
use eZ\Publish\Core\Event\Content\BeforeHideContentEvent;
32
use eZ\Publish\Core\Event\Content\BeforePublishVersionEvent;
33
use eZ\Publish\Core\Event\Content\BeforeRevealContentEvent;
34
use eZ\Publish\Core\Event\Content\BeforeUpdateContentEvent;
35
use eZ\Publish\Core\Event\Content\BeforeUpdateContentMetadataEvent;
36
use eZ\Publish\Core\Event\Content\CopyContentEvent;
37
use eZ\Publish\Core\Event\Content\CreateContentDraftEvent;
38
use eZ\Publish\Core\Event\Content\CreateContentEvent;
39
use eZ\Publish\Core\Event\Content\DeleteContentEvent;
40
use eZ\Publish\Core\Event\Content\DeleteRelationEvent;
41
use eZ\Publish\Core\Event\Content\DeleteTranslationEvent;
42
use eZ\Publish\Core\Event\Content\DeleteVersionEvent;
43
use eZ\Publish\Core\Event\Content\HideContentEvent;
44
use eZ\Publish\Core\Event\Content\PublishVersionEvent;
45
use eZ\Publish\Core\Event\Content\RevealContentEvent;
46
use eZ\Publish\Core\Event\Content\UpdateContentEvent;
47
use eZ\Publish\Core\Event\Content\UpdateContentMetadataEvent;
48
use eZ\Publish\SPI\Repository\Decorator\ContentServiceDecorator;
49
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
50
51
class ContentService extends ContentServiceDecorator
52
{
53
    /** @var \Symfony\Contracts\EventDispatcher\EventDispatcherInterface */
54
    protected $eventDispatcher;
55
56
    public function __construct(
57
        ContentServiceInterface $innerService,
58
        EventDispatcherInterface $eventDispatcher
59
    ) {
60
        parent::__construct($innerService);
61
62
        $this->eventDispatcher = $eventDispatcher;
63
    }
64
65
    public function createContent(
66
        ContentCreateStruct $contentCreateStruct,
67
        array $locationCreateStructs = []
68
    ): Content {
69
        $eventData = [
70
            $contentCreateStruct,
71
            $locationCreateStructs,
72
        ];
73
74
        $beforeEvent = new BeforeCreateContentEvent(...$eventData);
75
        if ($this->eventDispatcher->dispatch($beforeEvent)->isPropagationStopped()) {
76
            return $beforeEvent->getContent();
77
        }
78
79
        $content = $beforeEvent->hasContent()
80
            ? $beforeEvent->getContent()
81
            : $this->innerService->createContent($contentCreateStruct, $locationCreateStructs);
82
83
        $this->eventDispatcher->dispatch(new CreateContentEvent($content, ...$eventData));
84
85
        return $content;
86
    }
87
88
    public function updateContentMetadata(
89
        ContentInfo $contentInfo,
90
        ContentMetadataUpdateStruct $contentMetadataUpdateStruct
91
    ): Content {
92
        $eventData = [
93
            $contentInfo,
94
            $contentMetadataUpdateStruct,
95
        ];
96
97
        $beforeEvent = new BeforeUpdateContentMetadataEvent(...$eventData);
98
        if ($this->eventDispatcher->dispatch($beforeEvent)->isPropagationStopped()) {
99
            return $beforeEvent->getContent();
100
        }
101
102
        $content = $beforeEvent->hasContent()
103
            ? $beforeEvent->getContent()
104
            : $this->innerService->updateContentMetadata($contentInfo, $contentMetadataUpdateStruct);
105
106
        $this->eventDispatcher->dispatch(new UpdateContentMetadataEvent($content, ...$eventData));
107
108
        return $content;
109
    }
110
111
    public function deleteContent(ContentInfo $contentInfo): array
112
    {
113
        $eventData = [$contentInfo];
114
115
        $beforeEvent = new BeforeDeleteContentEvent(...$eventData);
116
        if ($this->eventDispatcher->dispatch($beforeEvent)->isPropagationStopped()) {
117
            return $beforeEvent->getLocations();
118
        }
119
120
        $locations = $beforeEvent->hasLocations()
121
            ? $beforeEvent->getLocations()
122
            : $this->innerService->deleteContent($contentInfo);
123
124
        $this->eventDispatcher->dispatch(new DeleteContentEvent($locations, ...$eventData));
125
126
        return $locations;
127
    }
128
129 View Code Duplication
    public function createContentDraft(
130
        ContentInfo $contentInfo,
131
        VersionInfo $versionInfo = null,
132
        User $creator = null
133
    ): Content {
134
        $eventData = [
135
            $contentInfo,
136
            $versionInfo,
137
            $creator,
138
        ];
139
140
        $beforeEvent = new BeforeCreateContentDraftEvent(...$eventData);
141
        if ($this->eventDispatcher->dispatch($beforeEvent)->isPropagationStopped()) {
142
            return $beforeEvent->getContentDraft();
143
        }
144
145
        $contentDraft = $beforeEvent->hasContentDraft()
146
            ? $beforeEvent->getContentDraft()
147
            : $this->innerService->createContentDraft($contentInfo, $versionInfo, $creator);
148
149
        $this->eventDispatcher->dispatch(new CreateContentDraftEvent($contentDraft, ...$eventData));
150
151
        return $contentDraft;
152
    }
153
154
    public function updateContent(
155
        VersionInfo $versionInfo,
156
        ContentUpdateStruct $contentUpdateStruct
157
    ): Content {
158
        $eventData = [
159
            $versionInfo,
160
            $contentUpdateStruct,
161
        ];
162
163
        $beforeEvent = new BeforeUpdateContentEvent(...$eventData);
164
        if ($this->eventDispatcher->dispatch($beforeEvent)->isPropagationStopped()) {
165
            return $beforeEvent->getContent();
166
        }
167
168
        $content = $beforeEvent->hasContent()
169
            ? $beforeEvent->getContent()
170
            : $this->innerService->updateContent($versionInfo, $contentUpdateStruct);
171
172
        $this->eventDispatcher->dispatch(new UpdateContentEvent($content, ...$eventData));
173
174
        return $content;
175
    }
176
177
    public function publishVersion(VersionInfo $versionInfo, array $translations = Language::ALL): Content
178
    {
179
        $eventData = [$versionInfo];
180
181
        $beforeEvent = new BeforePublishVersionEvent(...$eventData);
182
        if ($this->eventDispatcher->dispatch($beforeEvent)->isPropagationStopped()) {
183
            return $beforeEvent->getContent();
184
        }
185
186
        $content = $beforeEvent->hasContent()
187
            ? $beforeEvent->getContent()
188
            : $this->innerService->publishVersion($versionInfo);
189
190
        $this->eventDispatcher->dispatch(new PublishVersionEvent($content, ...$eventData));
191
192
        return $content;
193
    }
194
195
    public function deleteVersion(VersionInfo $versionInfo): void
196
    {
197
        $eventData = [$versionInfo];
198
199
        $beforeEvent = new BeforeDeleteVersionEvent(...$eventData);
200
        if ($this->eventDispatcher->dispatch($beforeEvent)->isPropagationStopped()) {
201
            return;
202
        }
203
204
        $this->innerService->deleteVersion($versionInfo);
205
206
        $this->eventDispatcher->dispatch(new DeleteVersionEvent(...$eventData));
207
    }
208
209 View Code Duplication
    public function copyContent(
210
        ContentInfo $contentInfo,
211
        LocationCreateStruct $destinationLocationCreateStruct,
212
        VersionInfo $versionInfo = null
213
    ): Content {
214
        $eventData = [
215
            $contentInfo,
216
            $destinationLocationCreateStruct,
217
            $versionInfo,
218
        ];
219
220
        $beforeEvent = new BeforeCopyContentEvent(...$eventData);
221
        if ($this->eventDispatcher->dispatch($beforeEvent)->isPropagationStopped()) {
222
            return $beforeEvent->getContent();
223
        }
224
225
        $content = $beforeEvent->hasContent()
226
            ? $beforeEvent->getContent()
227
            : $this->innerService->copyContent($contentInfo, $destinationLocationCreateStruct, $versionInfo);
228
229
        $this->eventDispatcher->dispatch(new CopyContentEvent($content, ...$eventData));
230
231
        return $content;
232
    }
233
234
    public function addRelation(
235
        VersionInfo $sourceVersion,
236
        ContentInfo $destinationContent
237
    ): Relation {
238
        $eventData = [
239
            $sourceVersion,
240
            $destinationContent,
241
        ];
242
243
        $beforeEvent = new BeforeAddRelationEvent(...$eventData);
244
        if ($this->eventDispatcher->dispatch($beforeEvent)->isPropagationStopped()) {
245
            return $beforeEvent->getRelation();
246
        }
247
248
        $relation = $beforeEvent->hasRelation()
249
            ? $beforeEvent->getRelation()
250
            : $this->innerService->addRelation($sourceVersion, $destinationContent);
251
252
        $this->eventDispatcher->dispatch(new AddRelationEvent($relation, ...$eventData));
253
254
        return $relation;
255
    }
256
257
    public function deleteRelation(
258
        VersionInfo $sourceVersion,
259
        ContentInfo $destinationContent
260
    ): void {
261
        $eventData = [
262
            $sourceVersion,
263
            $destinationContent,
264
        ];
265
266
        $beforeEvent = new BeforeDeleteRelationEvent(...$eventData);
267
        if ($this->eventDispatcher->dispatch($beforeEvent)->isPropagationStopped()) {
268
            return;
269
        }
270
271
        $this->innerService->deleteRelation($sourceVersion, $destinationContent);
272
273
        $this->eventDispatcher->dispatch(new DeleteRelationEvent(...$eventData));
274
    }
275
276
    public function deleteTranslation(
277
        ContentInfo $contentInfo,
278
        $languageCode
279
    ): void {
280
        $eventData = [
281
            $contentInfo,
282
            $languageCode,
283
        ];
284
285
        $beforeEvent = new BeforeDeleteTranslationEvent(...$eventData);
286
        if ($this->eventDispatcher->dispatch($beforeEvent)->isPropagationStopped()) {
287
            return;
288
        }
289
290
        $this->innerService->deleteTranslation($contentInfo, $languageCode);
291
292
        $this->eventDispatcher->dispatch(new DeleteTranslationEvent(...$eventData));
293
    }
294
295
    public function hideContent(ContentInfo $contentInfo): void
296
    {
297
        $eventData = [$contentInfo];
298
299
        $beforeEvent = new BeforeHideContentEvent(...$eventData);
300
        if ($this->eventDispatcher->dispatch($beforeEvent)->isPropagationStopped()) {
301
            return;
302
        }
303
304
        $this->innerService->hideContent($contentInfo);
305
306
        $this->eventDispatcher->dispatch(new HideContentEvent(...$eventData));
307
    }
308
309
    public function revealContent(ContentInfo $contentInfo): void
310
    {
311
        $eventData = [$contentInfo];
312
313
        $beforeEvent = new BeforeRevealContentEvent(...$eventData);
314
        if ($this->eventDispatcher->dispatch($beforeEvent)->isPropagationStopped()) {
315
            return;
316
        }
317
318
        $this->innerService->revealContent($contentInfo);
319
320
        $this->eventDispatcher->dispatch(new RevealContentEvent(...$eventData));
321
    }
322
}
323