Conditions | 16 |
Paths | 384 |
Total Lines | 72 |
Code Lines | 37 |
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 |
||
66 | public function getCurrentLanguage(Request $request): string |
||
67 | { |
||
68 | $sessionHandler = $request->getSession(); |
||
69 | $localeList = []; |
||
70 | |||
71 | // 1. Check platform locale; |
||
72 | if ($platformLocale = $this->settingsManager->getSetting('language.platform_language')) { |
||
73 | $localeList['platform_lang'] = $platformLocale; |
||
74 | } |
||
75 | |||
76 | // 2. Check user locale |
||
77 | // _locale_user is set when user logins the system check UserLocaleListener |
||
78 | if ($userLocale = $sessionHandler->get('_locale_user')) { |
||
79 | $localeList['user_profil_lang'] = $userLocale; |
||
80 | } |
||
81 | |||
82 | // 3. Check course locale |
||
83 | if ($request->query->getInt('cid') |
||
84 | || $request->request->getInt('cid') |
||
85 | || $request->attributes->getInt('cid') |
||
86 | ) { |
||
87 | /** @var Course|null $course */ |
||
88 | // 3. Check course locale |
||
89 | if ($course = $sessionHandler->get('course')) { |
||
90 | if ($courseLocale = $course->getCourseLanguage()) { |
||
91 | $localeList['course_lang'] = $courseLocale; |
||
92 | } |
||
93 | } |
||
94 | } |
||
95 | |||
96 | // 4. force locale if it was selected from the URL |
||
97 | if ($localeFromUrl = $sessionHandler->get('_selected_locale')) { |
||
98 | $localeList['user_selected_lang'] = $localeFromUrl; |
||
99 | } |
||
100 | |||
101 | $priorityList = [ |
||
102 | 'language_priority_1', |
||
103 | 'language_priority_2', |
||
104 | 'language_priority_3', |
||
105 | 'language_priority_4', |
||
106 | ]; |
||
107 | |||
108 | $locale = ''; |
||
109 | foreach ($priorityList as $setting) { |
||
110 | $priority = $this->settingsManager->getSetting(sprintf('language.%s', $setting)); |
||
111 | if (!empty($priority) && !empty($localeList[$priority])) { |
||
112 | $locale = $localeList[$priority]; |
||
113 | |||
114 | break; |
||
115 | } |
||
116 | } |
||
117 | |||
118 | if (empty($locale)) { |
||
119 | // Use default order |
||
120 | $priorityList = [ |
||
121 | 'platform_lang', |
||
122 | 'user_profil_lang', |
||
123 | 'course_lang', |
||
124 | 'user_selected_lang', |
||
125 | ]; |
||
126 | foreach ($priorityList as $setting) { |
||
127 | if (!empty($localeList[$setting])) { |
||
128 | $locale = $localeList[$setting]; |
||
129 | } |
||
130 | } |
||
131 | } |
||
132 | |||
133 | if (empty($locale)) { |
||
134 | $locale = $this->defaultLocale; |
||
135 | } |
||
136 | |||
137 | return $locale; |
||
138 | } |
||
148 |