Lines
100.00%
8 / 8
Methods
100.00%
2 / 2
Classes
100.00%
1 / 1
| Name | Lines | Methods | CRAP | ||||
|---|---|---|---|---|---|---|---|
| __construct | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| __invoke | 100.00% | 7 / 7 | 100.00% | 1 / 1 | 3 | ||
| 19 | final readonly class LocaleListener | |
| 20 | { | |
| 21 | /** | |
| 22 | * @param list<string> $enabledLocales | |
| 23 | */ | |
| 24 | public function __construct( | |
| 25 | #[Autowire(param: 'kernel.enabled_locales')] | |
| 26 | private array $enabledLocales, | |
| 27 | ) { | |
| 28 | } | |
| 29 | ||
| 30 | public function __invoke(RequestEvent $event): void | |
| 31 | { | |
| 32 | if (!$event->isMainRequest()) { | |
| 33 | return; | |
| 34 | } | |
| 35 | ||
| 36 | $request = $event->getRequest(); | |
| 37 | $chosen = $request->cookies->getString(Switcher::COOKIE); | |
| 38 | $request->setLocale(\in_array($chosen, $this->enabledLocales, true) | |
| 39 | ? $chosen | |
| 40 | : $request->getPreferredLanguage($this->enabledLocales) ?? $request->getDefaultLocale()); | |
| 41 | } | |
| 42 | } |