Completed
Push — master ( 383102...f77b2b )
by Rafael
08:15
created

setJWTCompatibility()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 1
crap 2
1
<?php
2
/*******************************************************************************
3
 *  This file is part of the GraphQL Bundle package.
4
 *
5
 *  (c) YnloUltratech <[email protected]>
6
 *
7
 *  For the full copyright and license information, please view the LICENSE
8
 *  file that was distributed with this source code.
9
 ******************************************************************************/
10
11
namespace Ynlo\GraphQLBundle\EventListener\JWT;
12
13
use GraphQL\Error\Error;
14
use Lexik\Bundle\JWTAuthenticationBundle\Event\AuthenticationFailureEvent;
15
use Lexik\Bundle\JWTAuthenticationBundle\Events;
16
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
17
use Symfony\Component\HttpFoundation\Response;
18
use Ynlo\GraphQLBundle\Error\ErrorFormatterInterface;
19
use Ynlo\GraphQLBundle\Error\ErrorHandlerInterface;
20
use Ynlo\GraphQLBundle\Exception\Controlled\UnauthorizedError;
21
22
class AuthenticationFailureListener implements EventSubscriberInterface
23
{
24
    /**
25
     * @var ErrorFormatterInterface
26
     */
27
    protected $errorFormatter;
28
29
    /**
30
     * @var ErrorHandlerInterface
31
     */
32
    protected $errorHandler;
33
34
    /**
35
     * @var bool
36
     */
37
    protected $debug = false;
38
39
    /**
40
     * @deprecated Since v1.1 and will will be removed in the next mayor release
41
     *
42
     * @var bool
43
     */
44
    protected $JWTCompatibility = false;
45
46
    /**
47
     * AuthenticationFailureListener constructor.
48
     *
49
     * @param ErrorFormatterInterface $errorFormatter
50
     * @param ErrorHandlerInterface   $errorHandler
51
     */
52
    public function __construct(ErrorFormatterInterface $errorFormatter, ErrorHandlerInterface $errorHandler)
53
    {
54
        $this->errorFormatter = $errorFormatter;
55
        $this->errorHandler = $errorHandler;
56
    }
57
58
    /**
59
     * @param bool $debug
60
     */
61
    public function setDebug(bool $debug): void
62
    {
63
        $this->debug = $debug;
64
    }
65
66
    /**
67
     * @deprecated Since v1.1 and will will be removed in the next mayor release
68
     *
69
     * @param bool $JWTCompatibility
70
     */
71
    public function setJWTCompatibility(bool $JWTCompatibility): void
72
    {
73
        $this->JWTCompatibility = $JWTCompatibility;
0 ignored issues
show
Deprecated Code introduced by
The property Ynlo\GraphQLBundle\Event...ener::$JWTCompatibility has been deprecated: Since v1.1 and will will be removed in the next mayor release ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

73
        /** @scrutinizer ignore-deprecated */ $this->JWTCompatibility = $JWTCompatibility;

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
74
    }
75
76
    /**
77
     * @inheritDoc
78
     */
79 1
    public static function getSubscribedEvents()
80
    {
81 1
        if (!class_exists('Lexik\Bundle\JWTAuthenticationBundle\Events')) {
82
            return [];
83
        }
84
85
        return [
86 1
            Events::AUTHENTICATION_FAILURE => 'onAuthFailure',
87 1
            Events::JWT_EXPIRED => 'onAuthFailure',
88 1
            Events::JWT_INVALID => 'onAuthFailure',
89 1
            Events::JWT_NOT_FOUND => 'onAuthFailure',
90
        ];
91
    }
92
93
    /**
94
     * @param AuthenticationFailureEvent $event
95
     */
96
    public function onAuthFailure(AuthenticationFailureEvent $event)
97
    {
98
        $error = Error::createLocatedError(new UnauthorizedError($event->getException()->getMessage()));
99
        $errors = $this->errorHandler->handle([$error], $this->errorFormatter, $this->debug);
100
101
        $responseArray = [
102
            'errors' => $errors,
103
        ];
104
105
        if ($this->JWTCompatibility) {
0 ignored issues
show
Deprecated Code introduced by
The property Ynlo\GraphQLBundle\Event...ener::$JWTCompatibility has been deprecated: Since v1.1 and will will be removed in the next mayor release ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

105
        if (/** @scrutinizer ignore-deprecated */ $this->JWTCompatibility) {

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
106
            @trigger_error('The JWT error compatibility has been deprecated and will be removed in the next mayor release, migrate your clients to the new error format.', E_USER_DEPRECATED);
107
            $responseArray = array_merge(
108
                [
109
                    'code' => Response::HTTP_UNAUTHORIZED,
110
                    'message' => $event->getException()->getMessage(),
111
                ],
112
                $responseArray
113
            );
114
        }
115
116
        $event->getResponse()->setContent(json_encode($responseArray));
117
    }
118
}
119