Completed
Pull Request — 5.6 (#2830)
by Jeroen
14:14
created

AdminBundle/EventListener/AdminLocaleListener.php (1 issue)

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
namespace Kunstmaan\AdminBundle\EventListener;
4
5
use Kunstmaan\AdminBundle\Helper\AdminRouteHelper;
6
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
7
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
8
use Symfony\Component\HttpKernel\Event\ResponseEvent;
9
use Symfony\Component\HttpKernel\KernelEvents;
10
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
11
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
12
use Symfony\Component\Translation\TranslatorInterface;
13
14
/**
15
 * AdminLocaleListener to override default locale if user-specific locale is set in database
16
 */
17
class AdminLocaleListener implements EventSubscriberInterface
18
{
19
    /**
20
     * @var TokenStorageInterface
21
     */
22
    private $tokenStorage;
23
24
    /**
25
     * @var TranslatorInterface
26
     */
27
    private $translator;
28
29
    /**
30
     * @var string
31
     */
32
    private $defaultAdminLocale;
33
34
    /**
35
     * @var string
36
     */
37
    private $providerKey;
38
39
    /**
40
     * @var AdminRouteHelper
41
     */
42
    private $adminRouteHelper;
43
44
    /**
45
     * @param string $defaultAdminLocale
46
     * @param string $providerKey        Firewall name to check against
47
     */
48 3
    public function __construct(TokenStorageInterface $tokenStorage, TranslatorInterface $translator, AdminRouteHelper $adminRouteHelper, $defaultAdminLocale, $providerKey = 'main')
49
    {
50 3
        $this->translator = $translator;
51 3
        $this->tokenStorage = $tokenStorage;
52 3
        $this->defaultAdminLocale = $defaultAdminLocale;
53 3
        $this->providerKey = $providerKey;
54 3
        $this->adminRouteHelper = $adminRouteHelper;
55 3
    }
56
57
    /**
58
     * onKernelRequest
59
     *
60
     * @param GetResponseEvent|ResponseEvent $event
61
     */
62 3
    public function onKernelRequest($event)
63
    {
64 3 View Code Duplication
        if (!$event instanceof GetResponseEvent && !$event instanceof ResponseEvent) {
65
            throw new \InvalidArgumentException(\sprintf('Expected instance of type %s, %s given', \class_exists(ResponseEvent::class) ? ResponseEvent::class : GetResponseEvent::class, \is_object($event) ? \get_class($event) : \gettype($event)));
66
        }
67
68 3
        $url = $event->getRequest()->getRequestUri();
69 3
        if (!$this->adminRouteHelper->isAdminRoute($url)) {
70 1
            return;
71
        }
72
73 2
        $token = $this->tokenStorage->getToken();
74 2
        if ($token && $this->isAdminToken($this->providerKey, $token)) {
75 2
            $locale = $token->getUser()->getAdminLocale();
76
77 2
            if (!$locale) {
78 2
                $locale = $this->defaultAdminLocale;
79
            }
80
81 2
            $this->translator->setLocale($locale);
82
        }
83 2
    }
84
85
    /**
86
     * @param TokenInterface $token
0 ignored issues
show
Should the type for parameter $token not be null|TokenInterface?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
87
     * @param                $providerKey
88
     *
89
     * @return bool
90
     */
91 2
    private function isAdminToken($providerKey, TokenInterface $token = null)
92
    {
93 2
        return \is_callable([$token, 'getProviderKey']) && $token->getProviderKey() === $providerKey;
94
    }
95
96
    /**
97
     * getSubscribedEvents
98
     */
99 6
    public static function getSubscribedEvents()
100
    {
101
        return [
102
            // The event subscriber must be registered after the Symfony FirewallListener so the user token is populated.
103 6
            KernelEvents::REQUEST => [['onKernelRequest', 5]],
104
        ];
105
    }
106
}
107