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\StepupBundle\EventListener; |
20
|
|
|
|
21
|
|
|
use Psr\Log\LoggerInterface; |
22
|
|
|
use Surfnet\StepupBundle\Http\CookieHelper; |
23
|
|
|
use Surfnet\StepupBundle\Service\LocaleProviderService; |
24
|
|
|
use Symfony\Component\HttpFoundation\Cookie; |
25
|
|
|
use Symfony\Component\HttpKernel\Event\FilterResponseEvent; |
26
|
|
|
|
27
|
|
|
final class LocaleCookieListener |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var Cookie |
31
|
|
|
*/ |
32
|
|
|
private $cookieHelper; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var LocaleProviderService |
36
|
|
|
*/ |
37
|
|
|
private $localeProvider; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var LoggerInterface |
41
|
|
|
*/ |
42
|
|
|
private $logger; |
43
|
|
|
|
44
|
|
|
public function __construct( |
45
|
|
|
CookieHelper $cookieHelper, |
46
|
|
|
LocaleProviderService $localeProvider, |
47
|
|
|
LoggerInterface $logger |
48
|
|
|
) { |
49
|
|
|
$this->cookieHelper = $cookieHelper; |
|
|
|
|
50
|
|
|
$this->localeProvider = $localeProvider; |
51
|
|
|
$this->logger = $logger; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* If there is a logged in user with a preferred language, set it as a cookie. |
56
|
|
|
* |
57
|
|
|
* @param FilterResponseEvent $event |
58
|
|
|
*/ |
59
|
|
|
public function onKernelResponse(FilterResponseEvent $event) |
60
|
|
|
{ |
61
|
|
|
$locale = $this->localeProvider->determinePreferredLocale(); |
62
|
|
|
|
63
|
|
|
// Unable to determine preferred locale? No need to hand out a cookie then. |
64
|
|
|
if (empty($locale)) { |
65
|
|
|
return; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
// Did the request already contain the proper cookie value? No need to hand out a cookie then. |
69
|
|
|
$requestCookie = $this->cookieHelper->read($event->getRequest()); |
|
|
|
|
70
|
|
|
if ($requestCookie && $requestCookie->getValue() === $locale) { |
71
|
|
|
$this->logger->debug(sprintf( |
72
|
|
|
'Locale cookie already set to "%s", nothing to do here', |
73
|
|
|
$locale |
74
|
|
|
)); |
75
|
|
|
return; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$this->cookieHelper->write($event->getResponse(), $locale); |
|
|
|
|
79
|
|
|
$this->logger->notice(sprintf("Set locale cookie to %s", $locale)); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..