Passed
Push — main ( 31c431...897645 )
by Paul
20:38 queued 13:24
created

updateLastInteractionMoment()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 7
c 0
b 0
f 0
nc 3
nop 1
dl 0
loc 14
rs 10
1
<?php
2
3
declare(strict_types = 1);
4
5
/**
6
 * Copyright 2016 SURFnet bv
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 *     http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
Coding Style introduced by
Missing @package tag in file comment
Loading history...
Coding Style introduced by
Missing @author tag in file comment
Loading history...
Coding Style introduced by
Missing @license tag in file comment
Loading history...
Coding Style introduced by
Missing @link tag in file comment
Loading history...
20
21
namespace Surfnet\StepupSelfService\SelfServiceBundle\EventListener;
22
23
use Psr\Log\LoggerInterface;
24
use Surfnet\StepupSelfService\SelfServiceBundle\Security\Authentication\AuthenticatedSessionStateHandler;
25
use Surfnet\StepupSelfService\SelfServiceBundle\Security\Authentication\Session\SessionLifetimeGuard;
26
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
27
use Symfony\Component\HttpKernel\Event\RequestEvent;
28
use Symfony\Component\HttpKernel\KernelEvents;
29
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
30
31
class AuthenticatedUserListener implements EventSubscriberInterface
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class AuthenticatedUserListener
Loading history...
32
{
33
    public function __construct(
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __construct()
Loading history...
34
        private readonly TokenStorageInterface            $tokenStorage,
35
        private readonly SessionLifetimeGuard             $sessionLifetimeGuard,
36
        private readonly AuthenticatedSessionStateHandler $sessionStateHandler,
37
        private readonly LoggerInterface                  $logger,
38
    ) {
39
    }
40
41
    public static function getSubscribedEvents(): array
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function getSubscribedEvents()
Loading history...
42
    {
43
        return [
44
            // The firewall, which makes the token available, listens at P8
45
            // We must jump in after the firewall, forcing us to overwrite the translator locale.
46
            KernelEvents::REQUEST => ['updateLastInteractionMoment', 6],
47
        ];
48
    }
49
50
    public function updateLastInteractionMoment(RequestEvent $event): void
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function updateLastInteractionMoment()
Loading history...
51
    {
52
        $token = $this->tokenStorage->getToken();
53
54
        if ($token === null || !$this->sessionLifetimeGuard->sessionLifetimeWithinLimits($this->sessionStateHandler)) {
55
            return;
56
        }
57
        $this->logger->notice('Logged in user with a session within time limits detected, updating session state');
58
59
        // see ExplicitSessionTimeoutHandler for the rationale
60
        if ($event->getRequest()->getMethod() === 'GET') {
61
            $this->sessionStateHandler->setCurrentRequestUri($event->getRequest()->getRequestUri());
62
        }
63
        $this->sessionStateHandler->updateLastInteractionMoment();
64
    }
65
}
66