| Conditions | 14 |
| Paths | 384 |
| Total Lines | 76 |
| Code Lines | 40 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 75 | public function getCurrentLanguage(Request $request): string |
||
| 76 | { |
||
| 77 | $localeList = []; |
||
| 78 | |||
| 79 | // 1. Check platform locale |
||
| 80 | $platformLocale = $this->settingsManager->getSetting('language.platform_language'); |
||
| 81 | |||
| 82 | if (!empty($platformLocale)) { |
||
| 83 | $localeList['platform_lang'] = $platformLocale; |
||
| 84 | } |
||
| 85 | // 2. Check user locale |
||
| 86 | // _locale_user is set when user logins the system check UserLocaleListener |
||
| 87 | $userLocale = $request->getSession()->get('_locale_user'); |
||
| 88 | |||
| 89 | if (!empty($userLocale)) { |
||
| 90 | $localeList['user_profil_lang'] = $userLocale; |
||
| 91 | } |
||
| 92 | |||
| 93 | // 3. Check course locale |
||
| 94 | $courseId = $request->get('cid'); |
||
| 95 | |||
| 96 | if (!empty($courseId)) { |
||
| 97 | /** @var Course $course */ |
||
| 98 | $course = $request->getSession()->get('course'); |
||
| 99 | // 3. Check course locale |
||
| 100 | if (!empty($course)) { |
||
| 101 | $courseLocale = $course->getCourseLanguage(); |
||
| 102 | if (!empty($courseLocale)) { |
||
| 103 | $localeList['course_lang'] = $platformLocale; |
||
| 104 | } |
||
| 105 | } |
||
| 106 | } |
||
| 107 | |||
| 108 | // 4. force locale if it was selected from the URL |
||
| 109 | $localeFromUrl = $request->get('_locale'); |
||
| 110 | if (!empty($localeFromUrl)) { |
||
| 111 | $localeList['user_selected_lang'] = $platformLocale; |
||
| 112 | } |
||
| 113 | |||
| 114 | $priorityList = [ |
||
| 115 | 'language_priority_4', |
||
| 116 | 'language_priority_3', |
||
| 117 | 'language_priority_2', |
||
| 118 | 'language_priority_1', |
||
| 119 | ]; |
||
| 120 | |||
| 121 | $locale = ''; |
||
| 122 | foreach ($priorityList as $setting) { |
||
| 123 | $priority = $this->settingsManager->getSetting(sprintf('language.%s', $setting)); |
||
| 124 | if (!empty($priority) && isset($localeList[$priority])) { |
||
| 125 | $locale = $localeList[$priority]; |
||
| 126 | |||
| 127 | break; |
||
| 128 | } |
||
| 129 | } |
||
| 130 | |||
| 131 | if (empty($locale)) { |
||
| 132 | // Use default order |
||
| 133 | $priorityList = [ |
||
| 134 | 'platform_lang', |
||
| 135 | 'user_profil_lang', |
||
| 136 | 'course_lang', |
||
| 137 | 'user_selected_lang', |
||
| 138 | ]; |
||
| 139 | foreach ($priorityList as $setting) { |
||
| 140 | if (isset($localeList[$setting])) { |
||
| 141 | $locale = $localeList[$setting]; |
||
| 142 | } |
||
| 143 | } |
||
| 144 | } |
||
| 145 | |||
| 146 | if (empty($locale)) { |
||
| 147 | $locale = $this->defaultLocale; |
||
| 148 | } |
||
| 149 | |||
| 150 | return $locale; |
||
| 151 | } |
||
| 161 |