SecondFactorLocaleListener::getSubscribedEvents()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Copyright 2015 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\StepupGateway\GatewayBundle\EventListener;
20
21
use Surfnet\StepupBundle\Http\CookieHelper;
22
use Surfnet\StepupGateway\GatewayBundle\Saml\ResponseContext;
23
use Surfnet\StepupGateway\GatewayBundle\Service\SecondFactorService;
24
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
25
use Symfony\Component\HttpFoundation\Request;
26
use Symfony\Component\HttpKernel\Event\RequestEvent;
27
use Symfony\Component\HttpKernel\KernelEvents;
28
29
final class SecondFactorLocaleListener implements EventSubscriberInterface
30
{
31
    public function __construct(
32
        private readonly ResponseContext     $responseContext,
33
        private readonly SecondFactorService $secondFactorService,
34
        private readonly CookieHelper        $cookieHelper
35
    ) {
36
    }
37
38
    public function setRequestLocale(RequestEvent $event): void
39
    {
40
        $locale = $this->getLocaleFromSelectedSecondFactor();
41
42
        if (!$locale) {
43
            $locale = $this->getLocaleFromCookie($event->getRequest());
44
        }
45
46
        if ($locale) {
47
            $event->getRequest()->setLocale($locale);
48
        }
49
    }
50
51
    /**
52
     * @return string|void
53
     */
54
    public function getLocaleFromSelectedSecondFactor()
55
    {
56
        $secondFactorId = $this->responseContext->getSelectedSecondFactor();
57
        if (!$secondFactorId) {
58
            return;
59
        }
60
61
        $secondFactor = $this->secondFactorService->findByUuid($secondFactorId);
62
        if ($secondFactor) {
63
            return $secondFactor->displayLocale;
0 ignored issues
show
Bug introduced by
The property displayLocale is declared private in Surfnet\StepupGateway\Se...econdfactorGsspFallback and cannot be accessed from this context.
Loading history...
64
        }
65
    }
66
67
    /**
68
     * @return string|void
69
     */
70
    public function getLocaleFromCookie(Request $request)
71
    {
72
        $requestCookie = $this->cookieHelper->read($request);
73
        if ($requestCookie) {
74
            return $requestCookie->getValue();
75
        }
76
    }
77
78
    public static function getSubscribedEvents()
79
    {
80
        return [
81
            KernelEvents::REQUEST => ['setRequestLocale', 17],
82
        ];
83
    }
84
}
85