| Total Complexity | 40 |
| Total Lines | 260 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Tongue 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.
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 Tongue, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | class Tongue |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * Our instance of the Laravel app. |
||
| 15 | * |
||
| 16 | * @var Illuminate\Foundation\Application |
||
|
|
|||
| 17 | */ |
||
| 18 | protected $app = ''; |
||
| 19 | |||
| 20 | public function __construct(Application $app) |
||
| 21 | { |
||
| 22 | $this->app = $app; |
||
| 23 | } |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Detects the tongue, the locale |
||
| 27 | * of the User. |
||
| 28 | * |
||
| 29 | * @return Tongue :P |
||
| 30 | */ |
||
| 31 | public function detect() |
||
| 32 | { |
||
| 33 | $locale = $this->findLocale(); |
||
| 34 | |||
| 35 | $this->speaks($locale); |
||
| 36 | |||
| 37 | return $this; |
||
| 38 | } |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Gets the current speaking tongue... |
||
| 42 | * (language code). |
||
| 43 | * |
||
| 44 | * @return string |
||
| 45 | */ |
||
| 46 | public function current($key = null) |
||
| 55 | } |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Gets the twist of the tongue. |
||
| 59 | * Return the direction left or right. |
||
| 60 | * e.g. for arabic language. |
||
| 61 | * |
||
| 62 | * @return string |
||
| 63 | */ |
||
| 64 | public function leftOrRight() |
||
| 75 | } |
||
| 76 | } |
||
| 77 | |||
| 78 | /** |
||
| 79 | * A tongue-twister is a phrase that is |
||
| 80 | * designed to be difficult to articulate properly, |
||
| 81 | * So lets just asume the user just can't speak the |
||
| 82 | * language... |
||
| 83 | * |
||
| 84 | * @return bool (yes if its not speakable) |
||
| 85 | */ |
||
| 86 | public function twister() |
||
| 87 | { |
||
| 88 | $locale = Localization::fromUrl(); |
||
| 89 | |||
| 90 | if (tongue()->speaking('subdomains', $locale)) { |
||
| 91 | //whitelisted subdomains! like admin.domain.com |
||
| 92 | return false; |
||
| 93 | } |
||
| 94 | |||
| 95 | //custom subdomains with locale. gewinnen.domain.com -> de as locale |
||
| 96 | if ($customLocale = tongue()->speaking('aliases', $locale)) { |
||
| 97 | //but we need to check again if it is spoken or not |
||
| 98 | return $this->current() != $customLocale; |
||
| 99 | } |
||
| 100 | |||
| 101 | //fallback language is the same as the current language |
||
| 102 | if (Config::beautify() && $this->current() === Config::fallbackLocale()) { |
||
| 103 | //didn't found locale means browser is set to exmaple.com |
||
| 104 | if (!$locale) { |
||
| 105 | return false; |
||
| 106 | } |
||
| 107 | //browser is set to en.example.com but should be forced back to example.com |
||
| 108 | if ($locale === Config::fallbackLocale()) { |
||
| 109 | return true; |
||
| 110 | } |
||
| 111 | } |
||
| 112 | |||
| 113 | //decipher from |
||
| 114 | return $this->current() != $locale; |
||
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * The user speaks locale language. |
||
| 119 | * Set the locale. |
||
| 120 | * |
||
| 121 | * @param string $locale |
||
| 122 | * @return Tongue :P |
||
| 123 | */ |
||
| 124 | public function speaks(string $locale) |
||
| 125 | { |
||
| 126 | if (!$this->isSpeaking($locale)) { |
||
| 127 | return abort(404); //oder error? |
||
| 128 | } |
||
| 129 | |||
| 130 | $this->app->setLocale($this->locale = $locale); |
||
| 131 | |||
| 132 | if ($locale != Localization::cookie() && Config::cookieLocalization()) { |
||
| 133 | Localization::cookie($locale); |
||
| 134 | } |
||
| 135 | |||
| 136 | // Regional locale such as de_DE, so formatLocalized works in Carbon |
||
| 137 | $regional = $this->speaking('regional', $locale); |
||
| 138 | |||
| 139 | if ($regional) { |
||
| 140 | setlocale(LC_TIME, $regional . '.UTF-8'); |
||
| 141 | setlocale(LC_MONETARY, $regional . '.UTF-8'); |
||
| 142 | } |
||
| 143 | |||
| 144 | return $this; |
||
| 145 | } |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Used to return back to previous url. |
||
| 149 | * e.g. if you change the language. its usefull. |
||
| 150 | * |
||
| 151 | * @return Illuminate\Routing\Redirector |
||
| 152 | */ |
||
| 153 | public function back() |
||
| 154 | { |
||
| 155 | return dialect()->redirect(dialect()->redirectUrl(url()->previous())); |
||
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Gets the collection list of all languages, |
||
| 160 | * the website speaks. Or give us the specific keys. |
||
| 161 | * |
||
| 162 | * @return collection|string|array|null |
||
| 163 | */ |
||
| 164 | public function speaking($key = null, $locale = null) |
||
| 165 | { |
||
| 166 | $locales = Config::supportedLocales(); |
||
| 167 | |||
| 168 | if (empty($locales) || !is_array($locales)) { |
||
| 169 | throw new SupportedLocalesNotDefined(); |
||
| 170 | } |
||
| 171 | |||
| 172 | if (!$key) { |
||
| 173 | return collect($locales); |
||
| 174 | } |
||
| 175 | |||
| 176 | if ($key === 'BCP47') { |
||
| 177 | return $this->BCP47($locale, $locales); |
||
| 178 | } |
||
| 179 | |||
| 180 | if ($key === 'subdomains') { |
||
| 181 | return $this->getSubdomains($locale); |
||
| 182 | } |
||
| 183 | |||
| 184 | if ($key === 'aliases') { |
||
| 185 | return $this->getAliases($locale); |
||
| 186 | } |
||
| 187 | |||
| 188 | if (!Arr::has($locales, "{$locale}.{$key}")) { |
||
| 189 | throw new SupportedLocalesNotDefined(); |
||
| 190 | } |
||
| 191 | |||
| 192 | return data_get($locales, "{$locale}.{$key}"); |
||
| 193 | } |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Finds the Subdomain in the URL. |
||
| 197 | * Like en, de... |
||
| 198 | * |
||
| 199 | * @return string |
||
| 200 | */ |
||
| 201 | protected function findLocale() |
||
| 202 | { |
||
| 203 | return Localization::decipherTongue(); |
||
| 204 | } |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Checks if your page is speaking the language. |
||
| 208 | * |
||
| 209 | * @param string $locale |
||
| 210 | * @return bool |
||
| 211 | */ |
||
| 212 | public function isSpeaking($locale) |
||
| 215 | } |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Gets the BCP 47 Value of the regional |
||
| 219 | * See for more: http://schneegans.de/lv/?tags=en&format=text. |
||
| 220 | * |
||
| 221 | * @param string $locale |
||
| 222 | * @param array $loacles [the list in the config file] |
||
| 223 | */ |
||
| 224 | protected function BCP47($locale, $locales) |
||
| 234 | } |
||
| 235 | |||
| 236 | /** |
||
| 237 | * @param string $subdomain [like "admin"] |
||
| 238 | * |
||
| 239 | * @return array|bool |
||
| 240 | */ |
||
| 241 | protected function getSubdomains(string $subdomain = null) |
||
| 248 | } |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Gets the array of the config, or gets the locale value of a subdomain. |
||
| 252 | * Like: "gewinnen" -> "de". |
||
| 253 | * |
||
| 254 | * @param string $subdomain |
||
| 255 | * |
||
| 256 | * @return array|string |
||
| 257 | */ |
||
| 258 | protected function getAliases(string $subdomain = null) |
||
| 273 |