Completed
Push — bugfix/anonymous-access ( c0efc9 )
by Boy
04:08
created

LocaleListener::setRequestLocale()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 25
rs 8.5806
cc 4
eloc 13
nc 4
nop 1
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
 */
18
19
namespace Surfnet\StepupRa\RaBundle\EventListener;
20
21
use Surfnet\StepupMiddlewareClientBundle\Identity\Dto\Identity;
22
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
23
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
24
use Symfony\Component\HttpKernel\KernelEvents;
25
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
26
use Symfony\Component\Translation\TranslatorInterface;
27
28
final class LocaleListener implements EventSubscriberInterface
29
{
30
    /**
31
     * @var \Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface
32
     */
33
    private $tokenStorage;
34
35
    /**
36
     * @var TranslatorInterface
37
     */
38
    private $translator;
39
40
    public function __construct(TokenStorageInterface $tokenStorage, TranslatorInterface $translator)
41
    {
42
        $this->tokenStorage = $tokenStorage;
43
        $this->translator = $translator;
44
    }
45
46
    public function setRequestLocale(GetResponseEvent $event)
47
    {
48
        $token = $this->tokenStorage->getToken();
49
50
        if (!$token) {
51
            return;
52
        }
53
54
        /** @var Identity $identity */
55
        $identity = $token->getUser();
56
57
        if (!$identity instanceof Identity) {
58
            return;
59
        }
60
61
        $request = $event->getRequest();
62
        $request->setLocale($identity->preferredLocale);
63
64
        // As per \Symfony\Component\HttpKernel\EventListener\TranslatorListener::setLocale()
0 ignored issues
show
Unused Code Comprehensibility introduced by
40% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
65
        try {
66
            $this->translator->setLocale($request->getLocale());
67
        } catch (\InvalidArgumentException $e) {
68
            $this->translator->setLocale($request->getDefaultLocale());
69
        }
70
    }
71
72
    public static function getSubscribedEvents()
73
    {
74
        return [
75
            // Default locale listener listens at P16
76
            // Translator listener, which sets the locale for the translator, listens at P10
77
            // The firewall, which makes the token available, listens at P8
78
            // We must jump in after the firewall, forcing us to overwrite the translator locale.
79
            KernelEvents::REQUEST => ['setRequestLocale', 7],
80
        ];
81
    }
82
}
83