Passed
Pull Request — main (#308)
by Paul
13:26 queued 06:57
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
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
30
use InvalidArgumentException;
31
32
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 32 at column 6
Loading history...
33
{
34
    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...
35
    {
36
    }
37
38
    public function setRequestLocale(RequestEvent $event): void
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function setRequestLocale()
Loading history...
39
    {
40
        $token = $this->tokenStorage->getToken();
41
42
        if (!$token instanceof TokenInterface) {
43
            return;
44
        }
45
46
        $identity = $token->getUser()->getIdentity();
47
48
        // Anonymous usage like /authentication/metadata has just "anonymous" as identity.
49
        if (!$identity instanceof Identity) {
50
            return;
51
        }
52
53
        $request = $event->getRequest();
54
        $request->setLocale($identity->preferredLocale);
55
56
        // As per \Symfony\Component\HttpKernel\EventListener\TranslatorListener::setLocale()
57
        try {
58
            $this->translator->setLocale($request->getLocale());
59
        } catch (InvalidArgumentException) {
60
            $this->translator->setLocale($request->getDefaultLocale());
61
        }
62
    }
63
64
    public static function getSubscribedEvents(): array
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function getSubscribedEvents()
Loading history...
65
    {
66
        return [
67
            // Default locale listener listens at P16
68
            // Translator listener, which sets the locale for the translator, listens at P10
69
            // The firewall, which makes the token available, listens at P8
70
            // We must jump in after the firewall, forcing us to overwrite the translator locale.
71
            KernelEvents::REQUEST => ['setRequestLocale', 7],
72
        ];
73
    }
74
}
75