Completed
Push — master ( 049d0b...27e03f )
by Tarmo
16s queued 13s
created

TranslatedAuthenticationFailureHandler   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 7
c 3
b 0
f 0
dl 0
loc 29
ccs 7
cts 7
cp 1
rs 10
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A onAuthenticationFailure() 0 13 1
1
<?php
2
declare(strict_types = 1);
3
/**
4
 * /src/Security/Handler/TranslatedAuthenticationFailureHandler.php
5
 *
6
 * @author TLe, Tarmo Leppänen <[email protected]>
7
 */
8
9
namespace App\Security\Handler;
10
11
use Lexik\Bundle\JWTAuthenticationBundle\Event\AuthenticationFailureEvent;
12
use Lexik\Bundle\JWTAuthenticationBundle\Response\JWTAuthenticationFailureResponse;
13
use Lexik\Bundle\JWTAuthenticationBundle\Security\Http\Authentication\AuthenticationFailureHandler;
14
use Symfony\Component\HttpFoundation\Request;
15
use Symfony\Component\HttpFoundation\Response;
16
use Symfony\Component\Security\Core\Exception\AuthenticationException;
17
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
18
use Symfony\Contracts\Translation\TranslatorInterface;
19
20
/**
21
 * Class TranslatedAuthenticationFailureHandler
22
 *
23
 * @package App\Security\Handler
24
 * @author TLe, Tarmo Leppänen <[email protected]>
25
 */
26
class TranslatedAuthenticationFailureHandler extends AuthenticationFailureHandler
27
{
28 18
    public function __construct(
29
        EventDispatcherInterface $dispatcher,
30
        private readonly TranslatorInterface $translator,
31
    ) {
32 18
        parent::__construct($dispatcher);
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     *
38
     * @see https://github.com/lexik/LexikJWTAuthenticationBundle/issues/944
39
     *
40
     * @noinspection PhpMissingParentCallCommonInspection
41
     */
42 4
    public function onAuthenticationFailure(Request $request, AuthenticationException $exception): Response
43
    {
44
        /**
45
         * @psalm-suppress MissingDependency, InvalidArgument
46
         */
47 4
        $event = new AuthenticationFailureEvent(
48
            $exception,
49 4
            new JWTAuthenticationFailureResponse($this->translator->trans('Invalid credentials.', [], 'security'))
50
        );
51
52 4
        $this->dispatcher->dispatch($event);
53
54 4
        return $event->getResponse();
55
    }
56
}
57