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( |
||
| 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) |
||
| 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::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) |
||
| 331 | } |
||
| 332 |