Skip to content

Commit

Permalink
bug #644 Fix FC/BC layer for EventDispatcher (nicolas-grekas, chalasr)
Browse files Browse the repository at this point in the history
This PR was merged into the 2.x-dev branch.

Discussion
----------

Fix FC/BC layer for EventDispatcher

Fix #637

Having Contracts 1.1 doesn't mean EventDispatcher 4.3 is installed ;)

ping @chalasr

Commits
-------

0d564b3 Re-add needed use statements
7d47084 Fix FC/BC layer for EventDispatcher
  • Loading branch information
chalasr committed Apr 17, 2019
2 parents bd0f5ab + 0d564b3 commit be2b78c
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 15 deletions.
4 changes: 3 additions & 1 deletion Event/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
namespace Lexik\Bundle\JWTAuthenticationBundle\Event;

use Symfony\Component\EventDispatcher\Event as BaseEvent;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Contracts\EventDispatcher\Event as ContractsBaseEvent;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;

if (class_exists(ContractsBaseEvent::class)) {
if (is_subclass_of(EventDispatcher::class, EventDispatcherInterface::class)) {
class Event extends ContractsBaseEvent
{
}
Expand Down
6 changes: 3 additions & 3 deletions Security/Guard/JWTTokenAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public function onAuthenticationFailure(Request $request, AuthenticationExceptio
$eventName = Events::JWT_INVALID;
}

if (interface_exists(ContractsEventDispatcherInterface::class)) {
if ($this->dispatcher instanceof ContractsEventDispatcherInterface) {
$this->dispatcher->dispatch($event, $eventName);
} else {
$this->dispatcher->dispatch($eventName, $event);
Expand All @@ -205,7 +205,7 @@ public function start(Request $request, AuthenticationException $authException =
$exception = new MissingTokenException('JWT Token not found', 0, $authException);
$event = new JWTNotFoundEvent($exception, new JWTAuthenticationFailureResponse($exception->getMessageKey()));

if (interface_exists(ContractsEventDispatcherInterface::class)) {
if ($this->dispatcher instanceof ContractsEventDispatcherInterface) {
$this->dispatcher->dispatch($event, Events::JWT_NOT_FOUND);
} else {
$this->dispatcher->dispatch(Events::JWT_NOT_FOUND, $event);
Expand Down Expand Up @@ -237,7 +237,7 @@ public function createAuthenticatedToken(UserInterface $user, $providerKey)

$authToken = new JWTUserToken($user->getRoles(), $user, $preAuthToken->getCredentials(), $providerKey);

if (interface_exists(ContractsEventDispatcherInterface::class)) {
if ($this->dispatcher instanceof ContractsEventDispatcherInterface) {
$this->dispatcher->dispatch(new JWTAuthenticatedEvent($preAuthToken->getPayload(), $authToken), Events::JWT_AUTHENTICATED);
} else {
$this->dispatcher->dispatch(Events::JWT_AUTHENTICATED, new JWTAuthenticatedEvent($preAuthToken->getPayload(), $authToken));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function onAuthenticationFailure(Request $request, AuthenticationExceptio
{
$event = new AuthenticationFailureEvent($exception, new JWTAuthenticationFailureResponse());

if (interface_exists(ContractsEventDispatcherInterface::class)) {
if ($this->dispatcher instanceof ContractsEventDispatcherInterface) {
$this->dispatcher->dispatch($event, Events::AUTHENTICATION_FAILURE);
} else {
$this->dispatcher->dispatch(Events::AUTHENTICATION_FAILURE, $event);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function handleAuthenticationSuccess(UserInterface $user, $jwt = null)
$response = new JWTAuthenticationSuccessResponse($jwt);
$event = new AuthenticationSuccessEvent(['token' => $jwt], $user, $response);

if (interface_exists(ContractsEventDispatcherInterface::class)) {
if ($this->dispatcher instanceof ContractsEventDispatcherInterface) {
$this->dispatcher->dispatch($event, Events::AUTHENTICATION_SUCCESS);
} else {
$this->dispatcher->dispatch(Events::AUTHENTICATION_SUCCESS, $event);
Expand Down
6 changes: 3 additions & 3 deletions Services/JWTManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function create(UserInterface $user)
$this->addUserIdentityToPayload($user, $payload);

$jwtCreatedEvent = new JWTCreatedEvent($payload, $user);
if (interface_exists(ContractsEventDispatcherInterface::class)) {
if ($this->dispatcher instanceof ContractsEventDispatcherInterface) {
$this->dispatcher->dispatch($jwtCreatedEvent, Events::JWT_CREATED);
} else {
$this->dispatcher->dispatch(Events::JWT_CREATED, $jwtCreatedEvent);
Expand All @@ -79,7 +79,7 @@ public function create(UserInterface $user)

$jwtEncodedEvent = new JWTEncodedEvent($jwtString);

if (interface_exists(ContractsEventDispatcherInterface::class, false)) {
if ($this->dispatcher instanceof ContractsEventDispatcherInterface) {
$this->dispatcher->dispatch($jwtEncodedEvent, Events::JWT_ENCODED);
} else {
$this->dispatcher->dispatch(Events::JWT_ENCODED, $jwtEncodedEvent);
Expand All @@ -98,7 +98,7 @@ public function decode(TokenInterface $token)
}

$event = new JWTDecodedEvent($payload);
if (interface_exists(ContractsEventDispatcherInterface::class)) {
if ($this->dispatcher instanceof ContractsEventDispatcherInterface) {
$this->dispatcher->dispatch($event, Events::JWT_DECODED);
} else {
$this->dispatcher->dispatch(Events::JWT_DECODED, $event);
Expand Down
2 changes: 1 addition & 1 deletion Tests/Security/Guard/JWTTokenAuthenticatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ private function getUserProviderMock()

private function expectEvent($eventName, $event, $dispatcher)
{
if (interface_exists(ContractsEventDispatcherInterface::class)) {
if ($dispatcher instanceof ContractsEventDispatcherInterface) {
$dispatcher->expects($this->once())->method('dispatch')->with($event, $eventName);

return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ private function getDispatcher()
->disableOriginalConstructor()
->getMock();

if (interface_exists(ContractsEventDispatcherInterface::class)) {
if ($dispatcher instanceof ContractsEventDispatcherInterface) {
$dispatcher
->expects($this->once())
->method('dispatch')
Expand Down
8 changes: 4 additions & 4 deletions Tests/Services/JWTManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function testCreate()
{
$dispatcher = $this->getEventDispatcherMock();

if (interface_exists(ContractsEventDispatcherInterface::class)) {
if ($dispatcher instanceof ContractsEventDispatcherInterface) {
$dispatcher
->expects($this->at(0))
->method('dispatch')
Expand Down Expand Up @@ -76,7 +76,7 @@ public function testDecode()
{
$dispatcher = $this->getEventDispatcherMock();

if (interface_exists(ContractsEventDispatcherInterface::class)) {
if ($dispatcher instanceof ContractsEventDispatcherInterface) {
$dispatcher
->expects($this->once())
->method('dispatch')
Expand Down Expand Up @@ -111,7 +111,7 @@ public function testIdentityField()
{
$dispatcher = $this->getEventDispatcherMock();

if (interface_exists(ContractsEventDispatcherInterface::class)) {
if ($dispatcher instanceof ContractsEventDispatcherInterface) {
$dispatcher
->expects($this->at(0))
->method('dispatch')
Expand Down Expand Up @@ -196,7 +196,7 @@ protected function getEventDispatcherMock()

private function expectEvent($eventName, $eventClass, $dispatcher)
{
if (interface_exists(ContractsEventDispatcherInterface::class)) {
if ($dispatcher instanceof ContractsEventDispatcherInterface) {
$dispatcher->expects($this->once())->method('dispatch')->with($event, $eventName);

return;
Expand Down

0 comments on commit be2b78c

Please sign in to comment.