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 URLAliasService implements URLAliasServiceInterface |
||
| 22 | { |
||
| 23 | /** |
||
| 24 | * Aggregated service. |
||
| 25 | * |
||
| 26 | * @var \eZ\Publish\API\Repository\URLAliasService |
||
| 27 | */ |
||
| 28 | protected $service; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * SignalDispatcher. |
||
| 32 | * |
||
| 33 | * @var \eZ\Publish\Core\SignalSlot\SignalDispatcher |
||
| 34 | */ |
||
| 35 | protected $signalDispatcher; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Constructor. |
||
| 39 | * |
||
| 40 | * Construct service object from aggregated service and signal |
||
| 41 | * dispatcher |
||
| 42 | * |
||
| 43 | * @param \eZ\Publish\API\Repository\URLAliasService $service |
||
| 44 | * @param \eZ\Publish\Core\SignalSlot\SignalDispatcher $signalDispatcher |
||
| 45 | */ |
||
| 46 | public function __construct(URLAliasServiceInterface $service, SignalDispatcher $signalDispatcher) |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Create a user chosen $alias pointing to $location in $languageCode. |
||
| 54 | * |
||
| 55 | * This method runs URL filters and transformers before storing them. |
||
| 56 | * Hence the path returned in the URLAlias Value may differ from the given. |
||
| 57 | * $alwaysAvailable makes the alias available in all languages. |
||
| 58 | * |
||
| 59 | * @param \eZ\Publish\API\Repository\Values\Content\Location $location |
||
| 60 | * @param string $path |
||
| 61 | * @param string $languageCode the languageCode for which this alias is valid |
||
| 62 | * @param bool $forwarding if true a redirect is performed |
||
| 63 | * @param bool $alwaysAvailable |
||
| 64 | * |
||
| 65 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the path already exists for the given language |
||
| 66 | * |
||
| 67 | * @return \eZ\Publish\API\Repository\Values\Content\URLAlias |
||
| 68 | */ |
||
| 69 | public function createUrlAlias(Location $location, $path, $languageCode, $forwarding = false, $alwaysAvailable = false) |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Create a user chosen $alias pointing to a resource in $languageCode. |
||
| 85 | * |
||
| 86 | * This method does not handle location resources - if a user enters a location target |
||
| 87 | * the createCustomUrlAlias method has to be used. |
||
| 88 | * This method runs URL filters and and transformers before storing them. |
||
| 89 | * Hence the path returned in the URLAlias Value may differ from the given. |
||
| 90 | * |
||
| 91 | * $alwaysAvailable makes the alias available in all languages. |
||
| 92 | * |
||
| 93 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the path already exists for the given |
||
| 94 | * language or if resource is not valid |
||
| 95 | * |
||
| 96 | * @param string $resource |
||
| 97 | * @param string $path |
||
| 98 | * @param string $languageCode |
||
| 99 | * @param bool $forwarding |
||
| 100 | * @param bool $alwaysAvailable |
||
| 101 | * |
||
| 102 | * @return \eZ\Publish\API\Repository\Values\Content\URLAlias |
||
| 103 | */ |
||
| 104 | public function createGlobalUrlAlias($resource, $path, $languageCode, $forwarding = false, $alwaysAvailable = false) |
||
| 117 | |||
| 118 | /** |
||
| 119 | * List of url aliases pointing to $location, sorted by language priority. |
||
| 120 | * |
||
| 121 | * @param \eZ\Publish\API\Repository\Values\Content\Location $location |
||
| 122 | * @param bool $custom if true the user generated aliases are listed otherwise the autogenerated |
||
| 123 | * @param string $languageCode filters those which are valid for the given language |
||
| 124 | * @param bool|null $showAllTranslations Default false from config, include all alias as if they where always available. |
||
| 125 | * @param array|null $prioritizedLanguageList By default taken from config, used as prioritized language codes for order of returned objects. |
||
| 126 | * |
||
| 127 | * @return \eZ\Publish\API\Repository\Values\Content\URLAlias[] |
||
| 128 | */ |
||
| 129 | public function listLocationAliases( |
||
| 144 | |||
| 145 | /** |
||
| 146 | * List global aliases. |
||
| 147 | * |
||
| 148 | * @param string $languageCode filters those which are valid for the given language |
||
| 149 | * @param int $offset |
||
| 150 | * @param int $limit |
||
| 151 | * |
||
| 152 | * @return \eZ\Publish\API\Repository\Values\Content\URLAlias[] |
||
| 153 | */ |
||
| 154 | public function listGlobalAliases($languageCode = null, $offset = 0, $limit = -1) |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Removes urls aliases. |
||
| 161 | * |
||
| 162 | * This method does not remove autogenerated aliases for locations. |
||
| 163 | * |
||
| 164 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if alias list contains |
||
| 165 | * autogenerated alias |
||
| 166 | * |
||
| 167 | * @param \eZ\Publish\API\Repository\Values\Content\URLAlias[] $aliasList |
||
| 168 | */ |
||
| 169 | public function removeAliases(array $aliasList) |
||
| 182 | |||
| 183 | /** |
||
| 184 | * looks up the URLAlias for the given url. |
||
| 185 | * |
||
| 186 | * @param string $url |
||
| 187 | * @param string $languageCode |
||
| 188 | * |
||
| 189 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if the path does not exist or is not valid for the given language |
||
| 190 | * |
||
| 191 | * @return \eZ\Publish\API\Repository\Values\Content\URLAlias |
||
| 192 | */ |
||
| 193 | public function lookup($url, $languageCode = null) |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Returns the URL alias for the given location in the given language. |
||
| 200 | * |
||
| 201 | * If $languageCode is null the method returns the url alias in the most prioritized language. |
||
| 202 | * |
||
| 203 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if no url alias exist for the given language |
||
| 204 | * |
||
| 205 | * @param \eZ\Publish\API\Repository\Values\Content\Location $location |
||
| 206 | * @param string $languageCode |
||
| 207 | * @param bool|null $showAllTranslations Default false from config, include all alias as if they where always available. |
||
| 208 | * @param array|null $prioritizedLanguageList By default taken from config, used as prioritized language codes for order of returned objects. |
||
| 209 | * |
||
| 210 | * @return \eZ\Publish\API\Repository\Values\Content\URLAlias |
||
| 211 | */ |
||
| 212 | public function reverseLookup( |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Loads URL alias by given $id. |
||
| 228 | * |
||
| 229 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException |
||
| 230 | * |
||
| 231 | * @param string $id |
||
| 232 | * |
||
| 233 | * @return \eZ\Publish\API\Repository\Values\Content\URLAlias |
||
| 234 | */ |
||
| 235 | public function load($id) |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Refresh all system URL aliases for the given Location (and historize outdated if needed). |
||
| 242 | * |
||
| 243 | * @param \eZ\Publish\API\Repository\Values\Content\Location $location |
||
| 244 | */ |
||
| 245 | View Code Duplication | public function refreshSystemUrlAliasesForLocation(Location $location): void |
|
| 259 | |||
| 260 | /** |
||
| 261 | * Delete global, system or custom URL alias pointing to non-existent Locations. |
||
| 262 | * |
||
| 263 | * @return int Number of deleted URL aliases |
||
| 264 | */ |
||
| 265 | public function deleteCorruptedUrlAliases(): int |
||
| 269 | } |
||
| 270 |