Completed
Push — master ( 97e40f...89ec5c )
by André
40:26 queued 12:35
created

loadParentLocationsForDraftContent()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 13

Duplication

Lines 20
Ratio 100 %

Importance

Changes 0
Metric Value
cc 3
eloc 13
nc 3
nop 1
dl 20
loc 20
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * File containing the LocationHandler 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\SPI\Persistence\Content\Location\Handler as LocationHandlerInterface;
12
use eZ\Publish\SPI\Persistence\Content\Location\CreateStruct;
13
use eZ\Publish\SPI\Persistence\Content\Location\UpdateStruct;
14
use eZ\Publish\SPI\Persistence\Content\Location;
15
16
/**
17
 * @see \eZ\Publish\SPI\Persistence\Content\Location\Handler
18
 */
19
class LocationHandler extends AbstractHandler implements LocationHandlerInterface
20
{
21
    /**
22
     * {@inheritdoc}
23
     */
24
    public function load($locationId)
25
    {
26
        $cacheItem = $this->cache->getItem("ez-location-${locationId}");
27
        if ($cacheItem->isHit()) {
28
            return $cacheItem->get();
29
        }
30
31
        $this->logger->logCall(__METHOD__, array('location' => $locationId));
32
        $location = $this->persistenceHandler->locationHandler()->load($locationId);
33
34
        $cacheItem->set($location);
35
        $cacheItem->tag($this->getCacheTags($location));
36
        $this->cache->save($cacheItem);
37
38
        return $location;
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function loadSubtreeIds($locationId)
45
    {
46
        $cacheItem = $this->cache->getItem("ez-location-subtree-${locationId}");
47
        if ($cacheItem->isHit()) {
48
            return $cacheItem->get();
49
        }
50
51
        $this->logger->logCall(__METHOD__, array('location' => $locationId));
52
        $locationIds = $this->persistenceHandler->locationHandler()->loadSubtreeIds($locationId);
53
54
        $cacheItem->set($locationIds);
55
        $cacheTags = ['location-' . $locationId, 'location-path-' . $locationId];
56
        foreach ($locationIds as $locationId) {
57
            $cacheTags[] = 'location-' . $locationId;
58
            $cacheTags[] = 'location-path-' . $locationId;
59
        }
60
        $cacheItem->tag($cacheTags);
61
        $this->cache->save($cacheItem);
62
63
        return $locationIds;
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    public function loadLocationsByContent($contentId, $rootLocationId = null)
70
    {
71
        if ($rootLocationId) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $rootLocationId of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
72
            $cacheItem = $this->cache->getItem("ez-content-locations-${contentId}-root-${rootLocationId}");
73
            $cacheTags = ['content-' . $contentId, 'location-' . $rootLocationId, 'location-path-' . $rootLocationId];
74
        } else {
75
            $cacheItem = $this->cache->getItem("ez-content-locations-${contentId}");
76
            $cacheTags = ['content-' . $contentId];
77
        }
78
79
        if ($cacheItem->isHit()) {
80
            return $cacheItem->get();
81
        }
82
83
        $this->logger->logCall(__METHOD__, array('content' => $contentId, 'root' => $rootLocationId));
84
        $locations = $this->persistenceHandler->locationHandler()->loadLocationsByContent($contentId, $rootLocationId);
85
86
        $cacheItem->set($locations);
87
        foreach ($locations as $location) {
88
            $cacheTags = $this->getCacheTags($location, $cacheTags);
89
        }
90
        $cacheItem->tag($cacheTags);
91
        $this->cache->save($cacheItem);
92
93
        return $locations;
94
    }
95
96
    /**
97
     * {@inheritdoc}
98
     */
99 View Code Duplication
    public function loadParentLocationsForDraftContent($contentId)
100
    {
101
        $cacheItem = $this->cache->getItem("ez-content-locations-${contentId}-parentForDraft");
102
        if ($cacheItem->isHit()) {
103
            return $cacheItem->get();
104
        }
105
106
        $this->logger->logCall(__METHOD__, array('content' => $contentId));
107
        $locations = $this->persistenceHandler->locationHandler()->loadParentLocationsForDraftContent($contentId);
108
109
        $cacheItem->set($locations);
110
        $cacheTags = ['content-' . $contentId];
111
        foreach ($locations as $location) {
112
            $cacheTags = $this->getCacheTags($location, $cacheTags);
113
        }
114
        $cacheItem->tag($cacheTags);
115
        $this->cache->save($cacheItem);
116
117
        return $locations;
118
    }
119
120
    /**
121
     * {@inheritdoc}
122
     */
123
    public function loadByRemoteId($remoteId)
124
    {
125
        $cacheItem = $this->cache->getItem("ez-location-${remoteId}-by-remoteid");
126
        if ($cacheItem->isHit()) {
127
            return $cacheItem->get();
128
        }
129
130
        $this->logger->logCall(__METHOD__, array('location' => $remoteId));
131
        $location = $this->persistenceHandler->locationHandler()->loadByRemoteId($remoteId);
132
133
        $cacheItem->set($location);
134
        $cacheItem->tag($this->getCacheTags($location));
135
        $this->cache->save($cacheItem);
136
137
        return $location;
138
    }
139
140
    /**
141
     * {@inheritdoc}
142
     */
143
    public function copySubtree($sourceId, $destinationParentId)
144
    {
145
        $this->logger->logCall(__METHOD__, array('source' => $sourceId, 'destination' => $destinationParentId));
146
147
        return $this->persistenceHandler->locationHandler()->copySubtree($sourceId, $destinationParentId);
148
    }
149
150
    /**
151
     * {@inheritdoc}
152
     */
153
    public function move($sourceId, $destinationParentId)
154
    {
155
        $this->logger->logCall(__METHOD__, array('source' => $sourceId, 'destination' => $destinationParentId));
156
        $return = $this->persistenceHandler->locationHandler()->move($sourceId, $destinationParentId);
157
158
        $this->cache->invalidateTags(['location-path-' . $sourceId]);
159
160
        return $return;
161
    }
162
163
    /**
164
     * {@inheritdoc}
165
     */
166
    public function markSubtreeModified($locationId, $timestamp = null)
167
    {
168
        $this->logger->logCall(__METHOD__, array('location' => $locationId, 'time' => $timestamp));
169
        $this->persistenceHandler->locationHandler()->markSubtreeModified($locationId, $timestamp);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\SPI\Persisten...::markSubtreeModified() has been deprecated with message: As of 6.8, not been used by repository since 5.x.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
170
    }
171
172
    /**
173
     * {@inheritdoc}
174
     */
175
    public function hide($locationId)
176
    {
177
        $this->logger->logCall(__METHOD__, array('location' => $locationId));
178
        $return = $this->persistenceHandler->locationHandler()->hide($locationId);
179
180
        $this->cache->invalidateTags(['location-path-data-' . $locationId]);
181
182
        return $return;
183
    }
184
185
    /**
186
     * {@inheritdoc}
187
     */
188
    public function unHide($locationId)
189
    {
190
        $this->logger->logCall(__METHOD__, array('location' => $locationId));
191
        $return = $this->persistenceHandler->locationHandler()->unHide($locationId);
192
193
        $this->cache->invalidateTags(['location-path-data-' . $locationId]);
194
195
        return $return;
196
    }
197
198
    /**
199
     * {@inheritdoc}
200
     */
201
    public function swap($locationId1, $locationId2)
202
    {
203
        $this->logger->logCall(__METHOD__, array('location1' => $locationId1, 'location2' => $locationId2));
204
        $locationHandler = $this->persistenceHandler->locationHandler();
205
206
        $return = $locationHandler->swap($locationId1, $locationId2);
207
208
        $this->cache->invalidateTags(['location-data-' . $locationId1, 'location-data-' . $locationId2]);
209
210
        return $return;
211
    }
212
213
    /**
214
     * {@inheritdoc}
215
     */
216
    public function update(UpdateStruct $struct, $locationId)
217
    {
218
        $this->logger->logCall(__METHOD__, array('location' => $locationId, 'struct' => $struct));
219
        $this->persistenceHandler->locationHandler()->update($struct, $locationId);
220
221
        $this->cache->invalidateTags(['location-data-' . $locationId]);
222
    }
223
224
    /**
225
     * {@inheritdoc}
226
     */
227 View Code Duplication
    public function create(CreateStruct $locationStruct)
228
    {
229
        $this->logger->logCall(__METHOD__, array('struct' => $locationStruct));
230
        $location = $this->persistenceHandler->locationHandler()->create($locationStruct);
231
232
        // need to clear loadLocationsByContent and similar collections involving locations data
233
        // also need to clear content info on main location changes
234
        $this->cache->invalidateTags(['content-' . $locationStruct->contentId, 'role-assignment-group-list-' . $locationStruct->contentId]);
235
236
        return $location;
237
    }
238
239
    /**
240
     * {@inheritdoc}
241
     */
242
    public function removeSubtree($locationId)
243
    {
244
        $this->logger->logCall(__METHOD__, array('location' => $locationId));
245
        $return = $this->persistenceHandler->locationHandler()->removeSubtree($locationId);
246
247
        $this->cache->invalidateTags(['location-path-' . $locationId]);
248
249
        return $return;
250
    }
251
252
    /**
253
     * {@inheritdoc}
254
     */
255
    public function setSectionForSubtree($locationId, $sectionId)
256
    {
257
        $this->logger->logCall(__METHOD__, array('location' => $locationId, 'section' => $sectionId));
258
        $this->persistenceHandler->locationHandler()->setSectionForSubtree($locationId, $sectionId);
259
260
        $this->cache->invalidateTags(['location-path-' . $locationId]);
261
    }
262
263
    /**
264
     * {@inheritdoc}
265
     */
266
    public function changeMainLocation($contentId, $locationId)
267
    {
268
        $this->logger->logCall(__METHOD__, array('location' => $locationId, 'content' => $contentId));
269
        $this->persistenceHandler->locationHandler()->changeMainLocation($contentId, $locationId);
270
271
        $this->cache->invalidateTags(['content-' . $contentId]);
272
    }
273
274
    /**
275
     * Return relevant content and location tags so cache can be purged reliably.
276
     *
277
     * @param Location $location
278
     * @param array $tags Optional, can be used to specify additional tags.
279
     *
280
     * @return array
281
     */
282
    private function getCacheTags(Location $location, $tags = [])
283
    {
284
        $tags[] = 'content-' . $location->contentId;
285
        $tags[] = 'location-' . $location->id;
286
        $tags[] = 'location-data-' . $location->id;
287
        foreach (explode('/', trim($location->pathString, '/')) as $pathId) {
288
            $tags[] = 'location-path-' . $pathId;
289
            $tags[] = 'location-path-data-' . $pathId;
290
        }
291
292
        return $tags;
293
    }
294
}
295