Completed
Push — 7.0-ezp27417 ( 65881e )
by
unknown
16:24
created

ContentHandler::removeTranslationFromContent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 2
rs 9.4285
1
<?php
2
3
/**
4
 * File containing the ContentHandler implementation.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 */
9
namespace eZ\Publish\Core\Persistence\Cache;
10
11
use eZ\Publish\API\Repository\Values\Content\Relation as APIRelation;
12
use eZ\Publish\SPI\Persistence\Content\Handler as ContentHandlerInterface;
13
use eZ\Publish\SPI\Persistence\Content\VersionInfo;
14
use eZ\Publish\SPI\Persistence\Content\ContentInfo;
15
use eZ\Publish\SPI\Persistence\Content\CreateStruct;
16
use eZ\Publish\SPI\Persistence\Content\UpdateStruct;
17
use eZ\Publish\SPI\Persistence\Content\MetadataUpdateStruct;
18
use eZ\Publish\SPI\Persistence\Content\Relation\CreateStruct as RelationCreateStruct;
19
20
/**
21
 * @see \eZ\Publish\SPI\Persistence\Content\Handler
22
 */
23
class ContentHandler extends AbstractHandler implements ContentHandlerInterface
24
{
25
    const ALL_TRANSLATIONS_KEY = '0';
26
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function create(CreateStruct $struct)
31
    {
32
        // Cached on demand when published or loaded
33
        $this->logger->logCall(__METHOD__, array('struct' => $struct));
34
35
        return $this->persistenceHandler->contentHandler()->create($struct);
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function createDraftFromVersion($contentId, $srcVersion, $userId)
42
    {
43
        $this->logger->logCall(__METHOD__, array('content' => $contentId, 'version' => $srcVersion, 'user' => $userId));
44
45
        return $this->persistenceHandler->contentHandler()->createDraftFromVersion($contentId, $srcVersion, $userId);
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function copy($contentId, $versionNo = null)
52
    {
53
        $this->logger->logCall(__METHOD__, array('content' => $contentId, 'version' => $versionNo));
54
55
        return $this->persistenceHandler->contentHandler()->copy($contentId, $versionNo);
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function load($contentId, $versionNo, array $translations = null)
62
    {
63
        $translationsKey = empty($translations) ? self::ALL_TRANSLATIONS_KEY : implode('|', $translations);
64
        $cacheItem = $this->cache->getItem("ez-content-${contentId}-${versionNo}-${translationsKey}");
65
        if ($cacheItem->isHit()) {
66
            return $cacheItem->get();
67
        }
68
69
        $this->logger->logCall(__METHOD__, array('content' => $contentId, 'version' => $versionNo, 'translations' => $translations));
70
        $content = $this->persistenceHandler->contentHandler()->load($contentId, $versionNo, $translations);
0 ignored issues
show
Bug introduced by
It seems like $translations defined by parameter $translations on line 61 can also be of type array; however, eZ\Publish\SPI\Persistence\Content\Handler::load() does only seem to accept null|array<integer,string>, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
71
        $cacheItem->set($content);
72
        $cacheItem->tag($this->getCacheTags($content->versionInfo->contentInfo, true));
73
        $this->cache->save($cacheItem);
74
75
        return $content;
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81
    public function loadContentInfo($contentId)
82
    {
83
        $cacheItem = $this->cache->getItem("ez-content-info-${contentId}");
84
        if ($cacheItem->isHit()) {
85
            return $cacheItem->get();
86
        }
87
88
        $this->logger->logCall(__METHOD__, array('content' => $contentId));
89
        $contentInfo = $this->persistenceHandler->contentHandler()->loadContentInfo($contentId);
90
        $cacheItem->set($contentInfo);
91
        $cacheItem->tag($this->getCacheTags($contentInfo));
92
        $this->cache->save($cacheItem);
93
94
        return $contentInfo;
95
    }
96
97
    /**
98
     * {@inheritdoc}
99
     */
100
    public function loadContentInfoByRemoteId($remoteId)
101
    {
102
        $cacheItem = $this->cache->getItem("ez-content-info-byRemoteId-${remoteId}");
103
        if ($cacheItem->isHit()) {
104
            return $cacheItem->get();
105
        }
106
107
        $this->logger->logCall(__METHOD__, array('content' => $remoteId));
108
        $contentInfo = $this->persistenceHandler->contentHandler()->loadContentInfoByRemoteId($remoteId);
109
        $cacheItem->set($contentInfo);
110
        $cacheItem->tag($this->getCacheTags($contentInfo));
111
        $this->cache->save($cacheItem);
112
113
        return $contentInfo;
114
    }
115
116
    /**
117
     * {@inheritdoc}
118
     */
119
    public function loadVersionInfo($contentId, $versionNo)
120
    {
121
        $cacheItem = $this->cache->getItem("ez-content-version-info-${contentId}-${versionNo}");
122
        if ($cacheItem->isHit()) {
123
            return $cacheItem->get();
124
        }
125
126
        $this->logger->logCall(__METHOD__, ['content' => $contentId, 'version' => $versionNo]);
127
        $versionInfo = $this->persistenceHandler->contentHandler()->loadVersionInfo($contentId, $versionNo);
128
        $cacheItem->set($versionInfo);
129
        $cacheItem->tag($this->getCacheTags($versionInfo->contentInfo));
130
        $this->cache->save($cacheItem);
131
132
        return $versionInfo;
133
    }
134
135
    /**
136
     * {@inheritdoc}
137
     */
138
    public function loadDraftsForUser($userId)
139
    {
140
        $this->logger->logCall(__METHOD__, array('user' => $userId));
141
142
        return $this->persistenceHandler->contentHandler()->loadDraftsForUser($userId);
143
    }
144
145
    /**
146
     * {@inheritdoc}
147
     */
148
    public function setStatus($contentId, $status, $versionNo)
149
    {
150
        $this->logger->logCall(__METHOD__, array('content' => $contentId, 'status' => $status, 'version' => $versionNo));
151
        $return = $this->persistenceHandler->contentHandler()->setStatus($contentId, $status, $versionNo);
152
153
        $this->cache->deleteItem("ez-content-version-info-${contentId}-${versionNo}");
154
        if ($status === VersionInfo::STATUS_PUBLISHED) {
155
            $this->cache->invalidateTags(['content-' . $contentId]);
156
        } else {
157
            $this->cache->invalidateTags(["content-$contentId-version-list"]);
158
        }
159
160
        return $return;
161
    }
162
163
    /**
164
     * {@inheritdoc}
165
     */
166
    public function updateMetadata($contentId, MetadataUpdateStruct $struct)
167
    {
168
        $this->logger->logCall(__METHOD__, array('content' => $contentId, 'struct' => $struct));
169
        $contentInfo = $this->persistenceHandler->contentHandler()->updateMetadata($contentId, $struct);
170
        $this->cache->invalidateTags(['content-' . $contentId]);
171
172
        return $contentInfo;
173
    }
174
175
    /**
176
     * {@inheritdoc}
177
     */
178
    public function updateContent($contentId, $versionNo, UpdateStruct $struct)
179
    {
180
        $this->logger->logCall(__METHOD__, array('content' => $contentId, 'version' => $versionNo, 'struct' => $struct));
181
        $content = $this->persistenceHandler->contentHandler()->updateContent($contentId, $versionNo, $struct);
182
        $this->cache->invalidateTags(['content-' . $contentId]);
183
184
        return $content;
185
    }
186
187
    /**
188
     * {@inheritdoc}
189
     */
190
    public function deleteContent($contentId)
191
    {
192
        $this->logger->logCall(__METHOD__, array('content' => $contentId));
193
194
        // Load reverse field relations first
195
        $reverseRelations = $this->persistenceHandler->contentHandler()->loadReverseRelations(
196
            $contentId,
197
            APIRelation::FIELD
198
        );
199
200
        $return = $this->persistenceHandler->contentHandler()->deleteContent($contentId);
201
202
        $this->cache->invalidateTags(['content-' . $contentId]);
203
        if (!empty($reverseRelations)) {
204
            $this->cache->invalidateTags(
205
                array_map(
206
                    function ($relation) {
207
                        // only the full content object *with* fields is affected by this
208
                        return 'content-fields-' . $relation->sourceContentId;
209
                    },
210
                    $reverseRelations
211
                )
212
            );
213
        }
214
215
        return $return;
216
    }
217
218
    /**
219
     * {@inheritdoc}
220
     */
221
    public function deleteVersion($contentId, $versionNo)
222
    {
223
        $this->logger->logCall(__METHOD__, array('content' => $contentId, 'version' => $versionNo));
224
        $return = $this->persistenceHandler->contentHandler()->deleteVersion($contentId, $versionNo);
225
        $this->cache->invalidateTags(['content-' . $contentId]);
226
227
        return $return;
228
    }
229
230
    /**
231
     * {@inheritdoc}
232
     */
233
    public function listVersions($contentId, $status = null, $limit = -1)
234
    {
235
        $cacheItem = $this->cache->getItem("ez-content-${contentId}-version-list" . ($status ? "-byStatus-${status}" : ''));
236
        if ($cacheItem->isHit()) {
237
            return $cacheItem->get();
238
        }
239
240
        $this->logger->logCall(__METHOD__, array('content' => $contentId, 'status' => $status));
241
        $versions = $this->persistenceHandler->contentHandler()->listVersions($contentId, $status, $limit);
242
        $cacheItem->set($versions);
243
        $tags = ["content-$contentId", "content-$contentId-version-list"];
244
        $cacheItem->tag(empty($versions) ? $tags : $this->getCacheTags($versions[0]->contentInfo, false, $tags));
245
        $this->cache->save($cacheItem);
246
247
        return $versions;
248
    }
249
250
    /**
251
     * {@inheritdoc}
252
     */
253
    public function addRelation(RelationCreateStruct $relation)
254
    {
255
        $this->logger->logCall(__METHOD__, array('struct' => $relation));
256
257
        return $this->persistenceHandler->contentHandler()->addRelation($relation);
258
    }
259
260
    /**
261
     * {@inheritdoc}
262
     */
263
    public function removeRelation($relationId, $type)
264
    {
265
        $this->logger->logCall(__METHOD__, array('relation' => $relationId, 'type' => $type));
266
        $this->persistenceHandler->contentHandler()->removeRelation($relationId, $type);
267
    }
268
269
    /**
270
     * {@inheritdoc}
271
     */
272
    public function loadRelations($sourceContentId, $sourceContentVersionNo = null, $type = null)
273
    {
274
        $this->logger->logCall(
275
            __METHOD__,
276
            array(
277
                'content' => $sourceContentId,
278
                'version' => $sourceContentVersionNo,
279
                'type' => $type,
280
            )
281
        );
282
283
        return $this->persistenceHandler->contentHandler()->loadRelations($sourceContentId, $sourceContentVersionNo, $type);
284
    }
285
286
    /**
287
     * {@inheritdoc}
288
     */
289
    public function loadReverseRelations($destinationContentId, $type = null)
290
    {
291
        $this->logger->logCall(__METHOD__, array('content' => $destinationContentId, 'type' => $type));
292
293
        return $this->persistenceHandler->contentHandler()->loadReverseRelations($destinationContentId, $type);
294
    }
295
296
    /**
297
     * {@inheritdoc}
298
     */
299
    public function publish($contentId, $versionNo, MetadataUpdateStruct $struct)
300
    {
301
        $this->logger->logCall(__METHOD__, array('content' => $contentId, 'version' => $versionNo, 'struct' => $struct));
302
        $content = $this->persistenceHandler->contentHandler()->publish($contentId, $versionNo, $struct);
303
        $this->cache->invalidateTags(['content-' . $contentId]);
304
305
        return $content;
306
    }
307
308
    /**
309
     * Return relevant content and location tags so cache can be purged reliably.
310
     *
311
     * @param \eZ\Publish\SPI\Persistence\Content\ContentInfo $contentInfo
312
     * @param bool $withFields Set to true if item contains fields which should be expired on relation or type updates.
313
     * @param array $tags Optional, can be used to specify other tags.
314
     *
315
     * @return array
316
     */
317
    private function getCacheTags(ContentInfo $contentInfo, $withFields = false, array $tags = [])
318
    {
319
        $tags[] = 'content-' . $contentInfo->id;
320
321
        if ($withFields) {
322
            $tags[] = 'content-fields-' . $contentInfo->id;
323
            $tags[] = 'content-fields-type-' . $contentInfo->contentTypeId;
324
        }
325
326
        if ($contentInfo->mainLocationId) {
327
            $tags[] = 'location-' . $contentInfo->mainLocationId;
328
329
            $location = $this->persistenceHandler->locationHandler()->load($contentInfo->mainLocationId);
330 View Code Duplication
            foreach (explode('/', trim($location->pathString, '/')) as $pathId) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
331
                $tags[] = 'location-path-' . $pathId;
332
            }
333
        }
334
335
        return $tags;
336
    }
337
338
    /**
339
     * {@inheritdoc}
340
     */
341
    public function removeTranslationFromContent($contentId, $languageCode)
342
    {
343
        $this->logger->logCall(
344
            __METHOD__,
345
            [
346
                'contentId' => $contentId,
347
                'languageCode' => $languageCode,
348
            ]
349
        );
350
351
        $this->persistenceHandler->contentHandler()->removeTranslationFromContent($contentId, $languageCode);
352
353
        $this->cache->clear('content', $contentId);
354
        $this->cache->clear('content', 'info', $contentId);
355
    }
356
}
357