onAuthenticationSuccess()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.3332

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 9
c 1
b 0
f 1
nc 4
nop 1
dl 0
loc 16
ccs 6
cts 9
cp 0.6667
crap 3.3332
rs 9.9666
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\EventListener;
6
7
use App\Entity\User;
8
use App\Serializer\Normalizer\UserNormalizer;
9
use Lexik\Bundle\JWTAuthenticationBundle\Event\AuthenticationSuccessEvent;
10
use Lexik\Bundle\JWTAuthenticationBundle\Events as JWTEvents;
11
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
12
use Symfony\Component\Serializer\Exception\ExceptionInterface;
13
14
final class JWTAuthenticationSubscriber implements EventSubscriberInterface
15
{
16
    private UserNormalizer $normalizer;
17
18 1
    public function __construct(UserNormalizer $normalizer)
19
    {
20 1
        $this->normalizer = $normalizer;
21
    }
22
23
    public static function getSubscribedEvents(): array
24
    {
25
        return [
26
            JWTEvents::AUTHENTICATION_SUCCESS => 'onAuthenticationSuccess',
27
        ];
28
    }
29
30 1
    public function onAuthenticationSuccess(AuthenticationSuccessEvent $event): void
31
    {
32 1
        $user = $event->getUser();
33
34 1
        if (!$user instanceof User) {
35
            return;
36
        }
37
38
        try {
39 1
            $userData = $this->normalizer->normalize($user, null, ['groups' => ['me']]);
40 1
            $eventData = $event->getData();
41
        } catch (ExceptionInterface $exception) {
42
            return;
43
        }
44
45 1
        $event->setData(['user' => array_merge($userData, $eventData)]);
46
    }
47
}
48