Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
15 / 15 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
IdleSubscriber | |
100.00% |
15 / 15 |
|
100.00% |
3 / 3 |
5 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getSubscribedEvents | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
onKernelRequest | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
3 |
1 | <?php |
2 | |
3 | /** |
4 | * This file is part of the MADIS - RGPD Management application. |
5 | * |
6 | * @copyright Copyright (c) 2018-2019 Soluris - Solutions Numériques Territoriales Innovantes |
7 | * |
8 | * This program is free software: you can redistribute it and/or modify |
9 | * it under the terms of the GNU Affero General Public License as published by |
10 | * the Free Software Foundation, either version 3 of the License, or |
11 | * (at your option) any later version. |
12 | * |
13 | * This program is distributed in the hope that it will be useful, |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | * GNU Affero General Public License for more details. |
17 | * |
18 | * You should have received a copy of the GNU Affero General Public License |
19 | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
20 | */ |
21 | |
22 | declare(strict_types=1); |
23 | |
24 | namespace App\Application\Symfony\EventSubscriber\Kernel; |
25 | |
26 | use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
27 | use Symfony\Component\HttpKernel\Event\RequestEvent; |
28 | use Symfony\Component\HttpKernel\KernelEvents; |
29 | |
30 | /** |
31 | * Class IdleSubscriber |
32 | * Check user idle time. |
33 | * If user is idle more than accepted time, invalidate his session. |
34 | */ |
35 | class IdleSubscriber implements EventSubscriberInterface |
36 | { |
37 | /** |
38 | * @var int |
39 | */ |
40 | private $expirationTime; |
41 | |
42 | /** |
43 | * IdleSubscriber constructor. |
44 | * |
45 | * @param int $expirationTime Time in seconds to define idle |
46 | */ |
47 | public function __construct(int $expirationTime) |
48 | { |
49 | $this->expirationTime = $expirationTime; |
50 | } |
51 | |
52 | public static function getSubscribedEvents() |
53 | { |
54 | return [ |
55 | KernelEvents::REQUEST => [ |
56 | ['onKernelRequest', 9], |
57 | ], |
58 | ]; |
59 | } |
60 | |
61 | /** |
62 | * OnKernelRequest check idle since last Request. |
63 | * If idle is over, then invalidate session. |
64 | */ |
65 | public function onKernelRequest(RequestEvent $event): void |
66 | { |
67 | if (!$event->isMasterRequest()) { |
68 | return; |
69 | } |
70 | |
71 | $request = $event->getRequest(); |
72 | $session = $request->getSession(); |
73 | |
74 | $session->start(); |
75 | $metaData = $session->getMetadataBag(); |
76 | $timeDifference = time() - $metaData->getLastUsed(); |
77 | |
78 | if ($timeDifference > $this->expirationTime) { |
79 | $session->invalidate(); |
80 | } |
81 | } |
82 | } |