Passed
Push — master ( f0d0a7...1b0620 )
by Jan
10:32
created

LogoutLoggerListener   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 28
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 16 3
A __construct() 0 4 1
1
<?php
2
/**
3
 * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
4
 *
5
 * Copyright (C) 2019 - 2020 Jan Böhmer (https://github.com/jbtronics)
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Affero General Public License as published
9
 * by the Free Software Foundation, either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License
18
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19
 */
20
21
declare(strict_types=1);
22
23
/**
24
 * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
25
 *
26
 * Copyright (C) 2019 - 2020 Jan Böhmer (https://github.com/jbtronics)
27
 *
28
 * This program is free software; you can redistribute it and/or
29
 * modify it under the terms of the GNU General Public License
30
 * as published by the Free Software Foundation; either version 2
31
 * of the License, or (at your option) any later version.
32
 *
33
 * This program is distributed in the hope that it will be useful,
34
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
35
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36
 * GNU General Public License for more details.
37
 *
38
 * You should have received a copy of the GNU General Public License
39
 * along with this program; if not, write to the Free Software
40
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
41
 */
42
43
namespace App\EventSubscriber\LogSystem;
44
45
use App\Entity\LogSystem\UserLogoutLogEntry;
46
use App\Entity\UserSystem\User;
47
use App\Services\LogSystem\EventLogger;
48
use Symfony\Component\HttpFoundation\Request;
49
use Symfony\Component\HttpFoundation\Response;
50
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
51
use Symfony\Component\Security\Http\Event\LogoutEvent;
52
use Symfony\Component\Security\Http\Logout\LogoutHandlerInterface;
53
54
/**
55
 * This handler logs to event log, if a user logs out.
56
 */
57
class LogoutLoggerListener
58
{
59
    protected $logger;
60
    protected $gpdr_compliance;
61
62
    public function __construct(EventLogger $logger, bool $gpdr_compliance)
63
    {
64
        $this->logger = $logger;
65
        $this->gpdr_compliance = $gpdr_compliance;
66
    }
67
68
69
    public function __invoke(LogoutEvent $event)
70
    {
71
        $request = $event->getRequest();
72
        $token = $event->getToken();
73
74
        if ($token === null) {
75
            return;
76
        }
77
78
        $log = new UserLogoutLogEntry($request->getClientIp(), $this->gpdr_compliance);
0 ignored issues
show
Bug introduced by
It seems like $request->getClientIp() can also be of type null; however, parameter $ip_address of App\Entity\LogSystem\Use...LogEntry::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

78
        $log = new UserLogoutLogEntry(/** @scrutinizer ignore-type */ $request->getClientIp(), $this->gpdr_compliance);
Loading history...
79
        $user = $token->getUser();
80
        if ($user instanceof User) {
81
            $log->setTargetElement($user);
82
        }
83
84
        $this->logger->logAndFlush($log);
85
    }
86
}
87