JWTTokenAuthenticator   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 9
c 1
b 0
f 0
dl 0
loc 24
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A onAuthenticationSuccess() 0 9 2
1
<?php
2
3
namespace App\Security;
4
5
use App\Model\UserInterface;
6
use Doctrine\ORM\EntityManagerInterface;
7
use Lexik\Bundle\JWTAuthenticationBundle\Security\Guard\JWTTokenAuthenticator as LexikJWTTokenAuthenticator;
8
use Lexik\Bundle\JWTAuthenticationBundle\Services\JWTTokenManagerInterface;
9
use Lexik\Bundle\JWTAuthenticationBundle\TokenExtractor\TokenExtractorInterface;
10
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
11
use Symfony\Component\HttpFoundation\Request;
12
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
13
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
14
15
final class JWTTokenAuthenticator extends LexikJWTTokenAuthenticator
16
{
17
    private EntityManagerInterface $entityManager;
18
19
    public function __construct(
20
        JWTTokenManagerInterface $jwtManager,
21
        EventDispatcherInterface $dispatcher,
22
        TokenExtractorInterface $tokenExtractor,
23
        EntityManagerInterface $entityManager,
24
        TokenStorageInterface $preAuthenticationTokenStorage
25
    ) {
26
        parent::__construct($jwtManager, $dispatcher, $tokenExtractor, $preAuthenticationTokenStorage);
27
        $this->entityManager = $entityManager;
28
    }
29
30
    public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey)
31
    {
32
        $user = $token->getUser();
33
        if ($user instanceof UserInterface) {
34
            $user->setLastLoginDate(new \DateTime());
35
            $this->entityManager->flush();
36
        }
37
38
        return null;
39
    }
40
}
41