Passed
Pull Request — master (#1701)
by Tarmo
07:59 queued 11s
created

onAuthenticationSuccess()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types = 1);
3
/**
4
 * /src/EventSubscriber/AuthenticationSuccessSubscriber.php
5
 *
6
 * @author TLe, Tarmo Leppänen <[email protected]>
7
 */
8
9
namespace App\EventSubscriber;
10
11
use App\Doctrine\DBAL\Types\EnumLogLoginType;
12
use App\Repository\UserRepository;
13
use App\Utils\LoginLogger;
14
use Lexik\Bundle\JWTAuthenticationBundle\Event\AuthenticationSuccessEvent;
15
use Lexik\Bundle\JWTAuthenticationBundle\Events;
16
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
17
use Throwable;
18
19
/**
20
 * Class AuthenticationSuccessSubscriber
21
 *
22
 * @package App\EventSubscriber
23
 * @author TLe, Tarmo Leppänen <[email protected]>
24
 */
25
class AuthenticationSuccessSubscriber implements EventSubscriberInterface
26
{
27
    public function __construct(
28
        private LoginLogger $loginLogger,
29
        private UserRepository $userRepository,
30
    ) {
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     *
36
     * @return array<string, string>
37
     */
38
    public static function getSubscribedEvents(): array
39
    {
40
        return [
41
            AuthenticationSuccessEvent::class => 'onAuthenticationSuccess',
42
            Events::AUTHENTICATION_SUCCESS => 'onAuthenticationSuccess',
43
        ];
44
    }
45
46
    /**
47
     * Method to log user successfully login to database.
48
     *
49
     * This method is called when following event is broadcast
50
     *  - lexik_jwt_authentication.on_authentication_success
51
     *
52
     * @throws Throwable
53
     */
54 15
    public function onAuthenticationSuccess(AuthenticationSuccessEvent $event): void
55
    {
56 15
        $this->loginLogger
57 15
            ->setUser($this->userRepository->loadUserByIdentifier($event->getUser()->getUserIdentifier(), true))
58 15
            ->process(EnumLogLoginType::TYPE_SUCCESS);
59
    }
60
}
61