Complex classes like Localization often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Localization, and based on these observations, apply Extract Interface, too.
| 1 | <?php namespace Arcanedev\Localization; |
||
| 17 | class Localization implements LocalizationContract |
||
| 18 | { |
||
| 19 | /* ------------------------------------------------------------------------------------------------ |
||
| 20 | | Properties |
||
| 21 | | ------------------------------------------------------------------------------------------------ |
||
| 22 | */ |
||
| 23 | /** |
||
| 24 | * Base url. |
||
| 25 | * |
||
| 26 | * @var string |
||
| 27 | */ |
||
| 28 | protected $baseUrl; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Laravel application instance. |
||
| 32 | * |
||
| 33 | * @var \Illuminate\Foundation\Application |
||
| 34 | */ |
||
| 35 | private $app; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * The RouteTranslator instance. |
||
| 39 | * |
||
| 40 | * @var \Arcanedev\Localization\Contracts\RouteTranslator |
||
| 41 | */ |
||
| 42 | protected $routeTranslator; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * The LocalesManager instance. |
||
| 46 | * |
||
| 47 | * @var \Arcanedev\Localization\Contracts\LocalesManager |
||
| 48 | */ |
||
| 49 | private $localesManager; |
||
| 50 | |||
| 51 | /* ------------------------------------------------------------------------------------------------ |
||
| 52 | | Constructor |
||
| 53 | | ------------------------------------------------------------------------------------------------ |
||
| 54 | */ |
||
| 55 | /** |
||
| 56 | * Creates new instance. |
||
| 57 | * |
||
| 58 | * @param \Illuminate\Foundation\Application $app |
||
| 59 | * @param \Arcanedev\Localization\Contracts\RouteTranslator $routeTranslator |
||
| 60 | * @param \Arcanedev\Localization\Contracts\LocalesManager $localesManager |
||
| 61 | */ |
||
| 62 | 504 | public function __construct( |
|
| 63 | Application $app, |
||
| 64 | RouteTranslatorContract $routeTranslator, |
||
| 65 | LocalesManagerContract $localesManager |
||
| 66 | ) { |
||
| 67 | 504 | $this->app = $app; |
|
| 68 | 504 | $this->routeTranslator = $routeTranslator; |
|
| 69 | 504 | $this->localesManager = $localesManager; |
|
| 70 | |||
| 71 | 504 | $this->localesManager->setDefaultLocale( |
|
| 72 | 504 | $this->app->getLocale() |
|
| 73 | 252 | ); |
|
| 74 | 504 | } |
|
| 75 | |||
| 76 | /* ------------------------------------------------------------------------------------------------ |
||
| 77 | | Getters & Setters |
||
| 78 | | ------------------------------------------------------------------------------------------------ |
||
| 79 | */ |
||
| 80 | /** |
||
| 81 | * Get Request instance. |
||
| 82 | * |
||
| 83 | * @return \Illuminate\Http\Request |
||
| 84 | */ |
||
| 85 | 72 | private function request() |
|
| 86 | { |
||
| 87 | 72 | return $this->app['request']; |
|
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Returns default locale. |
||
| 92 | * |
||
| 93 | * @return string |
||
| 94 | */ |
||
| 95 | 104 | public function getDefaultLocale() |
|
| 96 | { |
||
| 97 | 104 | return $this->localesManager->getDefaultLocale(); |
|
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Return an array of all supported Locales. |
||
| 102 | * |
||
| 103 | * @return \Arcanedev\Localization\Entities\LocaleCollection |
||
| 104 | */ |
||
| 105 | 80 | public function getSupportedLocales() |
|
| 106 | { |
||
| 107 | 80 | return $this->localesManager->getSupportedLocales(); |
|
| 108 | } |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Set the supported locales. |
||
| 112 | * |
||
| 113 | * @param array $supportedLocales |
||
| 114 | * |
||
| 115 | * @return self |
||
| 116 | */ |
||
| 117 | 16 | public function setSupportedLocales(array $supportedLocales) |
|
| 118 | { |
||
| 119 | 16 | $this->localesManager->setSupportedLocales($supportedLocales); |
|
| 120 | |||
| 121 | 8 | return $this; |
|
| 122 | } |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Get supported locales keys. |
||
| 126 | * |
||
| 127 | * @return array |
||
| 128 | */ |
||
| 129 | 8 | public function getSupportedLocalesKeys() |
|
| 130 | { |
||
| 131 | 8 | return $this->localesManager->getSupportedLocalesKeys(); |
|
| 132 | } |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Returns current language. |
||
| 136 | * |
||
| 137 | * @return string |
||
| 138 | */ |
||
| 139 | 504 | public function getCurrentLocale() |
|
| 140 | { |
||
| 141 | 504 | return $this->localesManager->getCurrentLocale(); |
|
| 142 | } |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Returns current language. |
||
| 146 | * |
||
| 147 | * @return \Arcanedev\Localization\Entities\Locale |
||
| 148 | */ |
||
| 149 | 40 | public function getCurrentLocaleEntity() |
|
| 150 | { |
||
| 151 | 40 | return $this->localesManager->getCurrentLocaleEntity(); |
|
| 152 | } |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Returns current locale name. |
||
| 156 | * |
||
| 157 | * @return string |
||
| 158 | */ |
||
| 159 | 8 | public function getCurrentLocaleName() |
|
| 160 | { |
||
| 161 | 8 | return $this->getCurrentLocaleEntity()->name(); |
|
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Returns current locale script. |
||
| 166 | * |
||
| 167 | * @return string |
||
| 168 | */ |
||
| 169 | 8 | public function getCurrentLocaleScript() |
|
| 173 | |||
| 174 | /** |
||
| 175 | * Returns current locale direction. |
||
| 176 | * |
||
| 177 | * @return string |
||
| 178 | */ |
||
| 179 | 8 | public function getCurrentLocaleDirection() |
|
| 180 | { |
||
| 181 | 8 | return $this->getCurrentLocaleEntity()->direction(); |
|
| 182 | } |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Returns current locale native name. |
||
| 186 | * |
||
| 187 | * @return string |
||
| 188 | */ |
||
| 189 | 8 | public function getCurrentLocaleNative() |
|
| 190 | { |
||
| 191 | 8 | return $this->getCurrentLocaleEntity()->native(); |
|
| 192 | } |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Returns current locale regional. |
||
| 196 | * |
||
| 197 | * @return string |
||
| 198 | */ |
||
| 199 | 8 | public function getCurrentLocaleRegional() |
|
| 200 | { |
||
| 201 | 8 | return $this->getCurrentLocaleEntity()->regional(); |
|
| 202 | } |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Get all locales. |
||
| 206 | * |
||
| 207 | * @return \Arcanedev\Localization\Entities\LocaleCollection |
||
| 208 | */ |
||
| 209 | 8 | public function getAllLocales() |
|
| 210 | { |
||
| 211 | 8 | return $this->localesManager->getAllLocales(); |
|
| 212 | } |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Set and return current locale. |
||
| 216 | * |
||
| 217 | * @param string|null $locale |
||
| 218 | * |
||
| 219 | * @return string |
||
| 220 | */ |
||
| 221 | 504 | public function setLocale($locale = null) |
|
| 222 | { |
||
| 223 | 504 | return $this->localesManager->setLocale($locale); |
|
| 224 | } |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Sets the base url for the site. |
||
| 228 | * |
||
| 229 | * @param string $url |
||
| 230 | * |
||
| 231 | * @return self |
||
| 232 | */ |
||
| 233 | 504 | public function setBaseUrl($url) |
|
| 234 | 4 | { |
|
| 235 | 504 | if (substr($url, -1) !== '/') $url .= '/'; |
|
| 236 | |||
| 237 | 504 | $this->baseUrl = $url; |
|
| 238 | |||
| 239 | 504 | return $this; |
|
| 240 | } |
||
| 241 | |||
| 242 | /* ------------------------------------------------------------------------------------------------ |
||
| 243 | | Main Functions |
||
| 244 | | ------------------------------------------------------------------------------------------------ |
||
| 245 | */ |
||
| 246 | /** |
||
| 247 | * Translate routes and save them to the translated routes array (used in the localize route filter). |
||
| 248 | * |
||
| 249 | * @param string $routeName |
||
| 250 | * |
||
| 251 | * @return string |
||
| 252 | */ |
||
| 253 | 504 | public function transRoute($routeName) |
|
| 254 | { |
||
| 255 | 504 | return $this->routeTranslator->trans($routeName); |
|
| 256 | } |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Returns an URL adapted to $locale or current locale. |
||
| 260 | * |
||
| 261 | * @param string|null $url |
||
| 262 | * @param string|null $locale |
||
| 263 | * |
||
| 264 | * @return string |
||
| 265 | */ |
||
| 266 | 16 | public function localizeURL($url = null, $locale = null) |
|
| 270 | |||
| 271 | /** |
||
| 272 | * It returns an URL without locale (if it has it). |
||
| 273 | * |
||
| 274 | * @param string|null $url |
||
| 275 | * |
||
| 276 | * @return string |
||
| 277 | */ |
||
| 278 | 72 | public function getNonLocalizedURL($url = null) |
|
| 282 | |||
| 283 | /** |
||
| 284 | * Returns an URL adapted to $locale or current locale. |
||
| 285 | * |
||
| 286 | * @todo: Refactor this beast |
||
| 287 | * |
||
| 288 | * @param string|null $locale |
||
| 289 | * @param string|null $url |
||
| 290 | * @param array $attributes |
||
| 291 | * |
||
| 292 | * @return string|false |
||
| 293 | */ |
||
| 294 | 72 | public function getLocalizedURL($locale = null, $url = null, $attributes = []) |
|
| 295 | { |
||
| 296 | 72 | if (is_null($locale)) { |
|
| 297 | 40 | $locale = $this->getCurrentLocale(); |
|
| 298 | 20 | } |
|
| 362 | |||
| 363 | /** |
||
| 364 | * Create an url from the uri. |
||
| 365 | * |
||
| 366 | * @param string $uri |
||
| 367 | * |
||
| 368 | * @return string |
||
| 369 | */ |
||
| 370 | 80 | public function createUrlFromUri($uri) |
|
| 376 | |||
| 377 | /** |
||
| 378 | * Get locales navigation bar. |
||
| 379 | * |
||
| 380 | * @return string |
||
| 381 | */ |
||
| 382 | 8 | public function localesNavbar() |
|
| 388 | |||
| 389 | /* ------------------------------------------------------------------------------------------------ |
||
| 390 | | Translation Functions |
||
| 391 | | ------------------------------------------------------------------------------------------------ |
||
| 392 | */ |
||
| 393 | /** |
||
| 394 | * Returns the translated route for an url and the attributes given and a locale |
||
| 395 | * |
||
| 396 | * @param string $url |
||
| 397 | * @param array $attributes |
||
| 398 | * @param string $locale |
||
| 399 | * |
||
| 400 | * @return string|false |
||
| 401 | */ |
||
| 402 | 64 | private function findTranslatedRouteByUrl($url, $attributes, $locale) |
|
| 417 | |||
| 418 | /** |
||
| 419 | * Returns an URL adapted to the route name and the locale given. |
||
| 420 | * |
||
| 421 | * @param string $locale |
||
| 422 | * @param string $transKey |
||
| 423 | * @param array $attributes |
||
| 424 | * |
||
| 425 | * @return string|false |
||
| 426 | */ |
||
| 427 | 80 | public function getUrlFromRouteName($locale, $transKey, $attributes = []) |
|
| 444 | |||
| 445 | /** |
||
| 446 | * Set route name from request. |
||
| 447 | * |
||
| 448 | * @param \Illuminate\Http\Request $request |
||
| 449 | */ |
||
| 450 | 8 | public function setRouteNameFromRequest(Request $request) |
|
| 458 | |||
| 459 | /* ------------------------------------------------------------------------------------------------ |
||
| 460 | | Check Functions |
||
| 461 | | ------------------------------------------------------------------------------------------------ |
||
| 462 | */ |
||
| 463 | /** |
||
| 464 | * Hide the default locale in URL ?? |
||
| 465 | * |
||
| 466 | * @return bool |
||
| 467 | */ |
||
| 468 | 72 | public function isDefaultLocaleHiddenInUrl() |
|
| 472 | |||
| 473 | /** |
||
| 474 | * Check if Locale exists on the supported locales collection. |
||
| 475 | * |
||
| 476 | * @param string|bool $locale |
||
| 477 | * |
||
| 478 | * @return bool |
||
| 479 | */ |
||
| 480 | 88 | public function isLocaleSupported($locale) |
|
| 484 | |||
| 485 | /** |
||
| 486 | * Check if the locale is supported or fail if not. |
||
| 487 | * |
||
| 488 | * @param string $locale |
||
| 489 | * |
||
| 490 | * @throws \Arcanedev\Localization\Exceptions\UnsupportedLocaleException |
||
| 491 | */ |
||
| 492 | 88 | private function isLocaleSupportedOrFail($locale) |
|
| 500 | } |
||
| 501 |