Completed
Push — ezp-27508-papi-delete-transl-f... ( c442c7...81e0a7 )
by
unknown
14:28
created

ContentHandler::deleteTranslationFromVersion()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 10
nc 1
nop 3
dl 0
loc 15
rs 9.4285
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\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, $version, array $translations = null)
62
    {
63
        $translationsKey = empty($translations) ? self::ALL_TRANSLATIONS_KEY : implode('|', $translations);
64
        $cache = $this->cache->getItem('content', $contentId, $version, $translationsKey);
65
        $content = $cache->get();
66 View Code Duplication
        if ($cache->isMiss()) {
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...
67
            $this->logger->logCall(__METHOD__, array('content' => $contentId, 'version' => $version, 'translations' => $translations));
68
            $content = $this->persistenceHandler->contentHandler()->load($contentId, $version, $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...
69
            $cache->set($content)->save();
70
        }
71
72
        return $content;
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78 View Code Duplication
    public function loadContentInfo($contentId)
79
    {
80
        $cache = $this->cache->getItem('content', 'info', $contentId);
81
        $contentInfo = $cache->get();
82
        if ($cache->isMiss()) {
83
            $this->logger->logCall(__METHOD__, array('content' => $contentId));
84
            $cache->set($contentInfo = $this->persistenceHandler->contentHandler()->loadContentInfo($contentId))->save();
85
        }
86
87
        return $contentInfo;
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93 View Code Duplication
    public function loadContentInfoByRemoteId($remoteId)
94
    {
95
        $cache = $this->cache->getItem('content', 'info', 'remoteId', $remoteId);
96
        $contentInfo = $cache->get();
97
        if ($cache->isMiss()) {
98
            $this->logger->logCall(__METHOD__, array('content' => $remoteId));
99
            $cache->set($contentInfo = $this->persistenceHandler->contentHandler()->loadContentInfoByRemoteId($remoteId))->save();
100
        }
101
102
        return $contentInfo;
103
    }
104
105
    /**
106
     * {@inheritdoc}
107
     */
108
    public function loadVersionInfo($contentId, $versionNo)
109
    {
110
        $this->logger->logCall(__METHOD__, array('content' => $contentId, 'version' => $versionNo));
111
112
        return $this->persistenceHandler->contentHandler()->loadVersionInfo($contentId, $versionNo);
113
    }
114
115
    /**
116
     * {@inheritdoc}
117
     */
118
    public function loadDraftsForUser($userId)
119
    {
120
        $this->logger->logCall(__METHOD__, array('user' => $userId));
121
122
        return $this->persistenceHandler->contentHandler()->loadDraftsForUser($userId);
123
    }
124
125
    /**
126
     * {@inheritdoc}
127
     */
128
    public function setStatus($contentId, $status, $version)
129
    {
130
        $this->logger->logCall(__METHOD__, array('content' => $contentId, 'status' => $status, 'version' => $version));
131
        $return = $this->persistenceHandler->contentHandler()->setStatus($contentId, $status, $version);
132
133
        $this->cache->clear('content', $contentId, $version);
134
        if ($status === VersionInfo::STATUS_PUBLISHED) {
135
            $this->cache->clear('content', 'info', $contentId);
136
            $this->cache->clear('content', 'info', 'remoteId');
137
        }
138
139
        return $return;
140
    }
141
142
    /**
143
     * {@inheritdoc}
144
     */
145
    public function updateMetadata($contentId, MetadataUpdateStruct $struct)
146
    {
147
        $this->logger->logCall(__METHOD__, array('content' => $contentId, 'struct' => $struct));
148
149
        $contentInfo = $this->persistenceHandler->contentHandler()->updateMetadata($contentId, $struct);
150
151
        $this->cache->clear('content', $contentId, $contentInfo->currentVersionNo);
152
        $this->cache->clear('content', 'info', $contentId);
153
154
        if ($struct->remoteId) {
155
            // remote id changed
156
            $this->cache->clear('content', 'info', 'remoteId');
157
        } else {
158
            $this->cache->clear('content', 'info', 'remoteId', $contentInfo->remoteId);
159
        }
160
161
        return $contentInfo;
162
    }
163
164
    /**
165
     * {@inheritdoc}
166
     */
167
    public function updateContent($contentId, $versionNo, UpdateStruct $struct)
168
    {
169
        $this->logger->logCall(__METHOD__, array('content' => $contentId, 'version' => $versionNo, 'struct' => $struct));
170
        $content = $this->persistenceHandler->contentHandler()->updateContent($contentId, $versionNo, $struct);
171
        $this->cache->clear('content', $contentId, $versionNo);
172
173
        return $content;
174
    }
175
176
    /**
177
     * {@inheritdoc}
178
     */
179
    public function deleteContent($contentId)
180
    {
181
        $this->logger->logCall(__METHOD__, array('content' => $contentId));
182
183
        // Load locations and reverse field relations first
184
        $locations = $this->persistenceHandler->locationHandler()->loadLocationsByContent($contentId);
185
        $reverseRelations = $this->persistenceHandler->contentHandler()->loadReverseRelations(
186
            $contentId,
187
            APIRelation::FIELD
188
        );
189
190
        $return = $this->persistenceHandler->contentHandler()->deleteContent($contentId);
191
192
        // Clear cache of the reversely related Content after main action has executed
193
        foreach ($reverseRelations as $relation) {
194
            $this->cache->clear('content', $relation->sourceContentId);
195
        }
196
197
        $this->cache->clear('content', $contentId);
198
        $this->cache->clear('content', 'info', $contentId);
199
        $this->cache->clear('content', 'info', 'remoteId');
200
        $this->cache->clear('location', 'subtree');
201
202
        foreach ($locations as $location) {
203
            $this->cache->clear('location', $location->id);
204
        }
205
206
        return $return;
207
    }
208
209
    /**
210
     * {@inheritdoc}
211
     */
212
    public function deleteVersion($contentId, $versionNo)
213
    {
214
        $this->logger->logCall(__METHOD__, array('content' => $contentId, 'version' => $versionNo));
215
        $return = $this->persistenceHandler->contentHandler()->deleteVersion($contentId, $versionNo);
216
217
        $this->cache->clear('content', $contentId, $versionNo);
218
        $this->cache->clear('content', 'info', $contentId);
219
        $this->cache->clear('content', 'info', 'remoteId');
220
        $this->cache->clear('location', 'subtree');
221
222
        return $return;
223
    }
224
225
    /**
226
     * {@inheritdoc}
227
     */
228
    public function listVersions($contentId, $status = null, $limit = -1)
229
    {
230
        $this->logger->logCall(__METHOD__, array('content' => $contentId, 'status' => $status));
231
232
        return $this->persistenceHandler->contentHandler()->listVersions($contentId, $status, $limit);
233
    }
234
235
    /**
236
     * {@inheritdoc}
237
     */
238
    public function addRelation(RelationCreateStruct $relation)
239
    {
240
        $this->logger->logCall(__METHOD__, array('struct' => $relation));
241
242
        return $this->persistenceHandler->contentHandler()->addRelation($relation);
243
    }
244
245
    /**
246
     * {@inheritdoc}
247
     */
248
    public function removeRelation($relationId, $type)
249
    {
250
        $this->logger->logCall(__METHOD__, array('relation' => $relationId, 'type' => $type));
251
        $this->persistenceHandler->contentHandler()->removeRelation($relationId, $type);
252
    }
253
254
    /**
255
     * {@inheritdoc}
256
     */
257
    public function loadRelations($sourceContentId, $sourceContentVersionNo = null, $type = null)
258
    {
259
        $this->logger->logCall(
260
            __METHOD__,
261
            array(
262
                'content' => $sourceContentId,
263
                'version' => $sourceContentVersionNo,
264
                'type' => $type,
265
            )
266
        );
267
268
        return $this->persistenceHandler->contentHandler()->loadRelations($sourceContentId, $sourceContentVersionNo, $type);
269
    }
270
271
    /**
272
     * {@inheritdoc}
273
     */
274
    public function loadReverseRelations($destinationContentId, $type = null)
275
    {
276
        $this->logger->logCall(__METHOD__, array('content' => $destinationContentId, 'type' => $type));
277
278
        return $this->persistenceHandler->contentHandler()->loadReverseRelations($destinationContentId, $type);
279
    }
280
281
    /**
282
     * {@inheritdoc}
283
     */
284
    public function publish($contentId, $versionNo, MetadataUpdateStruct $struct)
285
    {
286
        $this->logger->logCall(__METHOD__, array('content' => $contentId, 'version' => $versionNo, 'struct' => $struct));
287
        $content = $this->persistenceHandler->contentHandler()->publish($contentId, $versionNo, $struct);
288
289
        $this->cache->clear('content', $contentId);
290
        $this->cache->clear('content', 'info', 'remoteId');
291
        $this->cache->clear('location', 'subtree');
292
293
        // warm up cache
294
        $contentInfo = $content->versionInfo->contentInfo;
295
        $this->cache
296
            ->getItem('content', $contentInfo->id, $content->versionInfo->versionNo, self::ALL_TRANSLATIONS_KEY)
297
            ->set($content)
298
            ->save();
299
        $this->cache->getItem('content', 'info', $contentInfo->id)->set($contentInfo)->save();
300
301
        return $content;
302
    }
303
304
    /**
305
     * {@inheritdoc}
306
     */
307 View Code Duplication
    public function removeTranslationFromContent($contentId, $languageCode)
308
    {
309
        $this->logger->logCall(
310
            __METHOD__,
311
            [
312
                'contentId' => $contentId,
313
                'languageCode' => $languageCode,
314
            ]
315
        );
316
317
        $this->persistenceHandler->contentHandler()->removeTranslationFromContent($contentId, $languageCode);
318
319
        $this->cache->clear('content', $contentId);
320
        $this->cache->clear('content', 'info', $contentId);
321
    }
322
323
    /**
324
     * {@inheritdoc}
325
     */
326
    public function deleteTranslationFromDraft($contentId, $versionNo, $languageCode)
327
    {
328
        $this->logger->logCall(
329
            __METHOD__,
330
            ['content' => $contentId, 'version' => $versionNo, 'languageCode' => $languageCode]
331
        );
332
        $content = $this->persistenceHandler->contentHandler()->deleteTranslationFromDraft(
333
            $contentId,
334
            $versionNo,
335
            $languageCode
336
        );
337
        $this->cache->clear('content', $contentId, $versionNo);
338
339
        return $content;
340
    }
341
}
342