LocaleSubscriber::getCurrentLanguage()   F
last analyzed

Complexity

Conditions 16
Paths 384

Size

Total Lines 72
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 16
eloc 37
nc 384
nop 1
dl 0
loc 72
rs 2.4333
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
3
declare(strict_types=1);
4
5
/* For licensing terms, see /license.txt */
6
7
namespace Chamilo\CoreBundle\EventSubscriber;
8
9
use Chamilo\CoreBundle\Entity\Course;
10
use Chamilo\CoreBundle\Settings\SettingsManager;
11
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
12
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
13
use Symfony\Component\HttpFoundation\Request;
14
use Symfony\Component\HttpKernel\Event\RequestEvent;
15
use Symfony\Component\HttpKernel\KernelEvents;
16
17
/**
18
 * Checks the portal listener depending of different settings:
19
 * platform, user, course.
20
 */
21
class LocaleSubscriber implements EventSubscriberInterface
22
{
23
    protected string $defaultLocale;
24
    protected ParameterBagInterface $parameterBag;
25
    protected SettingsManager $settingsManager;
26
27
    public function __construct(string $defaultLocale, SettingsManager $settingsManager, ParameterBagInterface $parameterBag)
28
    {
29
        $this->defaultLocale = $defaultLocale;
30
        $this->settingsManager = $settingsManager;
31
        $this->parameterBag = $parameterBag;
32
    }
33
34
    public function onKernelRequest(RequestEvent $event): void
35
    {
36
        $request = $event->getRequest();
37
        /*if (!$request->hasPreviousSession()) {
38
            return;
39
        }*/
40
41
        $installed = false;
42
        if ($this->parameterBag->has('installed')) {
43
            $installed = 1 === (int) $this->parameterBag->get('installed');
44
        }
45
46
        if (!$installed) {
47
            return;
48
        }
49
50
        if (!$request->hasSession()) {
51
            return;
52
        }
53
54
        $sessionHandler = $request->getSession();
55
56
        if ($attrLocale = $request->query->get('_locale')) {
57
            $sessionHandler->set('_selected_locale', $attrLocale);
58
        }
59
60
        $locale = $this->getCurrentLanguage($request);
61
        // if no explicit locale has been set on this request, use one from the session
62
        $request->setLocale($locale);
63
        $sessionHandler->set('_locale', $locale);
64
    }
65
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
    }
139
140
    public static function getSubscribedEvents(): array
141
    {
142
        return [
143
            // must be registered before the default Locale listener
144
            KernelEvents::REQUEST => [['onKernelRequest', 20]],
145
        ];
146
    }
147
}
148