Completed
Push — ezp-27508-papi-delete-transl-f... ( e6c34a...11a3cc )
by
unknown
14:42
created

ContentHandler::deleteTranslationFromVersion()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 14

Duplication

Lines 19
Ratio 100 %

Importance

Changes 0
Metric Value
dl 19
loc 19
c 0
b 0
f 0
cc 1
eloc 14
nc 1
nop 3
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;
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 View Code Duplication
    public function updateMetadata($contentId, MetadataUpdateStruct $struct)
146
    {
147
        $this->logger->logCall(__METHOD__, array('content' => $contentId, 'struct' => $struct));
148
149
        $this->cache
150
            ->getItem('content', 'info', $contentId)
151
            ->set($contentInfo = $this->persistenceHandler->contentHandler()->updateMetadata($contentId, $struct))->save();
152
153
        $this->cache->clear('content', $contentId, $contentInfo->currentVersionNo);
154
155
        return $contentInfo;
156
    }
157
158
    /**
159
     * {@inheritdoc}
160
     */
161 View Code Duplication
    public function updateContent($contentId, $versionNo, UpdateStruct $struct)
162
    {
163
        $this->logger->logCall(__METHOD__, array('content' => $contentId, 'version' => $versionNo, 'struct' => $struct));
164
        $content = $this->persistenceHandler->contentHandler()->updateContent($contentId, $versionNo, $struct);
165
        $this->cache->clear('content', $contentId, $versionNo);
166
        $this->cache
167
            ->getItem('content', $contentId, $versionNo, self::ALL_TRANSLATIONS_KEY)
168
            ->set($content)
169
            ->save();
170
171
        return $content;
172
    }
173
174
    /**
175
     * {@inheritdoc}
176
     */
177
    public function deleteContent($contentId)
178
    {
179
        $this->logger->logCall(__METHOD__, array('content' => $contentId));
180
181
        // Load locations and reverse field relations first
182
        $locations = $this->persistenceHandler->locationHandler()->loadLocationsByContent($contentId);
183
        $reverseRelations = $this->persistenceHandler->contentHandler()->loadReverseRelations(
184
            $contentId,
185
            APIRelation::FIELD
186
        );
187
188
        $return = $this->persistenceHandler->contentHandler()->deleteContent($contentId);
189
190
        // Clear cache of the reversely related Content after main action has executed
191
        foreach ($reverseRelations as $relation) {
192
            $this->cache->clear('content', $relation->sourceContentId);
193
        }
194
195
        $this->cache->clear('content', $contentId);
196
        $this->cache->clear('content', 'info', $contentId);
197
        $this->cache->clear('content', 'info', 'remoteId');
198
        $this->cache->clear('location', 'subtree');
199
200
        foreach ($locations as $location) {
201
            $this->cache->clear('location', $location->id);
202
        }
203
204
        return $return;
205
    }
206
207
    /**
208
     * {@inheritdoc}
209
     */
210
    public function deleteVersion($contentId, $versionNo)
211
    {
212
        $this->logger->logCall(__METHOD__, array('content' => $contentId, 'version' => $versionNo));
213
        $return = $this->persistenceHandler->contentHandler()->deleteVersion($contentId, $versionNo);
214
215
        $this->cache->clear('content', $contentId, $versionNo);
216
        $this->cache->clear('content', 'info', $contentId);
217
        $this->cache->clear('content', 'info', 'remoteId');
218
        $this->cache->clear('location', 'subtree');
219
220
        return $return;
221
    }
222
223
    /**
224
     * {@inheritdoc}
225
     */
226
    public function listVersions($contentId, $status = null, $limit = -1)
227
    {
228
        $this->logger->logCall(__METHOD__, array('content' => $contentId, 'status' => $status));
229
230
        return $this->persistenceHandler->contentHandler()->listVersions($contentId, $status, $limit);
231
    }
232
233
    /**
234
     * {@inheritdoc}
235
     */
