Completed
Push — sf_cache ( b9ead9...8c890f )
by André
18:16
created

ContentHandler::loadContentInfoList()   C

Complexity

Conditions 8
Paths 22

Size

Total Lines 47
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 27
nc 22
nop 1
dl 0
loc 47
rs 5.7377
c 0
b 0
f 0
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;
14
use eZ\Publish\SPI\Persistence\Content\VersionInfo;
15
use eZ\Publish\SPI\Persistence\Content\ContentInfo;
16
use eZ\Publish\SPI\Persistence\Content\CreateStruct;
17
use eZ\Publish\SPI\Persistence\Content\UpdateStruct;
18
use eZ\Publish\SPI\Persistence\Content\MetadataUpdateStruct;
19
use eZ\Publish\SPI\Persistence\Content\Relation\CreateStruct as RelationCreateStruct;
20
21
/**
22
 * @see \eZ\Publish\SPI\Persistence\Content\Handler
23
 */
24
class ContentHandler extends AbstractHandler implements ContentHandlerInterface
25
{
26
    const ALL_TRANSLATIONS_KEY = '0';
27
28
    /**
29
     * {@inheritdoc}
30
     */
31
    public function create(CreateStruct $struct)
32
    {
33
        // Cached on demand when published or loaded
34
        $this->logger->logCall(__METHOD__, array('struct' => $struct));
35
36
        return $this->persistenceHandler->contentHandler()->create($struct);
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function createDraftFromVersion($contentId, $srcVersion, $userId)
43
    {
44
        $this->logger->logCall(__METHOD__, array('content' => $contentId, 'version' => $srcVersion, 'user' => $userId));
45
46
        return $this->persistenceHandler->contentHandler()->createDraftFromVersion($contentId, $srcVersion, $userId);
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function copy($contentId, $versionNo = null)
53
    {
54
        $this->logger->logCall(__METHOD__, array('content' => $contentId, 'version' => $versionNo));
55
56
        return $this->persistenceHandler->contentHandler()->copy($contentId, $versionNo);
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function load($contentId, $versionNo, array $translations = null)
63
    {
64
        $translationsKey = empty($translations) ? self::ALL_TRANSLATIONS_KEY : implode('|', $translations);
65
        $cacheItem = $this->cache->getItem("ez-content-${contentId}-${versionNo}-${translationsKey}");
66
        if ($cacheItem->isHit()) {
67
            return $cacheItem->get();
68
        }
69
70
        $this->logger->logCall(__METHOD__, array('content' => $contentId, 'version' => $versionNo, 'translations' => $translations));
71
        $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 62 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...
72
        $cacheItem->set($content);
73
        $cacheItem->tag($this->getCacheTags($content->versionInfo->contentInfo, true));
74
        $this->cache->save($cacheItem);
75
76
        return $content;
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82
    public function loadContentInfo($contentId)
83
    {
84
        $cacheItem = $this->cache->getItem("ez-content-info-${contentId}");
85
        if ($cacheItem->isHit()) {
86
            return $cacheItem->get();
87
        }
88
89
        $this->logger->logCall(__METHOD__, array('content' => $contentId));
90
        $contentInfo = $this->persistenceHandler->contentHandler()->loadContentInfo($contentId);
91
        $cacheItem->set($contentInfo);
92
        $cacheItem->tag($this->getCacheTags($contentInfo));
93
        $this->cache->save($cacheItem);
94
95
        return $contentInfo;
96
    }
97
98
    /**
99
     * Return list of Content Info in the order they are requested.
100
     *
101
     * @todo Might have issues if there are duplicates of content id's.
102
     *
103
     * @param array $contentIds
104
     *
105
     * @return \eZ\Publish\SPI\Persistence\Content\ContentInfo[]
106
     */
107
    public function loadContentInfoList(array $contentIds)
108
    {
109
        if (empty($contentIds)) {
110
            return [];
111
        }
112
113
        // Generate cache keys
114
        $cacheKeys = array_map(function($contentId){
115
            return "ez-content-info-${contentId}";
116
        }, $contentIds);
117
118
        // Load cache and check for cache misses
119
        $cacheMissIds = $contentInfoList = [];
120
        $cacheItems = $this->cache->getItems($cacheKeys);
121
        foreach ($cacheItems as $key => $cacheItem) {
122
            if ($cacheItem->isHit()) {
123
                $contentInfoList[] = $cacheItem->get();
124
            } else {
125
                $cacheMissIds[] = explode('-', $key)[3];
126
                $contentInfoList[] = $cacheItem;
127
            }
128
        }
129
130
        if (empty($cacheMissIds)) {
131
            return $contentInfoList;
132
        }
133
134
        // Load cache misses
135
        $cacheMissContentInfoList = [];
136
        foreach ($this->persistenceHandler->contentHandler()->loadContentInfoList($cacheMissIds) as $contentInfo) {
137
            $cacheMissContentInfoList['ez-content-info-'.$contentInfo->id] = $contentInfo;
138
        }
139
140
        // Populate cache misses with data and set final info data instead on list
141
        foreach ($contentInfoList as $key => $cacheItem) {
142
            if ($cacheItem instanceof ContentInfo) {
143
                continue;
144
            }
145
146
            $cacheItem->set($contentInfo = $cacheMissContentInfoList[$cacheItem->getKey()]);
147
            $cacheItem->tag($this->getCacheTags($contentInfo));
148
            $this->cache->save($cacheItem);
149
            $contentInfoList[$key] = $contentInfo;
150
        }
151
152
        return $contentInfoList;
153
    }
154
155
    /**
156
     * {@inheritdoc}
157
     */
158
    public function loadContentInfoByRemoteId($remoteId)
159
    {
160
        $cacheItem = $this->cache->getItem("ez-content-info-byRemoteId-${remoteId}");
161
        if ($cacheItem->isHit()) {
162
            return $cacheItem->get();
163
        }
164
165
        $this->logger->logCall(__METHOD__, array('content' => $remoteId));
166
        $contentInfo = $this->persistenceHandler->contentHandler()->loadContentInfoByRemoteId($remoteId);
167
        $cacheItem->set($contentInfo);
168
        $cacheItem->tag($this->getCacheTags($contentInfo));
169
        $this->cache->save($cacheItem);
170
171
        return $contentInfo;
172
    }
173
174
    /**
175
     * {@inheritdoc}
176
     */
177
    public function loadVersionInfo($contentId, $versionNo)
178
    {
179
        $cacheItem = $this->cache->getItem("ez-content-version-info-${contentId}-${versionNo}");
180
        if ($cacheItem->isHit()) {
181
            return $cacheItem->get();
182
        }
183
184
        $this->logger->logCall(__METHOD__, ['content' => $contentId, 'version' => $versionNo]);
185
        $versionInfo = $this->persistenceHandler->contentHandler()->loadVersionInfo($contentId, $versionNo);
186
        $cacheItem->set($versionInfo);
187
        $cacheItem->tag($this->getCacheTags($versionInfo->contentInfo));
188
        $this->cache->save($cacheItem);
189
190
        return $versionInfo;
191
    }
192
193
    /**
194
     * {@inheritdoc}
195
     *
196
     * @todo With a user-drafts-<user-id> tag we can identify operations that will need to clear it to introduce cache
197
     *       here, as long as those have access to user id.
198
     */
199
    public function loadDraftsForUser($userId)
200
    {
201
        $this->logger->logCall(__METHOD__, array('user' => $userId));
202
203
        return $this->persistenceHandler->contentHandler()->loadDraftsForUser($userId);
204
    }
205
206
    /**
207
     * {@inheritdoc}
208
     */
209
    public function setStatus($contentId, $status, $versionNo)
210
    {
211
        $this->logger->logCall(__METHOD__, array('content' => $contentId, 'status' => $status, 'version' => $versionNo));
212
        $return = $this->persistenceHandler->contentHandler()->setStatus($contentId, $status, $versionNo);
213
214
        $this->cache->deleteItem("ez-content-version-info-${contentId}-${versionNo}");
215
        if ($status === VersionInfo::STATUS_PUBLISHED) {
216
            $this->cache->invalidateTags(['content-'.$contentId]);
217
        }
218
219
        return $return;
220
    }
221
222
    /**
223
     * {@inheritdoc}
224
     */
225
    public function updateMetadata($contentId, MetadataUpdateStruct $struct)
226
    {
227
        $this->logger->logCall(__METHOD__, array('content' => $contentId, 'struct' => $struct));
228
        $contentInfo =  $this->persistenceHandler->contentHandler()->updateMetadata($contentId, $struct);
229
        $this->cache->invalidateTags(['content-'.$contentId]);
230
231
        return $contentInfo;
232
    }
233
234
    /**
235
     * {@inheritdoc}
236
     */
237
    public function updateContent($contentId, $versionNo, UpdateStruct $struct)
238
    {
239
        $this->logger->logCall(__METHOD__, array('content' => $contentId, 'version' => $versionNo, 'struct' => $struct));
240
        $content = $this->persistenceHandler->contentHandler()->updateContent($contentId, $versionNo, $struct);
241
        $this->cache->invalidateTags(['content-'.$contentId]);
242
243
        return $content;
244
    }
245
246
    /**
247
     * {@inheritdoc}
248
     */
249
    public function deleteContent($contentId)
250
    {
251
        $this->logger->logCall(__METHOD__, array('content' => $contentId));
252
253
        // Load reverse field relations first
254
        $reverseRelations = $this->persistenceHandler->contentHandler()->loadReverseRelations(
255
            $contentId,
256
            APIRelation::FIELD
257
        );
258
259
        $return = $this->persistenceHandler->contentHandler()->deleteContent($contentId);
260
261
        $this->cache->invalidateTags(['content-'.$contentId]);
262
        if (!empty($reverseRelations)) {
263
            $this->cache->invalidateTags(
264
                array_map(
265
                    function($relation){
266
                        // only the full content object *with* fields is affected by this
267
                        return 'content-fields-'.$relation->sourceContentId;
268
                    },
269
                    $reverseRelations
270
                )
271
            );
272
        }
273
274
275
276
        return $return;
277
    }
278
279
    /**
280
     * {@inheritdoc}
281
     */
282
    public function deleteVersion($contentId, $versionNo)
283
    {
284
        $this->logger->logCall(__METHOD__, array('content' => $contentId, 'version' => $versionNo));
285
        $return = $this->persistenceHandler->contentHandler()->deleteVersion($contentId, $versionNo);
286
        $this->cache->invalidateTags(['content-'.$contentId]);
287
288
        return $return;
289
    }
290
291
    /**
292
     * {@inheritdoc}
293
     *
294
     * @todo Could cache this now by identifying which operations affect it and needed tag, eg content-<id>-versions
295
     */
296
    public function listVersions($contentId, $status = null, $limit = -1)
297
    {
298
        $this->logger->logCall(__METHOD__, array('content' => $contentId, 'status' => $status));
299
300
        return $this->persistenceHandler->contentHandler()->listVersions($contentId, $status, $limit);
301
    }
302
303
    /**
304
     * {@inheritdoc}
305
     */
306
    public function addRelation(RelationCreateStruct $relation)
307
    {
308
        $this->logger->logCall(__METHOD__, array('struct' => $relation));
309
310
        return $this->persistenceHandler->contentHandler()->addRelation($relation);
311
    }
312
313
    /**
314
     * {@inheritdoc}
315
     */
316
    public function removeRelation($relationId, $type)
317
    {
318
        $this->logger->logCall(__METHOD__, array('relation' => $relationId, 'type' => $type));
319
        $this->persistenceHandler->contentHandler()->removeRelation($relationId, $type);
320
    }
321
322
    /**
323
     * {@inheritdoc}
324
     *
325
     * @todo Could cache this now by identifying which operations affect it and needed tag, eg content-<id>-relations
326
     */
327
    public function loadRelations($sourceContentId, $sourceContentVersionNo = null, $type = null)
328
    {
329
        $this->logger->logCall(
330
            __METHOD__,
331
            array(
332
                'content' => $sourceContentId,
333
                'version' => $sourceContentVersionNo,
334
                'type' => $type,
335
            )
336
        );
337
338
        return $this->persistenceHandler->contentHandler()->loadRelations($sourceContentId, $sourceContentVersionNo, $type);
339
    }
340
341
    /**
342
     * {@inheritdoc}
343
     *
344
     * @todo Could cache this now by identifying which operations affect it and needed tag, eg content-<id>-reverse-relations
345
     */
346
    public function loadReverseRelations($destinationContentId, $type = null)
347
    {
348
        $this->logger->logCall(__METHOD__, array('content' => $destinationContentId, 'type' => $type));
349
350
        return $this->persistenceHandler->contentHandler()->loadReverseRelations($destinationContentId, $type);
351
    }
352
353
    /**
354
     * {@inheritdoc}
355
     */
356
    public function publish($contentId, $versionNo, MetadataUpdateStruct $struct)
357
    {
358
        $this->logger->logCall(__METHOD__, array('content' => $contentId, 'version' => $versionNo, 'struct' => $struct));
359
        $content = $this->persistenceHandler->contentHandler()->publish($contentId, $versionNo, $struct);
360
        $this->cache->invalidateTags(['content-'.$contentId]);
361
362
        return $content;
363
    }
364
365
    /**
366
     * Return relevant content and location tags so cache can be purged reliably.
367
     *
368
     * @param ContentInfo $contentInfo
369
     * @param bool $withFields Set to true if item contains fields which should be expired on relation or type updates.
370
     * @param array $tags Optional, can be used to specify other tags.
371
     *
372
     * @return array
373
     */
374
    private function getCacheTags(ContentInfo $contentInfo, $withFields = false, array $tags = [])
375
    {
376
        $tags[] = 'content-'.$contentInfo->id;
377
378
        if ($withFields) {
379
            $tags[] = 'content-fields-'.$contentInfo->id;
380
            $tags[] = 'content-fields-type-'.$contentInfo->contentTypeId;
381
        }
382
383
        if ($contentInfo->mainLocationId) {
384
            $tags[] = 'location-'.$contentInfo->mainLocationId;
385
386
            $location = $this->persistenceHandler->locationHandler()->load($contentInfo->mainLocationId);
387 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...
388
                $tags[] = 'location-path-'.$pathId;
389
            }
390
        }
391
392
        return $tags;
393
    }
394
}
395