Passed
Pull Request — main (#308)
by Paul
13:29 queued 06:46
created

LocaleListener   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 52
rs 10
c 0
b 0
f 0
wmc 6
1
<?php
2
3
/**
4
 * Copyright 2014 SURFnet bv
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
Coding Style introduced by
Missing @package tag in file comment
Loading history...
Coding Style introduced by
Missing @author tag in file comment
Loading history...
Coding Style introduced by
Missing @license tag in file comment
Loading history...
Coding Style introduced by
Missing @link tag in file comment
Loading history...
18
19
namespace Surfnet\StepupSelfService\SelfServiceBundle\EventListener;
20
21
use Surfnet\StepupMiddlewareClientBundle\Identity\Dto\Identity;
22
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
23
use Symfony\Component\HttpKernel\Event\RequestEvent;
24
use Symfony\Component\HttpKernel\KernelEvents;
25
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
26
use Symfony\Contracts\Translation\TranslatorInterface;
27
28
final readonly class LocaleListener implements EventSubscriberInterface
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class LocaleListener
Loading history...
Bug introduced by
A parse error occurred: Syntax error, unexpected T_READONLY, expecting T_CLASS on line 28 at column 6
Loading history...
29
{
30
    public function __construct(private TokenStorageInterface $tokenStorage, private TranslatorInterface $translator)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __construct()
Loading history...
31
    {
32
    }
33
34
    public function setRequestLocale(RequestEvent $event): void
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function setRequestLocale()
Loading history...
35
    {
36
        $token = $this->tokenStorage->getToken();
37
38
        if (!$token instanceof \Symfony\Component\Security\Core\Authentication\Token\TokenInterface) {
39
            return;
40
        }
41
42
        $identity = $token->getUser();
43
44
        // Anonymous usage like /authentication/metadata has just "anonymous" as identity.
45
        if (!$identity instanceof Identity) {
46
            return;
47
        }
48
49
        $request = $event->getRequest();
50
        $request->setLocale($identity->preferredLocale);
51
52
        // As per \Symfony\Component\HttpKernel\EventListener\TranslatorListener::setLocale()
53
        try {
54
            $this->translator->setLocale($request->getLocale());
55
        } catch (\InvalidArgumentException) {
56
            $this->translator->setLocale($request->getDefaultLocale());
57
        }
58
    }
59
60
    public static function getSubscribedEvents(): array
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function getSubscribedEvents()
Loading history...
61
    {
62
        return [
63
            // Default locale listener listens at P16
64
            // Translator listener, which sets the locale for the translator, listens at P10
65
            // The firewall, which makes the token available, listens at P8
66
            // We must jump in after the firewall, forcing us to overwrite the translator locale.
67
            KernelEvents::REQUEST => ['setRequestLocale', 7],
68
        ];
69
    }
70
}
71