| Total Complexity | 40 |
| Total Lines | 275 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 1 |
Complex classes like LangHelper 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 LangHelper, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | class LangHelper |
||
| 21 | { |
||
| 22 | use Configurable; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * The default key for global translation |
||
| 26 | */ |
||
| 27 | const GLOBAL_ENTITY = 'Global'; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Provision fluent locales defined in yml |
||
| 31 | * Use LangHelper::provisionLocales |
||
| 32 | * |
||
| 33 | * eg: |
||
| 34 | * LeKoala\Base\i18n\BaseI18n: |
||
| 35 | * default_locales: |
||
| 36 | * - en_US |
||
| 37 | * - fr_FR |
||
| 38 | * @config |
||
| 39 | * @var array |
||
| 40 | */ |
||
| 41 | private static $default_locales = []; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @config |
||
| 45 | * @var boolean |
||
| 46 | */ |
||
| 47 | private static $persist_cookie = true; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var array |
||
| 51 | */ |
||
| 52 | protected static $locale_cache = []; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Get a global translation |
||
| 56 | * |
||
| 57 | * By default all global translation are stored under the Global key |
||
| 58 | * |
||
| 59 | * @param string $entity If no entity is specified, Global is assumed |
||
| 60 | * @return string |
||
| 61 | */ |
||
| 62 | public static function globalTranslation($entity) |
||
| 63 | { |
||
| 64 | $parts = explode('.', $entity); |
||
| 65 | if (count($parts) == 1) { |
||
| 66 | array_unshift($parts, self::GLOBAL_ENTITY); |
||
| 67 | } |
||
| 68 | return i18n::_t(implode('.', $parts), $entity); |
||
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Call this to make sure we are not setting any cookies that has |
||
| 73 | * not been accepted |
||
| 74 | * |
||
| 75 | * @return void |
||
| 76 | */ |
||
| 77 | public static function persistLocaleIfCookiesAreAllowed() |
||
| 78 | { |
||
| 79 | if (headers_sent()) { |
||
| 80 | return; |
||
| 81 | } |
||
| 82 | |||
| 83 | $class = \TractorCow\Fluent\Middleware\DetectLocaleMiddleware::class; |
||
| 84 | if (!class_exists($class)) { |
||
| 85 | return; |
||
| 86 | } |
||
| 87 | |||
| 88 | $persist = static::config()->persist_cookie; |
||
| 89 | if (!$persist) { |
||
| 90 | return; |
||
| 91 | } |
||
| 92 | |||
| 93 | // If we choose to persist cookies, we should check our cookie consent first |
||
| 94 | $dont_persist = true; |
||
| 95 | // cookie consent from osano |
||
| 96 | $status = Cookie::get('cookieconsent_status'); |
||
| 97 | if (strlen($status) && $status == 'allow') { |
||
| 98 | $dont_persist = false; |
||
| 99 | } |
||
| 100 | // cookie from cookieconsent |
||
| 101 | $status = Cookie::get('cookie_consent_user_accepted'); |
||
| 102 | if (strlen($status) && $status == 'true') { |
||
| 103 | $dont_persist = false; |
||
| 104 | } |
||
| 105 | |||
| 106 | if ($dont_persist) { |
||
| 107 | return; |
||
| 108 | } |
||
| 109 | |||
| 110 | // Persist according to fluent settings |
||
| 111 | $curr = Controller::curr(); |
||
| 112 | $request = $curr->getRequest(); |
||
| 113 | |||
| 114 | $secure = Director::is_https($request) |
||
| 115 | && Session::config()->get('cookie_secure'); |
||
| 116 | |||
| 117 | $persistIds = $class::config()->get('persist_ids'); |
||
| 118 | $persistKey = FluentState::singleton()->getIsFrontend() |
||
| 119 | ? $persistIds['frontend'] |
||
| 120 | : $persistIds['cms']; |
||
| 121 | |||
| 122 | $locale = $request->getSession()->get($persistKey); |
||
| 123 | // If session is not started or set, it may not be set |
||
| 124 | if (!$locale) { |
||
| 125 | $locale = self::get_locale(); |
||
| 126 | } |
||
| 127 | |||
| 128 | Cookie::set( |
||
| 129 | $persistKey, |
||
| 130 | $locale, |
||
| 131 | $class::config()->get('persist_cookie_expiry'), |
||
| 132 | $class::config()->get('persist_cookie_path'), |
||
| 133 | $class::config()->get('persist_cookie_domain'), |
||
| 134 | $secure, |
||
| 135 | $class::config()->get('persist_cookie_http_only') |
||
| 136 | ); |
||
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Provision locales defined in default_locales |
||
| 141 | * |
||
| 142 | * @return void |
||
| 143 | */ |
||
| 144 | public static function provisionLocales() |
||
| 145 | { |
||
| 146 | $locales = self::config()->default_locales; |
||
| 147 | if (empty($locales)) { |
||
| 148 | throw new Exception("No locales defined in BaseI18n:default_locales"); |
||
| 149 | } |
||
| 150 | |||
| 151 | foreach ($locales as $loc) { |
||
| 152 | $Locale = Locale::get()->filter('Locale', $loc)->first(); |
||
| 153 | $allLocales = i18n::getData()->getLocales(); |
||
| 154 | if (!$Locale) { |
||
| 155 | $Locale = new Locale(); |
||
| 156 | $Locale->Title = $allLocales[$loc]; |
||
| 157 | $Locale->Locale = $loc; |
||
| 158 | $Locale->URLSegment = self::get_lang($loc); |
||
| 159 | $Locale->IsGlobalDefault = $loc == i18n::get_locale(); |
||
| 160 | $Locale->write(); |
||
| 161 | } |
||
| 162 | } |
||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Make sure we get a proper two characters lang |
||
| 167 | * |
||
| 168 | * @param string|object $lang a string or a fluent locale object |
||
| 169 | * @return string a two chars lang |
||
| 170 | */ |
||
| 171 | public static function get_lang($lang = null) |
||
| 180 | } |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Get the right locale (using fluent data if exists) |
||
| 184 | * |
||
| 185 | * @return string |
||
| 186 | */ |
||
| 187 | public static function get_locale() |
||
| 188 | { |
||
| 189 | if (class_exists(FluentState::class)) { |
||
| 190 | $locale = FluentState::singleton()->getLocale(); |
||
| 191 | // Locale may not be set, in tests for instance |
||
| 192 | if ($locale) { |
||
| 193 | return $locale; |
||
| 194 | } |
||
| 195 | } |
||
| 196 | return i18n::get_locale(); |
||
| 197 | } |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Get a locale from the lang |
||
| 201 | * |
||
| 202 | * @param string $lang |
||
| 203 | * @return string |
||
| 204 | */ |
||
| 205 | public static function get_locale_from_lang($lang) |
||
| 206 | { |
||
| 207 | // Use fluent data |
||
| 208 | if (class_exists(Locale::class)) { |
||
| 209 | if (empty(self::$locale_cache)) { |
||
| 210 | $fluentLocales = Locale::getLocales(); |
||
| 211 | foreach ($fluentLocales as $locale) { |
||
| 212 | self::$locale_cache[self::get_lang($locale->Locale)] = $locale->Locale; |
||
| 213 | } |
||
| 214 | } |
||
| 215 | if (isset(self::$locale_cache[$lang])) { |
||
| 216 | return self::$locale_cache[$lang]; |
||
| 217 | } |
||
| 218 | } |
||
| 219 | // Guess |
||
| 220 | $localesData = i18n::getData(); |
||
| 221 | return $localesData->localeFromLang($lang); |
||
| 222 | } |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Do we have the subsite module installed |
||
| 226 | * TODO: check if it might be better to use module manifest instead? |
||
| 227 | * |
||
| 228 | * @return bool |
||
| 229 | */ |
||
| 230 | public static function usesFluent() |
||
| 231 | { |
||
| 232 | return class_exists(FluentState::class); |
||
| 233 | } |
||
| 234 | |||
| 235 | /** |
||
| 236 | * @return array |
||
| 237 | */ |
||
| 238 | public static function get_available_langs() |
||
| 239 | { |
||
| 240 | if (!self::usesFluent()) { |
||
| 241 | return [ |
||
| 242 | self::get_lang() |
||
| 243 | ]; |
||
| 244 | } |
||
| 245 | |||
| 246 | $allLocales = Locale::get(); |
||
| 247 | $results = []; |
||
| 248 | foreach ($allLocales as $locale) { |
||
| 249 | $results[] = $locale->URLSegment; |
||
| 250 | } |
||
| 251 | return $results; |
||
| 252 | } |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Execute the callback in given subsite |
||
| 256 | * |
||
| 257 | * @param string $locale |
||
| 258 | * @param callable $cb |
||
| 259 | * @return mixed the callback result |
||
| 260 | */ |
||
| 261 | public static function withLocale($locale, $cb) |
||
| 274 | }); |
||
| 275 | } |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Execute the callback for all locales |
||
| 279 | * |
||
| 280 | * @param callable $cb |
||
| 281 | * @return array an array of callback results |
||
| 282 | */ |
||
| 283 | public static function withLocales($cb) |
||
| 295 | } |
||
| 296 | } |
||
| 297 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths