Passed
Push — master ( f52952...8a81ff )
by
unknown
04:07
created

SecuritySubscriber::onAuthenticationFailure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\EventSubscriber;
4
5
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
6
use Symfony\Component\Security\Core\AuthenticationEvents;
7
use Symfony\Component\Security\Core\Event\AuthenticationFailureEvent;
8
use Symfony\Component\Security\Core\Event\AuthenticationEvent;
9
use Psr\Log\LoggerInterface;
10
11
class SecuritySubscriber implements EventSubscriberInterface
12
{
13
    public static function getSubscribedEvents()
14
    {
15
        return array(
16
            AuthenticationEvents::AUTHENTICATION_FAILURE => 'onAuthenticationFailure',
17
            AuthenticationEvents::AUTHENTICATION_SUCCESS => 'onAuthenticationSuccess',
18
        );
19
    }
20
21
    public function __construct(LoggerInterface $logger)
22
    {
23
        $this->logger = $logger;
0 ignored issues
show
Bug Best Practice introduced by
The property logger does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
24
    }
25
26
    public function onAuthenticationFailure(AuthenticationFailureEvent $event)
27
    {
28
        $exception = $event->getAuthenticationException();
29
        $token = $event->getAuthenticationToken();
30
        $creds = $token->getCredentials();
31
        $this->logger->error("Login failed for " . $creds['username'] . ": " . $exception->getMessage());
32
    }
33
34
    public function onAuthenticationSuccess(AuthenticationEvent $event)
35
    {
36
        $token = $event->getAuthenticationToken();
37
        if ($token->getUsername() != "anon") {
38
            $this->logger->info("Login succeeeded: " . $token->getUsername());
39
        }
40
    }
41
}
42