Completed
Push — ezp-26146_location_swap_incons... ( 4a02af...baaf24 )
by
unknown
21:53
created

UrlAliasHandler::loadUrlAlias()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 13
Ratio 100 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 1
dl 13
loc 13
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * File containing the UrlAlias Handler 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
 * @version //autogentag//
10
 */
11
namespace eZ\Publish\Core\Persistence\Cache;
12
13
use eZ\Publish\API\Repository\Exceptions\NotFoundException as APINotFoundException;
14
use eZ\Publish\Core\Base\Exceptions\NotFoundException;
15
use eZ\Publish\SPI\Persistence\Content\UrlAlias\Handler as UrlAliasHandlerInterface;
16
use eZ\Publish\SPI\Persistence\Content\UrlAlias;
17
18
/**
19
 * @see eZ\Publish\SPI\Persistence\Content\UrlAlias\Handler
20
 */
21
class UrlAliasHandler extends AbstractHandler implements UrlAliasHandlerInterface
22
{
23
    /**
24
     * Constant used for storing not found results for lookup().
25
     */
26
    const NOT_FOUND = 0;
27
28
    /**
29
     * @see eZ\Publish\SPI\Persistence\Content\UrlAlias\Handler::publishUrlAliasForLocation
30
     */
31
    public function publishUrlAliasForLocation(
32
        $locationId,
33
        $parentLocationId,
34
        $name,
35
        $languageCode,
36
        $alwaysAvailable = false,
37
        $updatePathIdentificationString = false
38
    ) {
39
        $this->logger->logCall(
40
            __METHOD__,
41
            array(
42
                'location' => $locationId,
43
                'parent' => $parentLocationId,
44
                'name' => $name,
45
                'language' => $languageCode,
46
                'alwaysAvailable' => $alwaysAvailable,
47
            )
48
        );
49
        $this->clearLocation($locationId);
50
51
        $this->persistenceHandler->urlAliasHandler()->publishUrlAliasForLocation(
52
            $locationId,
53
            $parentLocationId,
54
            $name,
55
            $languageCode,
56
            $alwaysAvailable,
57
            $updatePathIdentificationString
58
        );
59
    }
60
61
    /**
62
     * @see eZ\Publish\SPI\Persistence\Content\UrlAlias\Handler::createCustomUrlAlias
63
     */
64
    public function createCustomUrlAlias($locationId, $path, $forwarding = false, $languageCode = null, $alwaysAvailable = false)
65
    {
66
        $this->logger->logCall(
67
            __METHOD__,
68
            array(
69
                'location' => $locationId,
70
                '$path' => $path,
71
                '$forwarding' => $forwarding,
72
                'language' => $languageCode,
73
                'alwaysAvailable' => $alwaysAvailable,
74
            )
75
        );
76
77
        $urlAlias = $this->persistenceHandler->urlAliasHandler()->createCustomUrlAlias(
78
            $locationId,
79
            $path,
80
            $forwarding,
81
            $languageCode,
82
            $alwaysAvailable
83
        );
84
85
        $this->cache->getItem('urlAlias', $urlAlias->id)->set($urlAlias)->save();
86
        $cache = $this->cache->getItem('urlAlias', 'location', $urlAlias->destination, 'custom');
87
        $urlAliasIds = $cache->get();
88
        if ($cache->isMiss()) {
89
            $urlAliasIds = array();
90
        }
91
92
        $urlAliasIds[] = $urlAlias->id;
93
        $cache->set($urlAliasIds)->save();
94
95
        return $urlAlias;
96
    }
97
98
    /**
99
     * @see eZ\Publish\SPI\Persistence\Content\UrlAlias\Handler::createGlobalUrlAlias
100
     */
101
    public function createGlobalUrlAlias($resource, $path, $forwarding = false, $languageCode = null, $alwaysAvailable = false)
102
    {
103
        $this->logger->logCall(
104
            __METHOD__,
105
            array(
106
                'resource' => $resource,
107
                'path' => $path,
108
                'forwarding' => $forwarding,
109
                'language' => $languageCode,
110
                'alwaysAvailable' => $alwaysAvailable,
111
            )
112
        );
113
114
        $urlAlias = $this->persistenceHandler->urlAliasHandler()->createGlobalUrlAlias(
115
            $resource,
116
            $path,
117
            $forwarding,
118
            $languageCode,
119
            $alwaysAvailable
120
        );
121
122
        $this->cache->getItem('urlAlias', $urlAlias->id)->set($urlAlias)->save();
123
124
        return $urlAlias;
125
    }
126
127
    /**
128
     * @see eZ\Publish\SPI\Persistence\Content\UrlAlias\Handler::listGlobalURLAliases
129
     */
130
    public function listGlobalURLAliases($languageCode = null, $offset = 0, $limit = -1)
131
    {
132
        $this->logger->logCall(__METHOD__, array('language' => $languageCode, 'offset' => $offset, 'limit' => $limit));
133
134
        return $this->persistenceHandler->urlAliasHandler()->listGlobalURLAliases($languageCode, $offset, $limit);
135
    }
136
137
    /**
138
     * @see eZ\Publish\SPI\Persistence\Content\UrlAlias\Handler::listURLAliasesForLocation
139
     */
140 View Code Duplication
    public function listURLAliasesForLocation($locationId, $custom = false)
141
    {
142
        // Look for location to list of url alias id's cache
143
        if ($custom) {
144
            $cache = $this->cache->getItem('urlAlias', 'location', $locationId, 'custom');
145
        } else {
146
            $cache = $this->cache->getItem('urlAlias', 'location', $locationId);
147
        }
148
        $urlAliasIds = $cache->get();
149
        if ($cache->isMiss()) {
150
            $this->logger->logCall(__METHOD__, array('location' => $locationId, 'custom' => $custom));
151
            $urlAliases = $this->persistenceHandler->urlAliasHandler()->listURLAliasesForLocation($locationId, $custom);
152
153
            $urlAliasIds = array();
154
            foreach ($urlAliases as $urlAlias) {
155
                $urlAliasIds[] = $urlAlias->id;
156
            }
157
158
            $cache->set($urlAliasIds)->save();
159
        } else {
160
            // Reuse loadUrlAlias for the url alias object cache
161
            $urlAliases = array();
162
            foreach ($urlAliasIds as $urlAliasId) {
163
                $urlAliases[] = $this->loadUrlAlias($urlAliasId);
164
            }
165
        }
166
167
        return $urlAliases;
168
    }
169
170
    /**
171
     * @see eZ\Publish\SPI\Persistence\Content\UrlAlias\Handler::removeURLAliases
172
     */
173
    public function removeURLAliases(array $urlAliases)
174
    {
175
        $this->logger->logCall(__METHOD__, array('aliases' => $urlAliases));
176
        $return = $this->persistenceHandler->urlAliasHandler()->removeURLAliases($urlAliases);
177
178
        $this->cache->clear('urlAlias', 'url');//TIMBER! (no easy way to do reverse lookup of urls)
179
        foreach ($urlAliases as $urlAlias) {
180
            $this->cache->clear('urlAlias', $urlAlias->id);
181
            if ($urlAlias->type === UrlAlias::LOCATION) {
182
                $this->cache->clear('urlAlias', 'location', $urlAlias->destination);
183
            }
184
            if ($urlAlias->isCustom) {
185
                $this->cache->clear('urlAlias', 'location', $urlAlias->destination, 'custom');
186
            }
187
        }
188
189
        return $return;
190
    }
191
192
    /**
193
     * @see eZ\Publish\SPI\Persistence\Content\UrlAlias\Handler::lookup
194
     */
195
    public function lookup($url)
196
    {
197
        // Look for url to url alias id cache
198
        // Replace slashes by "|" to be sure not to mix cache key combinations in underlying lib.
199
        $cacheKey = $url ?: '/';
200
        $cache = $this->cache->getItem('urlAlias', 'url', $cacheKey);
201
        $urlAliasId = $cache->get();
202
        if ($cache->isMiss()) {
203
            // Also cache "not found" as this function is heavliy used and hance should be cached
204
            try {
205
                $this->logger->logCall(__METHOD__, array('url' => $url));
206
                $urlAlias = $this->persistenceHandler->urlAliasHandler()->lookup($url);
207
                $cache->set($urlAlias->id)->save();
208
209
                $urlAliasCache = $this->cache->getItem('urlAlias', $urlAlias->id);
210
                $urlAliasCache->set($urlAlias)->save();
211
            } catch (APINotFoundException $e) {
212
                $cache->set(self::NOT_FOUND)->save();
213
                throw $e;
214
            }
215
        } elseif ($urlAliasId === self::NOT_FOUND) {
216
            throw new NotFoundException('UrlAlias', $url);
217
        } else {
218
            $urlAlias = $this->loadUrlAlias($urlAliasId);
219
        }
220
221
        return $urlAlias;
222
    }
223
224
    /**
225
     * @see eZ\Publish\SPI\Persistence\Content\UrlAlias\Handler::loadUrlAlias
226
     */
227 View Code Duplication
    public function loadUrlAlias($id)
228
    {
229
        // Look for url alias cache
230
        $cache = $this->cache->getItem('urlAlias', $id);
231
        $urlAlias = $cache->get();
232
        if ($cache->isMiss()) {
233
            $this->logger->logCall(__METHOD__, array('alias' => $id));
234
            $urlAlias = $this->persistenceHandler->urlAliasHandler()->loadUrlAlias($id);
235
            $cache->set($urlAlias)->save();
236
        }
237
238
        return $urlAlias;
239
    }
240
241
    /**
242
     * @see \eZ\Publish\SPI\Persistence\Content\UrlAlias\Handler::swap
243
     */
244
    public function swap($locationId1, $locationId2)
245
    {
246
        $this->logger->logCall(__METHOD__, array('location1' => $locationId1, 'location2' => $locationId2));
247
        $return = $this->persistenceHandler->urlAliasHandler()->swap($locationId1, $locationId2);
248
249
        $this->clearLocation($locationId1);
250
        $this->clearLocation($locationId2);
251
252
        return $return;
253
    }
254
255
    /**
256
     * @see eZ\Publish\SPI\Persistence\Content\UrlAlias\Handler::locationMoved
257
     */
258
    public function locationMoved($locationId, $oldParentId, $newParentId)
259
    {
260
        $this->logger->logCall(
261
            __METHOD__,
262
            array(
263
                'location' => $locationId,
264
                'oldParent' => $oldParentId,
265
                'newParent' => $newParentId,
266
            )
267
        );
268
269
        $return = $this->persistenceHandler->urlAliasHandler()->locationMoved($locationId, $oldParentId, $newParentId);
270
        $this->cache->clear('urlAlias', 'url');//TIMBER! (Will have to load url aliases for location to be able to clear specific entries)
271
272
        return $return;
273
    }
274
275
    /**
276
     * @see eZ\Publish\SPI\Persistence\Content\UrlAlias\Handler::locationCopied
277
     */
278
    public function locationCopied($locationId, $newLocationId, $newParentId)
279
    {
280
        $this->logger->logCall(
281
            __METHOD__,
282
            array(
283
                'oldLocation' => $locationId,
284
                'newLocation' => $newLocationId,
285
                'newParent' => $newParentId,
286
            )
287
        );
288
289
        $return = $this->persistenceHandler->urlAliasHandler()->locationCopied(
290
            $locationId,
291
            $newLocationId,
292
            $newParentId
293
        );
294
        $this->cache->clear('urlAlias', 'url'); // required due to caching not found aliases
295
296
        return $return;
297
    }
298
299
    /**
300
     * @see eZ\Publish\SPI\Persistence\Content\UrlAlias\Handler::locationDeleted
301
     */
302
    public function locationDeleted($locationId)
303
    {
304
        $this->logger->logCall(__METHOD__, array('location' => $locationId));
305
        $return = $this->persistenceHandler->urlAliasHandler()->locationDeleted($locationId);
306
307
        $this->clearLocation($locationId);
308
309
        return $return;
310
    }
311
312
    /**
313
     * @param $locationId
314
     */
315
    protected function clearLocation($locationId)
316
    {
317
        $locationCache = $this->cache->getItem('urlAlias', 'location', $locationId);
318
319
        if ($locationCache->isMiss()) {
320
            // we need to clear all if we don't have location id in cache
321
            $this->cache->clear('urlAlias');
322
        } else {
323
            $urlAliasIds = $locationCache->get();
324
            foreach ((array) $urlAliasIds as $urlAliasId) {
325
                $this->cache->clear('urlAlias', $urlAliasId);
326
            }
327
            $this->cache->clear('urlAlias', 'url');
328
            $locationCache->clear();
329
        }
330
    }
331
}
332