Passed
Pull Request — master (#1817)
by Tarmo
03:34
created

__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 5
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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