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; |
||
| 18 | class Localization implements LocalizationContract |
||
| 19 | { |
||
| 20 | /* ----------------------------------------------------------------- |
||
| 21 | | Properties |
||
| 22 | | ----------------------------------------------------------------- |
||
| 23 | */ |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Base url. |
||
| 27 | * |
||
| 28 | * @var string |
||
| 29 | */ |
||
| 30 | protected $baseUrl; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Laravel application instance. |
||
| 34 | * |
||
| 35 | * @var \Illuminate\Contracts\Foundation\Application |
||
| 36 | */ |
||
| 37 | private $app; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * The RouteTranslator instance. |
||
| 41 | * |
||
| 42 | * @var \Arcanedev\Localization\Contracts\RouteTranslator |
||
| 43 | */ |
||
| 44 | protected $routeTranslator; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * The LocalesManager instance. |
||
| 48 | * |
||
| 49 | * @var \Arcanedev\Localization\Contracts\LocalesManager |
||
| 50 | */ |
||
| 51 | private $localesManager; |
||
| 52 | |||
| 53 | /* ----------------------------------------------------------------- |
||
| 54 | | Constructor |
||
| 55 | | ----------------------------------------------------------------- |
||
| 56 | */ |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Localization constructor. |
||
| 60 | * |
||
| 61 | * @param \Illuminate\Contracts\Foundation\Application $app |
||
| 62 | * @param \Arcanedev\Localization\Contracts\RouteTranslator $routeTranslator |
||
| 63 | * @param \Arcanedev\Localization\Contracts\LocalesManager $localesManager |
||
| 64 | */ |
||
| 65 | 252 | public function __construct( |
|
| 66 | ApplicationContract $app, |
||
| 67 | RouteTranslatorContract $routeTranslator, |
||
| 68 | LocalesManagerContract $localesManager |
||
| 69 | ) { |
||
| 70 | 252 | $this->app = $app; |
|
| 71 | 252 | $this->routeTranslator = $routeTranslator; |
|
| 72 | 252 | $this->localesManager = $localesManager; |
|
| 73 | |||
| 74 | 252 | $this->localesManager->setDefaultLocale( |
|
| 75 | 252 | $this->app['config']->get('app.locale') |
|
| 76 | ); |
||
| 77 | 252 | } |
|
| 78 | |||
| 79 | /* ----------------------------------------------------------------- |
||
| 80 | | Getters & Setters |
||
| 81 | | ----------------------------------------------------------------- |
||
| 82 | */ |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Get Request instance. |
||
| 86 | * |
||
| 87 | * @return \Illuminate\Http\Request |
||
| 88 | */ |
||
| 89 | 106 | private function request() |
|
| 93 | |||
| 94 | /** |
||
| 95 | * Returns default locale. |
||
| 96 | * |
||
| 97 | * @return string |
||
| 98 | */ |
||
| 99 | 116 | public function getDefaultLocale() |
|
| 103 | |||
| 104 | /** |
||
| 105 | * Return an array of all supported Locales. |
||
| 106 | * |
||
| 107 | * @return \Arcanedev\Localization\Entities\LocaleCollection |
||
| 108 | */ |
||
| 109 | 108 | public function getSupportedLocales() |
|
| 113 | |||
| 114 | /** |
||
| 115 | * Set the supported locales. |
||
| 116 | * |
||
| 117 | * @param array $supportedLocales |
||
| 118 | * |
||
| 119 | * @return self |
||
| 120 | */ |
||
| 121 | 4 | public function setSupportedLocales(array $supportedLocales) |
|
| 127 | |||
| 128 | /** |
||
| 129 | * Get supported locales keys. |
||
| 130 | * |
||
| 131 | * @return array |
||
| 132 | */ |
||
| 133 | 2 | public function getSupportedLocalesKeys() |
|
| 137 | |||
| 138 | /** |
||
| 139 | * Returns current language. |
||
| 140 | * |
||
| 141 | * @return string |
||
| 142 | */ |
||
| 143 | 110 | public function getCurrentLocale() |
|
| 147 | |||
| 148 | /** |
||
| 149 | * Returns current language. |
||
| 150 | * |
||
| 151 | * @return \Arcanedev\Localization\Entities\Locale |
||
| 152 | */ |
||
| 153 | 10 | public function getCurrentLocaleEntity() |
|
| 157 | |||
| 158 | /** |
||
| 159 | * Returns current locale name. |
||
| 160 | * |
||
| 161 | * @return string |
||
| 162 | */ |
||
| 163 | 2 | public function getCurrentLocaleName() |
|
| 167 | |||
| 168 | /** |
||
| 169 | * Returns current locale script. |
||
| 170 | * |
||
| 171 | * @return string |
||
| 172 | */ |
||
| 173 | 2 | public function getCurrentLocaleScript() |
|
| 177 | |||
| 178 | /** |
||
| 179 | * Returns current locale direction. |
||
| 180 | * |
||
| 181 | * @return string |
||
| 182 | */ |
||
| 183 | 2 | public function getCurrentLocaleDirection() |
|
| 187 | |||
| 188 | /** |
||
| 189 | * Returns current locale native name. |
||
| 190 | * |
||
| 191 | * @return string |
||
| 192 | */ |
||
| 193 | 2 | public function getCurrentLocaleNative() |
|
| 197 | |||
| 198 | /** |
||
| 199 | * Returns current locale regional. |
||
| 200 | * |
||
| 201 | * @return string |
||
| 202 | */ |
||
| 203 | 2 | public function getCurrentLocaleRegional() |
|
| 207 | |||
| 208 | /** |
||
| 209 | * Get all locales. |
||
| 210 | * |
||
| 211 | * @return \Arcanedev\Localization\Entities\LocaleCollection |
||
| 212 | */ |
||
| 213 | 2 | public function getAllLocales() |
|
| 217 | |||
| 218 | /** |
||
| 219 | * Set and return current locale. |
||
| 220 | * |
||
| 221 | * @param string|null $locale |
||
| 222 | * |
||
| 223 | * @return string |
||
| 224 | */ |
||
| 225 | 252 | public function setLocale($locale = null) |
|
| 229 | |||
| 230 | /** |
||
| 231 | * Sets the base url for the site. |
||
| 232 | * |
||
| 233 | * @param string $url |
||
| 234 | * |
||
| 235 | * @return self |
||
| 236 | */ |
||
| 237 | 252 | public function setBaseUrl($url) |
|
| 245 | |||
| 246 | /* ----------------------------------------------------------------- |
||
| 247 | | Main Methods |
||
| 248 | | ----------------------------------------------------------------- |
||
| 249 | */ |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Translate routes and save them to the translated routes array (used in the localize route filter). |
||
| 253 | * |
||
| 254 | * @param string $routeName |
||
| 255 | * |
||
| 256 | * @return string |
||
| 257 | */ |
||
| 258 | 252 | public function transRoute($routeName) |
|
| 262 | |||
| 263 | /** |
||
| 264 | * Returns an URL adapted to $locale or current locale. |
||
| 265 | * |
||
| 266 | * @param string|null $url |
||
| 267 | * @param string|null $locale |
||
| 268 | * |
||
| 269 | * @return string |
||
| 270 | */ |
||
| 271 | 4 | public function localizeURL($url = null, $locale = null) |
|
| 275 | |||
| 276 | /** |
||
| 277 | * It returns an URL without locale (if it has it). |
||
| 278 | * |
||
| 279 | * @param string|null $url |
||
| 280 | * |
||
| 281 | * @return string |
||
| 282 | */ |
||
| 283 | 106 | public function getNonLocalizedURL($url = null) |
|
| 287 | |||
| 288 | /** |
||
| 289 | * Returns an URL adapted to $locale or current locale. |
||
| 290 | * |
||
| 291 | * @todo: Refactor this beast |
||
| 292 | * |
||
| 293 | * @param string|null $locale |
||
| 294 | * @param string|null $url |
||
| 295 | * @param array $attributes |
||
| 296 | * @param bool|false $showHiddenLocale |
||
| 297 | * |
||
| 298 | * @return string|false |
||
| 299 | */ |
||
| 300 | 106 | public function getLocalizedURL($locale = null, $url = null, array $attributes = [], $showHiddenLocale = false) |
|
| 301 | { |
||
| 302 | 106 | if (is_null($locale)) |
|
| 303 | 10 | $locale = $this->getCurrentLocale(); |
|
| 304 | |||
| 305 | 106 | $this->isLocaleSupportedOrFail($locale); |
|
| 306 | |||
| 307 | 106 | if (empty($attributes)) |
|
| 308 | 106 | $attributes = Url::extractAttributes($url); |
|
| 309 | |||
| 310 | 106 | if (empty($url)) { |
|
| 311 | 10 | if ($this->routeTranslator->hasCurrentRoute()) { |
|
| 312 | 2 | if (empty($attributes)) |
|
| 313 | 2 | $attributes = $this->request()->route()->parameters(); |
|
| 314 | |||
| 315 | 2 | return $this->getUrlFromRouteName( |
|
| 316 | 1 | $locale, |
|
| 317 | 2 | $this->routeTranslator->getCurrentRoute(), |
|
| 318 | 1 | $attributes, |
|
| 319 | 1 | $showHiddenLocale |
|
| 320 | ); |
||
| 321 | } |
||
| 322 | |||
| 323 | 10 | $url = $this->request()->fullUrl(); |
|
| 324 | } |
||
| 325 | |||
| 326 | if ( |
||
| 327 | 106 | $locale && |
|
| 328 | 106 | $translatedRoute = $this->findTranslatedRouteByUrl($url, $attributes, $this->getCurrentLocale()) |
|
| 329 | ) { |
||
| 330 | 52 | return $this->getUrlFromRouteName($locale, $translatedRoute, $attributes, $showHiddenLocale); |
|
| 331 | } |
||
| 332 | |||
| 333 | 106 | $baseUrl = $this->request()->getBaseUrl(); |
|
| 334 | 106 | $parsedUrl = parse_url($url); |
|
| 335 | |||
| 336 | 106 | $translatedRoute = $this->routeTranslator->getTranslatedRoute( |
|
| 337 | 106 | $baseUrl, $parsedUrl, $this->getDefaultLocale(), $this->getSupportedLocales() |
|
| 338 | ); |
||
| 339 | |||
| 340 | 106 | if ($translatedRoute !== false) |
|
| 341 | 104 | return $this->getUrlFromRouteName($locale, $translatedRoute, $attributes, $showHiddenLocale); |
|
| 342 | |||
| 343 | 90 | if ( ! empty($locale)) { |
|
| 344 | 56 | if ($locale !== $this->getDefaultLocale() || ! $this->isDefaultLocaleHiddenInUrl() || $showHiddenLocale) { |
|
| 345 | 52 | $parsedUrl['path'] = $locale.'/'.ltrim($parsedUrl['path'], '/'); |
|
| 346 | } |
||
| 347 | } |
||
| 348 | |||
| 349 | 90 | $parsedUrl['path'] = ltrim(ltrim($baseUrl, '/') . '/' . $parsedUrl['path'], '/'); |
|
| 350 | 90 | $parsedUrl['path'] = rtrim($parsedUrl['path'], '/'); |
|
| 351 | |||
| 352 | 90 | $url = Url::unparse($parsedUrl); |
|
| 353 | |||
| 354 | 90 | if (filter_var($url, FILTER_VALIDATE_URL)) return $url; |
|
| 355 | |||
| 356 | 2 | return $this->createUrlFromUri( |
|
| 357 | 2 | empty($url) ? $parsedUrl['path'] : $url |
|
| 358 | ); |
||
| 359 | } |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Create an url from the uri. |
||
| 363 | * |
||
| 364 | * @param string $uri |
||
| 365 | * |
||
| 366 | * @return string |
||
| 367 | */ |
||
| 368 | 110 | public function createUrlFromUri($uri) |
|
| 369 | { |
||
| 370 | 110 | $uri = ltrim($uri, '/'); |
|
| 371 | |||
| 372 | 110 | return empty($this->baseUrl) |
|
| 373 | ? $this->app[\Illuminate\Contracts\Routing\UrlGenerator::class]->to($uri) |
||
| 374 | 110 | : $this->baseUrl.$uri; |
|
| 375 | } |
||
| 376 | |||
| 377 | /** |
||
| 378 | * Get locales navigation bar. |
||
| 379 | * |
||
| 380 | * @return string |
||
| 381 | */ |
||
| 382 | 2 | public function localesNavbar() |
|
| 390 | |||
| 391 | /* ----------------------------------------------------------------- |
||
| 392 | | Translation Methods |
||
| 393 | | ----------------------------------------------------------------- |
||
| 394 | */ |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Returns the translated route for an url and the attributes given and a locale |
||
| 398 | * |
||
| 399 | * @param string $url |
||
| 400 | * @param array $attributes |
||
| 401 | * @param string $locale |
||
| 402 | * |
||
| 403 | * @return string|false |
||
| 404 | */ |
||
| 405 | 104 | private function findTranslatedRouteByUrl($url, $attributes, $locale) |
|
| 406 | { |
||
| 407 | 104 | if (empty($url)) return false; |
|
| 408 | |||
| 409 | // check if this url is a translated url |
||
| 410 | 104 | foreach ($this->routeTranslator->getTranslatedRoutes() as $translatedRoute) { |
|
| 411 | 104 | $translatedUrl = $this->getUrlFromRouteName($locale, $translatedRoute, $attributes); |
|
| 412 | |||
| 413 | 104 | if ($this->getNonLocalizedURL($translatedUrl) === $this->getNonLocalizedURL($url)) |
|
| 414 | 104 | return $translatedRoute; |
|
| 415 | } |
||
| 416 | |||
| 417 | 56 | return false; |
|
| 418 | } |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Returns an URL adapted to the route name and the locale given. |
||
| 422 | * |
||
| 423 | * @param string|bool $locale |
||
| 424 | * @param string $transKey |
||
| 425 | * @param array $attributes |
||
| 426 | * @param bool|false $showHiddenLocale |
||
| 427 | * |
||
| 428 | * @return string|false |
||
| 429 | */ |
||
| 430 | 110 | public function getUrlFromRouteName($locale, $transKey, array $attributes = [], $showHiddenLocale = false) |
|
| 448 | |||
| 449 | /** |
||
| 450 | * Set route name from request. |
||
| 451 | * |
||
| 452 | * @param \Illuminate\Http\Request $request |
||
| 453 | */ |
||
| 454 | 2 | public function setRouteNameFromRequest(Request $request) |
|
| 455 | { |
||
| 456 | 2 | $routeName = $this->routeTranslator->getRouteNameFromPath( |
|
| 457 | 2 | $request->getUri(), $this->getCurrentLocale() |
|
| 458 | ); |
||
| 459 | |||
| 460 | 2 | $this->routeTranslator->setCurrentRoute($routeName); |
|
| 461 | 2 | } |
|
| 462 | |||
| 463 | /* ----------------------------------------------------------------- |
||
| 464 | | Check Methods |
||
| 465 | | ----------------------------------------------------------------- |
||
| 466 | */ |
||
| 467 | |||
| 468 | /** |
||
| 469 | * Hide the default locale in URL ?? |
||
| 470 | * |
||
| 471 | * @return bool |
||
| 472 | */ |
||
| 473 | 108 | public function isDefaultLocaleHiddenInUrl() |
|
| 477 | |||
| 478 | /** |
||
| 479 | * Check if Locale exists on the supported locales collection. |
||
| 480 | * |
||
| 481 | * @param string|bool $locale |
||
| 482 | * |
||
| 483 | * @return bool |
||
| 484 | */ |
||
| 485 | 112 | public function isLocaleSupported($locale) |
|
| 489 | |||
| 490 | /** |
||
| 491 | * Check if the locale is supported or fail if not. |
||
| 492 | * |
||
| 493 | * @param string $locale |
||
| 494 | * |
||
| 495 | * @throws \Arcanedev\Localization\Exceptions\UnsupportedLocaleException |
||
| 496 | */ |
||
| 497 | 112 | private function isLocaleSupportedOrFail($locale) |
|
| 504 | } |
||
| 505 |