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