236
    public function addRelation(RelationCreateStruct $relation)
237
    {
238
        $this->logger->logCall(__METHOD__, array('struct' => $relation));
239
240
        return $this->persistenceHandler->contentHandler()->addRelation($relation);
241
    }
242
243
    /**
244
     * {@inheritdoc}
245
     */
246
    public function removeRelation($relationId, $type)
247
    {
248
        $this->logger->logCall(__METHOD__, array('relation' => $relationId, 'type' => $type));
249
        $this->persistenceHandler->contentHandler()->removeRelation($relationId, $type);
250
    }
251
252
    /**
253
     * {@inheritdoc}
254
     */
255
    public function loadRelations($sourceContentId, $sourceContentVersionNo = null, $type = null)
256
    {
257
        $this->logger->logCall(
258
            __METHOD__,
259
            array(
260
                'content' => $sourceContentId,
261
                'version' => $sourceContentVersionNo,
262
                'type' => $type,
263
            )
264
        );
265
266
        return $this->persistenceHandler->contentHandler()->loadRelations($sourceContentId, $sourceContentVersionNo, $type);
267
    }
268
269
    /**
270
     * {@inheritdoc}
271
     */
272
    public function loadReverseRelations($destinationContentId, $type = null)
273
    {
274
        $this->logger->logCall(__METHOD__, array('content' => $destinationContentId, 'type' => $type));
275
276
        return $this->persistenceHandler->contentHandler()->loadReverseRelations($destinationContentId, $type);
277
    }
278
279
    /**
280
     * {@inheritdoc}
281
     */
282
    public function publish($contentId, $versionNo, MetadataUpdateStruct $struct)
283
    {
284
        $this->logger->logCall(__METHOD__, array('content' => $contentId, 'version' => $versionNo, 'struct' => $struct));
285
        $content = $this->persistenceHandler->contentHandler()->publish($contentId, $versionNo, $struct);
286
287
        $this->cache->clear('content', $contentId);
288
        $this->cache->clear('content', 'info', 'remoteId');
289
        $this->cache->clear('location', 'subtree');
290
291
        // warm up cache
292
        $contentInfo = $content->versionInfo->contentInfo;
293
        $this->cache
294
            ->getItem('content', $contentInfo->id, $content->versionInfo->versionNo, self::ALL_TRANSLATIONS_KEY)
295
            ->set($content)
296
            ->save();
297
        $this->cache->getItem('content', 'info', $contentInfo->id)->set($contentInfo)->save();
298
299
        return $content;
300
    }
301
302
    /**
303
     * {@inheritdoc}
304
     */
305 View Code Duplication
    public function removeTranslationFromContent($contentId, $languageCode)
306
    {
307
        $this->logger->logCall(
308
            __METHOD__,
309
            [
310
                'contentId' => $contentId,
311
                'languageCode' => $languageCode,
312
            ]
313
        );
314
315
        $this->persistenceHandler->contentHandler()->removeTranslationFromContent($contentId, $languageCode);
316
317
        $this->cache->clear('content', $contentId);
318
        $this->cache->clear('content', 'info', $contentId);
319
    }
320
321
    /**
322
     * {@inheritdoc}
323
     */
324 View Code Duplication
    public function deleteTranslationFromVersion($contentId, $versionNo, $languageCode)
325
    {
326
        $this->logger->logCall(
327
            __METHOD__,
328
            ['content' => $contentId, 'version' => $versionNo, 'languageCode' => $languageCode]
329
        );
330
        $content = $this->persistenceHandler->contentHandler()->deleteTranslationFromVersion(
331
            $contentId,
332
            $versionNo,
333
            $languageCode
334
        );
335
        $this->cache->clear('content', $contentId, $versionNo);
336
        $this->cache
337
            ->getItem('content', $contentId, $versionNo, self::ALL_TRANSLATIONS_KEY)
338
            ->set($content)
339
            ->save();
340
341
        return $content;
342
    }
343
}
344