Skip to content

Commit

Permalink
Simplify AuthorizationRequestResolveEvent creation
Browse files Browse the repository at this point in the history
  • Loading branch information
Petar Obradović committed Nov 29, 2019
1 parent 7e507e8 commit 76446a7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 22 deletions.
33 changes: 12 additions & 21 deletions Event/AuthorizationRequestResolveEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,8 @@
use League\OAuth2\Server\RequestTypes\AuthorizationRequest;
use LogicException;
use Psr\Http\Message\ResponseInterface;
use RuntimeException;
use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\Security\Core\User\UserInterface;
use Trikoder\Bundle\OAuth2Bundle\Converter\ScopeConverterInterface;
use Trikoder\Bundle\OAuth2Bundle\Manager\ClientManagerInterface;
use Trikoder\Bundle\OAuth2Bundle\Model\Client;
use Trikoder\Bundle\OAuth2Bundle\Model\Scope;

Expand All @@ -26,14 +23,14 @@ final class AuthorizationRequestResolveEvent extends Event
private $authorizationRequest;

/**
* @var ScopeConverterInterface
* @var Scope[]
*/
private $scopeConverter;
private $scopes;

/**
* @var ClientManagerInterface
* @var Client
*/
private $clientManager;
private $client;

/**
* @var bool
Expand All @@ -50,11 +47,14 @@ final class AuthorizationRequestResolveEvent extends Event
*/
private $user;

public function __construct(AuthorizationRequest $authorizationRequest, ScopeConverterInterface $scopeConverter, ClientManagerInterface $clientManager)
/**
* @param Scope[] $scopes
*/
public function __construct(AuthorizationRequest $authorizationRequest, array $scopes, Client $client)
{
$this->authorizationRequest = $authorizationRequest;
$this->scopeConverter = $scopeConverter;
$this->clientManager = $clientManager;
$this->scopes = $scopes;
$this->client = $client;
}

public function getAuthorizationResolution(): bool
Expand Down Expand Up @@ -100,14 +100,7 @@ public function getGrantTypeId(): string

public function getClient(): Client
{
$identifier = $this->authorizationRequest->getClient()->getIdentifier();
$client = $this->clientManager->find($identifier);

if (null === $client) {
throw new RuntimeException(sprintf('No client found for the given identifier "%s".', $identifier));
}

return $client;
return $this->client;
}

public function getUser(): ?UserInterface
Expand All @@ -127,9 +120,7 @@ public function setUser(?UserInterface $user): self
*/
public function getScopes(): array
{
return $this->scopeConverter->toDomainArray(
$this->authorizationRequest->getScopes()
);
return $this->scopes;
}

public function isAuthorizationApproved(): bool
Expand Down
11 changes: 10 additions & 1 deletion Event/AuthorizationRequestResolveEventFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Trikoder\Bundle\OAuth2Bundle\Event;

use League\OAuth2\Server\RequestTypes\AuthorizationRequest;
use RuntimeException;
use Trikoder\Bundle\OAuth2Bundle\Converter\ScopeConverterInterface;
use Trikoder\Bundle\OAuth2Bundle\Manager\ClientManagerInterface;

Expand All @@ -28,6 +29,14 @@ public function __construct(ScopeConverterInterface $scopeConverter, ClientManag

public function fromAuthorizationRequest(AuthorizationRequest $authorizationRequest): AuthorizationRequestResolveEvent
{
return new AuthorizationRequestResolveEvent($authorizationRequest, $this->scopeConverter, $this->clientManager);
$scopes = $this->scopeConverter->toDomainArray($authorizationRequest->getScopes());

$client = $this->clientManager->find($authorizationRequest->getClient()->getIdentifier());

if (null === $client) {
throw new RuntimeException(sprintf('No client found for the given identifier \'%s\'.', $authorizationRequest->getClient()->getIdentifier()));
}

return new AuthorizationRequestResolveEvent($authorizationRequest, $scopes, $client);
}
}

0 comments on commit 76446a7

Please sign in to comment.