Completed
Push — feature/fine-grained-authoriza... ( 3e6dd7 )
by Michiel
02:20
created

SelfServiceBundle/EventListener/LocaleListener.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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\StepupSelfService\SelfServiceBundle\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
        $identity = $token->getUser();
55
56
        // Anonymous usage like /authentication/metadata has just "anonymous" as identity.
57
        if (!$identity instanceof Identity) {
0 ignored issues
show
The class Surfnet\StepupMiddleware...e\Identity\Dto\Identity does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
58
            return;
59
        }
60
61
        $request = $event->getRequest();
62
        $request->setLocale($identity->preferredLocale);
63
64
        // As per \Symfony\Component\HttpKernel\EventListener\TranslatorListener::setLocale()
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