| Conditions | 21 |
| Paths | 389 |
| Total Lines | 109 |
| Code Lines | 58 |
| 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 |
||
| 31 | public function onKernelRequest(RequestEvent $event) |
||
| 32 | { |
||
| 33 | $request = $event->getRequest(); |
||
| 34 | if (!$request->hasPreviousSession()) { |
||
| 35 | return; |
||
| 36 | } |
||
| 37 | $installed = 1 === (int) $this->parameterBag->get('installed'); |
||
| 38 | |||
| 39 | if (!$installed) { |
||
| 40 | return; |
||
| 41 | } |
||
| 42 | |||
| 43 | if (false === $request->hasSession()) { |
||
| 44 | return; |
||
| 45 | } |
||
| 46 | |||
| 47 | $loadFromDb = $request->getSession()->get('check_locale_from_db', true); |
||
| 48 | |||
| 49 | if (false === $loadFromDb && |
||
| 50 | $request->getSession()->has('_locale') && |
||
| 51 | !empty($request->getSession()->get('_locale')) |
||
| 52 | ) { |
||
| 53 | $locale = $request->getSession()->get('_locale'); |
||
| 54 | $request->setLocale($locale); |
||
| 55 | |||
| 56 | return true; |
||
| 57 | } |
||
| 58 | |||
| 59 | // Try to see if the locale has been set as a _locale routing parameter (from lang switcher) |
||
| 60 | //if ($locale = $request->getSession('_locale')) { |
||
| 61 | if ($loadFromDb) { |
||
| 62 | $localeList = []; |
||
| 63 | |||
| 64 | // 1. Check platform locale |
||
| 65 | $platformLocale = $this->settingsManager->getSetting('language.platform_language'); |
||
| 66 | |||
| 67 | if (!empty($platformLocale)) { |
||
| 68 | $localeList['platform_lang'] = $platformLocale; |
||
| 69 | } |
||
| 70 | // 2. Check user locale |
||
| 71 | // _locale_user is set when user logins the system check UserLocaleListener |
||
| 72 | $userLocale = $request->getSession()->get('_locale_user'); |
||
| 73 | |||
| 74 | if (!empty($userLocale)) { |
||
| 75 | $localeList['user_profil_lang'] = $userLocale; |
||
| 76 | } |
||
| 77 | |||
| 78 | // 3. Check course locale |
||
| 79 | $courseId = $request->get('cid'); |
||
| 80 | |||
| 81 | if (!empty($courseId)) { |
||
| 82 | /** @var Course $course */ |
||
| 83 | $course = $request->getSession()->get('course'); |
||
| 84 | // 3. Check course locale |
||
| 85 | /** @var Course $course */ |
||
| 86 | if (!empty($course)) { |
||
| 87 | $courseLocale = $course->getCourseLanguage(); |
||
| 88 | if (!empty($courseLocale)) { |
||
| 89 | $localeList['course_lang'] = $platformLocale; |
||
| 90 | } |
||
| 91 | } |
||
| 92 | } |
||
| 93 | |||
| 94 | // 4. force locale if it was selected from the URL |
||
| 95 | $localeFromUrl = $request->get('_locale'); |
||
| 96 | if (!empty($localeFromUrl)) { |
||
| 97 | $localeList['user_selected_lang'] = $platformLocale; |
||
| 98 | } |
||
| 99 | |||
| 100 | $priorityList = [ |
||
| 101 | 'language_priority_4', |
||
| 102 | 'language_priority_3', |
||
| 103 | 'language_priority_2', |
||
| 104 | 'language_priority_1', |
||
| 105 | ]; |
||
| 106 | |||
| 107 | $locale = ''; |
||
| 108 | foreach ($priorityList as $setting) { |
||
| 109 | $priority = $this->settingsManager->getSetting("language.$setting"); |
||
| 110 | if (!empty($priority) && isset($localeList[$priority])) { |
||
| 111 | $locale = $localeList[$priority]; |
||
| 112 | |||
| 113 | break; |
||
| 114 | } |
||
| 115 | } |
||
| 116 | |||
| 117 | if (empty($locale)) { |
||
| 118 | // Use default order |
||
| 119 | $priorityList = [ |
||
| 120 | 'platform_lang', |
||
| 121 | 'user_profil_lang', |
||
| 122 | 'course_lang', |
||
| 123 | 'user_selected_lang', |
||
| 124 | ]; |
||
| 125 | foreach ($priorityList as $setting) { |
||
| 126 | if (isset($localeList[$setting])) { |
||
| 127 | $locale = $localeList[$setting]; |
||
| 128 | } |
||
| 129 | } |
||
| 130 | } |
||
| 131 | |||
| 132 | if (empty($locale)) { |
||
| 133 | $locale = $this->defaultLocale; |
||
| 134 | } |
||
| 135 | |||
| 136 | // if no explicit locale has been set on this request, use one from the session |
||
| 137 | $request->setLocale($locale); |
||
| 138 | $request->getSession()->set('_locale', $locale); |
||
| 139 | $request->getSession()->set('check_locale_from_db', false); |
||
| 140 | } |
||
| 151 |