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\Event\ResponseEvent; |
8
|
|
|
use Symfony\Component\HttpKernel\KernelEvents; |
9
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
10
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
11
|
|
|
use Symfony\Component\Translation\TranslatorInterface; |
12
|
|
|
use Kunstmaan\AdminBundle\Helper\AdminRouteHelper; |
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 TokenStorageInterface $tokenStorage |
46
|
|
|
* @param TranslatorInterface $translator |
47
|
|
|
* @param string $defaultAdminLocale |
48
|
|
|
* @param AdminRouteHelper $adminRouteHelper |
49
|
|
|
* @param string $providerKey Firewall name to check against |
50
|
|
|
*/ |
51
|
3 |
|
public function __construct(TokenStorageInterface $tokenStorage, TranslatorInterface $translator, AdminRouteHelper $adminRouteHelper, $defaultAdminLocale, $providerKey = 'main') |
52
|
|
|
{ |
53
|
3 |
|
$this->translator = $translator; |
54
|
3 |
|
$this->tokenStorage = $tokenStorage; |
55
|
3 |
|
$this->defaultAdminLocale = $defaultAdminLocale; |
56
|
3 |
|
$this->providerKey = $providerKey; |
57
|
3 |
|
$this->adminRouteHelper = $adminRouteHelper; |
58
|
3 |
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* onKernelRequest |
62
|
|
|
* |
63
|
|
|
* @param GetResponseEvent|ResponseEvent $event |
64
|
|
|
*/ |
65
|
3 |
|
public function onKernelRequest($event) |
66
|
|
|
{ |
67
|
3 |
View Code Duplication |
if (!$event instanceof GetResponseEvent && !$event instanceof ResponseEvent) { |
|
|
|
|
68
|
|
|
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))); |
69
|
|
|
} |
70
|
|
|
|
71
|
3 |
|
$url = $event->getRequest()->getRequestUri(); |
72
|
3 |
|
if (!$this->adminRouteHelper->isAdminRoute($url)) { |
73
|
1 |
|
return; |
74
|
|
|
} |
75
|
|
|
|
76
|
2 |
|
$token = $this->tokenStorage->getToken(); |
77
|
2 |
|
if ($token && $this->isAdminToken($this->providerKey, $token)) { |
78
|
2 |
|
$locale = $token->getUser()->getAdminLocale(); |
79
|
|
|
|
80
|
2 |
|
if (!$locale) { |
81
|
2 |
|
$locale = $this->defaultAdminLocale; |
82
|
|
|
} |
83
|
|
|
|
84
|
2 |
|
$this->translator->setLocale($locale); |
85
|
|
|
} |
86
|
2 |
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param TokenInterface $token |
90
|
|
|
* @param $providerKey |
91
|
|
|
* |
92
|
|
|
* @return bool |
93
|
|
|
*/ |
94
|
2 |
|
private function isAdminToken($providerKey, TokenInterface $token = null) |
95
|
|
|
{ |
96
|
2 |
|
return \is_callable([$token, 'getProviderKey']) && $token->getProviderKey() === $providerKey; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* getSubscribedEvents |
101
|
|
|
*/ |
102
|
6 |
|
public static function getSubscribedEvents() |
103
|
|
|
{ |
104
|
|
|
return [ |
105
|
|
|
// The event subscriber must be registered after the Symfony FirewallListener so the user token is populated. |
106
|
6 |
|
KernelEvents::REQUEST => [['onKernelRequest', 5]], |
107
|
|
|
]; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.