Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 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) |
||
| 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) |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @see eZ\Publish\SPI\Persistence\Content\UrlAlias\Handler::listGlobalURLAliases |
||
| 129 | */ |
||
| 130 | public function listGlobalURLAliases($languageCode = null, $offset = 0, $limit = -1) |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @see eZ\Publish\SPI\Persistence\Content\UrlAlias\Handler::listURLAliasesForLocation |
||
| 139 | */ |
||
| 140 | View Code Duplication | public function listURLAliasesForLocation($locationId, $custom = false) |
|
| 169 | |||
| 170 | /** |
||
| 171 | * @see eZ\Publish\SPI\Persistence\Content\UrlAlias\Handler::removeURLAliases |
||
| 172 | */ |
||
| 173 | public function removeURLAliases(array $urlAliases) |
||
| 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) |
|
| 240 | |||
| 241 | /** |
||
| 242 | * @see eZ\Publish\SPI\Persistence\Content\UrlAlias\Handler::locationMoved |
||
| 243 | */ |
||
| 244 | View Code Duplication | public function locationMoved($locationId, $oldParentId, $newParentId) |
|
| 260 | |||
| 261 | /** |
||
| 262 | * @see eZ\Publish\SPI\Persistence\Content\UrlAlias\Handler::locationCopied |
||
| 263 | */ |
||
| 264 | View Code Duplication | public function locationCopied($locationId, $newLocationId, $newParentId) |
|
| 265 | { |
||
| 266 | $this->logger->logCall( |
||
| 267 | __METHOD__, |
||
| 268 | array( |
||
| 269 | 'oldLocation' => $locationId, |
||
| 270 | 'newLocation' => $newLocationId, |
||
| 271 | 'newParent' => $newParentId, |
||
| 272 | ) |
||
| 273 | ); |
||
| 274 | |||
| 275 | $return = $this->persistenceHandler->urlAliasHandler()->locationCopied( |
||
| 276 | $locationId, |
||
| 277 | $newLocationId, |
||
| 278 | $newParentId |
||
| 279 | ); |
||
| 280 | $this->cache->clear('urlAlias', 'url'); // required due to caching not found aliases |
||
| 281 | |||
| 282 | return $return; |
||
| 283 | } |
||
| 284 | |||
| 285 | /** |
||
| 286 | * @see eZ\Publish\SPI\Persistence\Content\UrlAlias\Handler::locationDeleted |
||
| 287 | */ |
||
| 288 | public function locationDeleted($locationId) |
||
| 297 | |||
| 298 | /** |
||
| 299 | * @param $locationId |
||
| 300 | */ |
||
| 301 | protected function clearLocation($locationId) |
||
| 317 | |||
| 318 | /** |
||
| 319 | * @see \eZ\Publish\SPI\Persistence\Content\UrlAlias\Handler::swap |
||
| 320 | */ |
||
| 321 | View Code Duplication | public function locationSwapped($location1ParentId, $location1Id, $location2ParentId, $location2Id) |
|
| 345 | } |
||
| 346 |