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 Symfony\Component\EventDispatcher\EventSubscriberInterface; |
||
6 | use Symfony\Component\HttpKernel\Event\GetResponseEvent; |
||
7 | use Symfony\Component\HttpKernel\KernelEvents; |
||
8 | use Symfony\Component\Security\Core\Authentication\Token\RememberMeToken; |
||
9 | use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
||
10 | use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
||
11 | use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken; |
||
12 | use Symfony\Component\Translation\TranslatorInterface; |
||
13 | use Kunstmaan\AdminBundle\Helper\AdminRouteHelper; |
||
14 | |||
15 | /** |
||
16 | * AdminLocaleListener to override default locale if user-specific locale is set in database |
||
17 | */ |
||
18 | class AdminLocaleListener implements EventSubscriberInterface |
||
19 | { |
||
20 | /** |
||
21 | * @var TokenStorageInterface |
||
22 | */ |
||
23 | private $tokenStorage; |
||
24 | |||
25 | /** |
||
26 | * @var TranslatorInterface |
||
27 | */ |
||
28 | private $translator; |
||
29 | |||
30 | /** |
||
31 | * @var string |
||
32 | */ |
||
33 | private $defaultAdminLocale; |
||
34 | |||
35 | /** |
||
36 | * @var string |
||
37 | */ |
||
38 | private $providerKey; |
||
39 | |||
40 | /** |
||
41 | * @var AdminRouteHelper |
||
42 | */ |
||
43 | private $adminRouteHelper; |
||
44 | |||
45 | /** |
||
46 | * @param TokenStorageInterface $tokenStorage |
||
47 | * @param TranslatorInterface $translator |
||
48 | * @param string $defaultAdminLocale |
||
49 | * @param AdminRouteHelper $adminRouteHelper |
||
50 | * @param string $providerKey Firewall name to check against |
||
51 | */ |
||
52 | public function __construct(TokenStorageInterface $tokenStorage, TranslatorInterface $translator, AdminRouteHelper $adminRouteHelper, $defaultAdminLocale, $providerKey = 'main') |
||
53 | { |
||
54 | $this->translator = $translator; |
||
55 | $this->tokenStorage = $tokenStorage; |
||
56 | $this->defaultAdminLocale = $defaultAdminLocale; |
||
57 | $this->providerKey = $providerKey; |
||
58 | $this->adminRouteHelper = $adminRouteHelper; |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * onKernelRequest |
||
63 | * |
||
64 | * @param GetResponseEvent $event |
||
65 | */ |
||
66 | public function onKernelRequest(GetResponseEvent $event) |
||
67 | { |
||
68 | $url = $event->getRequest()->getRequestUri(); |
||
69 | $token = $this->tokenStorage->getToken(); |
||
70 | |||
71 | if ($token && $this->isAdminToken($this->providerKey, $token) && $this->adminRouteHelper->isAdminRoute($url)) { |
||
72 | $locale = $token->getUser()->getAdminLocale(); |
||
73 | |||
74 | if (!$locale) { |
||
75 | $locale = $this->defaultAdminLocale; |
||
76 | } |
||
77 | |||
78 | $this->translator->setLocale($locale); |
||
79 | } |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * @param TokenInterface $token |
||
0 ignored issues
–
show
|
|||
84 | * @param $providerKey |
||
85 | * |
||
86 | * @return bool |
||
87 | */ |
||
88 | private function isAdminToken($providerKey, TokenInterface $token = null) |
||
89 | { |
||
90 | return is_callable([$token, 'getProviderKey']) && $token->getProviderKey() === $providerKey; |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * getSubscribedEvents |
||
95 | */ |
||
96 | static public function getSubscribedEvents() |
||
97 | { |
||
98 | return array( |
||
99 | // Must be registered before the default Locale listener |
||
100 | KernelEvents::REQUEST => array(array('onKernelRequest', 17)), |
||
101 | ); |
||
102 | } |
||
103 | } |
||
104 |
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.