Conditions | 13 |
Paths | 224 |
Total Lines | 56 |
Code Lines | 30 |
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 |
||
55 | public function getCurrentLanguage(Request $request): string |
||
56 | { |
||
57 | $sessionHandler = $request->getSession(); |
||
58 | $localeList = []; |
||
59 | |||
60 | // 1. Platform default locale |
||
61 | if ($platformLocale = $this->settingsManager->getSetting('language.platform_language')) { |
||
62 | $localeList['platform_lang'] = $platformLocale; |
||
63 | } |
||
64 | |||
65 | // 2. User profile locale from session |
||
66 | if ($userLocale = $sessionHandler->get('_locale_user')) { |
||
67 | $localeList['user_profil_lang'] = $userLocale; |
||
68 | } |
||
69 | |||
70 | // 3. Course locale or user locale if course allows user language |
||
71 | $course = $sessionHandler->get('course'); |
||
72 | if ($course instanceof Course) { |
||
73 | $userLocale = $localeList['user_profil_lang'] ?? null; |
||
74 | $courseLocale = $course->getCourseLanguage(); |
||
75 | |||
76 | $this->courseSettingsManager->setCourse($course); |
||
77 | if ($this->courseSettingsManager->getCourseSettingValue('show_course_in_user_language') === '1' && $userLocale) { |
||
78 | $localeList['course_lang'] = $userLocale; |
||
79 | } elseif ($courseLocale) { |
||
80 | $localeList['course_lang'] = $courseLocale; |
||
81 | } |
||
82 | } |
||
83 | |||
84 | // 4. Locale selected manually via URL |
||
85 | if ($localeFromUrl = $sessionHandler->get('_selected_locale')) { |
||
86 | $localeList['user_selected_lang'] = $localeFromUrl; |
||
87 | } |
||
88 | |||
89 | // 5. Resolve locale based on configured language priorities |
||
90 | foreach ([ |
||
91 | 'language_priority_1', |
||
92 | 'language_priority_2', |
||
93 | 'language_priority_3', |
||
94 | 'language_priority_4', |
||
95 | ] as $settingKey) { |
||
96 | $priority = $this->settingsManager->getSetting("language.$settingKey"); |
||
97 | if (!empty($priority) && !empty($localeList[$priority])) { |
||
98 | return $localeList[$priority]; |
||
99 | } |
||
100 | } |
||
101 | |||
102 | // 6. Fallback order if priorities are not defined |
||
103 | foreach (['platform_lang', 'user_profil_lang', 'course_lang', 'user_selected_lang'] as $key) { |
||
104 | if (!empty($localeList[$key])) { |
||
105 | return $localeList[$key]; |
||
106 | } |
||
107 | } |
||
108 | |||
109 | // 7. Final fallback to system default |
||
110 | return $this->defaultLocale; |
||
111 | } |
||
121 |