Fail2BanSubscriber   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 11
dl 0
loc 30
rs 10
c 2
b 0
f 0
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A logFail2Ban() 0 5 1
A getSubscribedEvents() 0 5 1
A __construct() 0 4 1
1
<?php
2
/*
3
 * Copyright (C) 2020  Jan Böhmer
4
 *
5
 * This program is free software: you can redistribute it and/or modify
6
 * it under the terms of the GNU Affero General Public License as published
7
 * by the Free Software Foundation, either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU Affero General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU Affero General Public License
16
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
17
 */
18
19
namespace App\EventSubscriber;
20
21
use Psr\Log\LoggerInterface;
22
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
23
use Symfony\Component\HttpFoundation\RequestStack;
24
use Symfony\Component\Security\Core\AuthenticationEvents;
25
use Symfony\Component\Security\Core\Event\AuthenticationFailureEvent;
26
27
/**
28
 * This event subscriber.
29
 */
30
class Fail2BanSubscriber implements EventSubscriberInterface
31
{
32
    /**
33
     * @var LoggerInterface
34
     */
35
    private $logger;
36
37
    /**
38
     * @var RequestStack
39
     */
40
    private $request;
41
42
    public function __construct(LoggerInterface $fail2banLogger, RequestStack $request)
43
    {
44
        $this->logger = $fail2banLogger;
45
        $this->request = $request;
46
    }
47
48
    public function logFail2Ban(AuthenticationFailureEvent $event): void
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed. ( Ignorable by Annotation )

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

48
    public function logFail2Ban(/** @scrutinizer ignore-unused */ AuthenticationFailureEvent $event): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
49
    {
50
        $ipAddress = $this->request->getCurrentRequest()
51
            ->getClientIp();
52
        $this->logger->error('Authentication failed for IP: '.$ipAddress);
53
    }
54
55
    public static function getSubscribedEvents(): array
56
    {
57
        return [
58
            AuthenticationEvents::AUTHENTICATION_FAILURE => [
0 ignored issues
show
Deprecated Code introduced by
The constant Symfony\Component\Securi...:AUTHENTICATION_FAILURE has been deprecated: since Symfony 5.4, use {@see Event\LoginFailureEvent} instead ( Ignorable by Annotation )

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

58
            /** @scrutinizer ignore-deprecated */ AuthenticationEvents::AUTHENTICATION_FAILURE => [

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
59
                'logFail2Ban',
60
            ],
61
        ];
62
    }
63
}
64