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 |
||
| 19 | class UrlAliasHandler extends AbstractHandler implements UrlAliasHandlerInterface |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * Constant used for storing not found results for lookup(). |
||
| 23 | */ |
||
| 24 | const NOT_FOUND = 0; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * {@inheritdoc} |
||
| 28 | */ |
||
| 29 | View Code Duplication | public function publishUrlAliasForLocation( |
|
| 30 | $locationId, |
||
| 31 | $parentLocationId, |
||
| 32 | $name, |
||
| 33 | $languageCode, |
||
| 34 | $alwaysAvailable = false, |
||
| 35 | $updatePathIdentificationString = false |
||
| 36 | ) { |
||
| 37 | $this->logger->logCall( |
||
| 38 | __METHOD__, |
||
| 39 | array( |
||
| 40 | 'location' => $locationId, |
||
| 41 | 'parent' => $parentLocationId, |
||
| 42 | 'name' => $name, |
||
| 43 | 'language' => $languageCode, |
||
| 44 | 'alwaysAvailable' => $alwaysAvailable, |
||
| 45 | ) |
||
| 46 | ); |
||
| 47 | |||
| 48 | $this->persistenceHandler->urlAliasHandler()->publishUrlAliasForLocation( |
||
| 49 | $locationId, |
||
| 50 | $parentLocationId, |
||
| 51 | $name, |
||
| 52 | $languageCode, |
||
| 53 | $alwaysAvailable, |
||
| 54 | $updatePathIdentificationString |
||
| 55 | ); |
||
| 56 | |||
| 57 | $this->cache->invalidateTags(['urlAlias-location-'.$locationId, 'urlAlias-notFound']); |
||
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * {@inheritdoc} |
||
| 62 | */ |
||
| 63 | View Code Duplication | public function createCustomUrlAlias($locationId, $path, $forwarding = false, $languageCode = null, $alwaysAvailable = false) |
|
| 64 | { |
||
| 65 | $this->logger->logCall( |
||
| 66 | __METHOD__, |
||
| 67 | array( |
||
| 68 | 'location' => $locationId, |
||
| 69 | '$path' => $path, |
||
| 70 | '$forwarding' => $forwarding, |
||
| 71 | 'language' => $languageCode, |
||
| 72 | 'alwaysAvailable' => $alwaysAvailable, |
||
| 73 | ) |
||
| 74 | ); |
||
| 75 | |||
| 76 | $urlAlias = $this->persistenceHandler->urlAliasHandler()->createCustomUrlAlias( |
||
| 77 | $locationId, |
||
| 78 | $path, |
||
| 79 | $forwarding, |
||
| 80 | $languageCode, |
||
| 81 | $alwaysAvailable |
||
| 82 | ); |
||
| 83 | |||
| 84 | $this->cache->invalidateTags(['urlAlias-location-'.$locationId, 'urlAlias-notFound']); |
||
| 85 | |||
| 86 | return $urlAlias; |
||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * {@inheritdoc} |
||
| 91 | */ |
||
| 92 | View Code Duplication | public function createGlobalUrlAlias($resource, $path, $forwarding = false, $languageCode = null, $alwaysAvailable = false) |
|
| 93 | { |
||
| 94 | $this->logger->logCall( |
||
| 95 | __METHOD__, |
||
| 96 | array( |
||
| 97 | 'resource' => $resource, |
||
| 98 | 'path' => $path, |
||
| 99 | 'forwarding' => $forwarding, |
||
| 100 | 'language' => $languageCode, |
||
| 101 | 'alwaysAvailable' => $alwaysAvailable, |
||
| 102 | ) |
||
| 103 | ); |
||
| 104 | |||
| 105 | $urlAlias = $this->persistenceHandler->urlAliasHandler()->createGlobalUrlAlias( |
||
| 106 | $resource, |
||
| 107 | $path, |
||
| 108 | $forwarding, |
||
| 109 | $languageCode, |
||
| 110 | $alwaysAvailable |
||
| 111 | ); |
||
| 112 | |||
| 113 | $this->cache->invalidateTags(['urlAlias-global-'.$languageCode, 'urlAlias-notFound']); |
||
| 114 | |||
| 115 | return $urlAlias; |
||
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * {@inheritdoc} |
||
| 120 | */ |
||
| 121 | public function listGlobalURLAliases($languageCode = null, $offset = 0, $limit = -1) |
||
| 122 | { |
||
| 123 | $this->logger->logCall(__METHOD__, array('language' => $languageCode, 'offset' => $offset, 'limit' => $limit)); |
||
| 124 | |||
| 125 | return $this->persistenceHandler->urlAliasHandler()->listGlobalURLAliases($languageCode, $offset, $limit); |
||
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * {@inheritdoc} |
||
| 130 | */ |
||
| 131 | public function listURLAliasesForLocation($locationId, $custom = false) |
||
| 132 | { |
||
| 133 | $cacheItem = $this->cache->getItem('ez-urlAlias-location-list-'.$locationId.($custom ? '-custom' : '')); |
||
| 134 | if ($cacheItem->isHit()) { |
||
| 135 | return $cacheItem->get(); |
||
| 136 | } |
||
| 137 | |||
| 138 | $this->logger->logCall(__METHOD__, array('location' => $locationId, 'custom' => $custom)); |
||
| 139 | $urlAliases = $this->persistenceHandler->urlAliasHandler()->listURLAliasesForLocation($locationId, $custom); |
||
| 140 | |||
| 141 | $cacheItem->set($urlAliases); |
||
| 142 | $cacheTags = ['urlAlias-location-'.$locationId]; |
||
| 143 | foreach ($urlAliases as $urlAlias) { |
||
| 144 | $cacheTags[] = 'urlAlias-'.$urlAlias->id; |
||
| 145 | } |
||
| 146 | $cacheItem->tag($cacheTags); |
||
| 147 | $this->cache->save($cacheItem); |
||
| 148 | |||
| 149 | return $urlAliases; |
||
| 150 | } |
||
| 151 | |||
| 152 | /** |
||
| 153 | * {@inheritdoc} |
||
| 154 | */ |
||
| 155 | public function removeURLAliases(array $urlAliases) |
||
| 156 | { |
||
| 157 | $this->logger->logCall(__METHOD__, array('aliases' => $urlAliases)); |
||
| 158 | $return = $this->persistenceHandler->urlAliasHandler()->removeURLAliases($urlAliases); |
||
| 159 | |||
| 160 | $cacheTags = []; |
||
| 161 | foreach ($urlAliases as $urlAlias) { |
||
| 162 | $cacheTags[] = 'urlAlias-'.$urlAlias->id; |
||
| 163 | if ($urlAlias->type === UrlAlias::LOCATION) { |
||
| 164 | $cacheTags[] = 'urlAlias-location-'.$urlAlias->destination; |
||
| 165 | } |
||
| 166 | if ($urlAlias->isCustom) { |
||
| 167 | $cacheTags[] = 'urlAlias-custom-'.$urlAlias->destination; |
||
| 168 | } |
||
| 169 | } |
||
| 170 | $this->cache->invalidateTags($cacheTags); |
||
| 171 | |||
| 172 | return $return; |
||
| 173 | } |
||
| 174 | |||
| 175 | /** |
||
| 176 | * {@inheritdoc} |
||
| 177 | */ |
||
| 178 | public function lookup($url) |
||
| 179 | { |
||
| 180 | $cacheItem = $this->cache->getItem('ez-urlAlias-url-'.str_replace('/', '_', $url)); |
||
| 181 | if ($cacheItem->isHit()) { |
||
| 182 | if (($return = $cacheItem->get()) === self::NOT_FOUND) { |
||
| 183 | throw new NotFoundException('UrlAlias', $url); |
||
| 184 | } |
||
| 185 | |||
| 186 | return $return; |
||
| 187 | } |
||
| 188 | |||
| 189 | $this->logger->logCall(__METHOD__, array('url' => $url)); |
||
| 190 | try { |
||
| 191 | $urlAlias = $this->persistenceHandler->urlAliasHandler()->lookup($url); |
||
| 192 | } catch (APINotFoundException $e) { |
||
| 193 | $cacheItem->set(self::NOT_FOUND) |
||
| 194 | ->expiresAfter(30) |
||
| 195 | ->tag(['urlAlias-notFound']); |
||
| 196 | $this->cache->save($cacheItem); |
||
| 197 | throw $e; |
||
| 198 | } |
||
| 199 | |||
| 200 | $cacheItem->set($urlAlias); |
||
| 201 | $cachTags = ['urlAlias-'.$urlAlias->id]; |
||
| 202 | if ($urlAlias->type === UrlAlias::LOCATION) { |
||
| 203 | $cachTags[] = 'urlAlias-location-'.$urlAlias->destination; |
||
| 204 | } |
||
| 205 | $cacheItem->tag($cachTags); |
||
| 206 | $this->cache->save($cacheItem); |
||
| 207 | |||
| 208 | |||
| 209 | return $urlAlias; |
||
| 210 | } |
||
| 211 | |||
| 212 | /** |
||
| 213 | * {@inheritdoc} |
||
| 214 | */ |
||
| 215 | public function loadUrlAlias($id) |
||
| 216 | { |
||
| 217 | $cacheItem = $this->cache->getItem('ez-urlAlias-'.$id); |
||
| 218 | if ($cacheItem->isHit()) { |
||
| 219 | return $cacheItem->get(); |
||
| 220 | } |
||
| 221 | |||
| 222 | $this->logger->logCall(__METHOD__, array('alias' => $id)); |
||
| 223 | $urlAlias = $this->persistenceHandler->urlAliasHandler()->loadUrlAlias($id); |
||
| 224 | |||
| 225 | $cacheItem->set($urlAlias); |
||
| 226 | if ($urlAlias->type === UrlAlias::LOCATION) { |
||
| 227 | $cacheItem->tag(['urlAlias-'.$urlAlias->id, 'urlAlias-location-'.$urlAlias->destination]); |
||
| 228 | } else { |
||
| 229 | $cacheItem->tag(['urlAlias-'.$urlAlias->id]); |
||
| 230 | } |
||
| 231 | $this->cache->save($cacheItem); |
||
| 232 | |||
| 233 | |||
| 234 | return $urlAlias; |
||
| 235 | } |
||
| 236 | |||
| 237 | /** |
||
| 238 | * {@inheritdoc} |
||
| 239 | */ |
||
| 240 | public function locationMoved($locationId, $oldParentId, $newParentId) |
||
| 241 | { |
||
| 242 | $this->logger->logCall( |
||
| 243 | __METHOD__, |
||
| 244 | array( |
||
| 245 | 'location' => $locationId, |
||
| 246 | 'oldParent' => $oldParentId, |
||
| 247 | 'newParent' => $newParentId, |
||
| 248 | ) |
||
| 249 | ); |
||
| 250 | |||
| 251 | $return = $this->persistenceHandler->urlAliasHandler()->locationMoved($locationId, $oldParentId, $newParentId); |
||
| 252 | |||
| 253 | $this->cache->invalidateTags(['urlAlias-location-'.$locationId]); |
||
| 254 | |||
| 255 | return $return; |
||
| 256 | } |
||
| 257 | |||
| 258 | /** |
||
| 259 | * {@inheritdoc} |
||
| 260 | */ |
||
| 261 | View Code Duplication | public function locationCopied($locationId, $newLocationId, $newParentId) |
|
| 262 | { |
||
| 263 | $this->logger->logCall( |
||
| 264 | __METHOD__, |
||
| 265 | array( |
||
| 266 | 'oldLocation' => $locationId, |
||
| 267 | 'newLocation' => $newLocationId, |
||
| 268 | 'newParent' => $newParentId, |
||
| 269 | ) |
||
| 270 | ); |
||
| 271 | |||
| 272 | $return = $this->persistenceHandler->urlAliasHandler()->locationCopied( |
||
| 273 | $locationId, |
||
| 274 | $newLocationId, |
||
| 275 | $newParentId |
||
| 276 | ); |
||
| 277 | $this->cache->invalidateTags(['urlAlias-location-'.$locationId, 'urlAlias-location-'.$newLocationId]); |
||
| 278 | |||
| 279 | return $return; |
||
| 280 | } |
||
| 281 | |||
| 282 | /** |
||
| 283 | * {@inheritdoc} |
||
| 284 | */ |
||
| 285 | public function locationDeleted($locationId) |
||
| 294 | |||
| 295 | /** |
||
| 296 | * {@inheritdoc} |
||
| 297 | */ |
||
| 298 | View Code Duplication | public function locationSwapped($location1Id, $location1ParentId, $location2Id, $location2ParentId) |
|
| 299 | { |
||
| 300 | $this->logger->logCall( |
||
| 301 | __METHOD__, |
||
| 302 | [ |
||
| 303 | 'location1Id' => $location1Id, |
||
| 304 | 'location1ParentId' => $location1ParentId, |
||
| 321 | } |
||
| 322 |