Conditions | 18 |
Paths | 1568 |
Total Lines | 80 |
Code Lines | 41 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 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 | $session = $request->getSession(); |
||
58 | $localeList = []; |
||
59 | |||
60 | // 1) Platform default |
||
61 | if ($platformLocale = $this->settingsManager->getSetting('language.platform_language')) { |
||
62 | $localeList['platform_lang'] = $platformLocale; |
||
63 | } |
||
64 | |||
65 | // 2) User profile (from session) |
||
66 | if ($userLocale = $session->get('_locale_user')) { |
||
67 | $localeList['user_profil_lang'] = $userLocale; |
||
68 | } |
||
69 | |||
70 | // 3) Course language, or user language if course allows it |
||
71 | // First try: course already in session |
||
72 | $course = $session->get('course'); |
||
73 | |||
74 | // Fallback: resolve course from request if not in session yet |
||
75 | if (!$course instanceof Course) { |
||
76 | // Accept both numeric id (?cid=123) and code (?cid=ABC) as well as legacy ?cidReq=CODE |
||
77 | $cid = $request->query->get('cid'); |
||
78 | $cidReq = $request->query->get('cidReq'); |
||
79 | |||
80 | if ($cid) { |
||
81 | if (ctype_digit((string) $cid)) { |
||
82 | $course = $this->em->getRepository(Course::class)->find((int) $cid); |
||
83 | } else { |
||
84 | $course = $this->em->getRepository(Course::class)->findOneBy(['code' => (string) $cid]); |
||
85 | } |
||
86 | } |
||
87 | |||
88 | if (!$course && $cidReq) { |
||
89 | $course = $this->em->getRepository(Course::class)->findOneBy(['code' => (string) $cidReq]); |
||
90 | } |
||
91 | } |
||
92 | |||
93 | if ($course instanceof Course) { |
||
94 | $userLocale = $localeList['user_profil_lang'] ?? null; |
||
95 | $courseLocale = $course->getCourseLanguage(); |
||
96 | |||
97 | // The per-course setting decides whether to use user language |
||
98 | $this->courseSettingsManager->setCourse($course); |
||
99 | $allowUser = $this->courseSettingsManager->getCourseSettingValue('show_course_in_user_language') === '1'; |
||
100 | |||
101 | if ($allowUser && $userLocale) { |
||
102 | $localeList['course_lang'] = $userLocale; |
||
103 | } elseif ($courseLocale) { |
||
104 | $localeList['course_lang'] = $courseLocale; |
||
105 | } |
||
106 | } |
||
107 | |||
108 | // 4) Explicit selection via URL (?_locale=xx) saved earlier in session |
||
109 | if ($selected = $session->get('_selected_locale')) { |
||
110 | $localeList['user_selected_lang'] = $selected; |
||
111 | } |
||
112 | |||
113 | // 5) Honor configured priorities language_priority_1..4 |
||
114 | foreach ([ |
||
115 | 'language_priority_1', |
||
116 | 'language_priority_2', |
||
117 | 'language_priority_3', |
||
118 | 'language_priority_4', |
||
119 | ] as $settingKey) { |
||
120 | $priority = $this->settingsManager->getSetting("language.$settingKey"); |
||
121 | if (!empty($priority) && !empty($localeList[$priority])) { |
||
122 | return $localeList[$priority]; |
||
123 | } |
||
124 | } |
||
125 | |||
126 | // 6) Fallback order when priorities are absent |
||
127 | foreach (['platform_lang', 'user_profil_lang', 'course_lang', 'user_selected_lang'] as $key) { |
||
128 | if (!empty($localeList[$key])) { |
||
129 | return $localeList[$key]; |
||
130 | } |
||
131 | } |
||
132 | |||
133 | // 7) Final fallback to system default |
||
134 | return $this->defaultLocale; |
||
135 | } |
||
145 |