Merge pull request #459 from juliangut/throw_instead_of_return

V5 - Throw exception instead of return Response
This commit is contained in:
Alex Bilbie 2016-03-11 07:49:57 +00:00
commit c3ffed2daf
2 changed files with 11 additions and 7 deletions

View File

@ -128,7 +128,8 @@ class Server implements EmitterAwareInterface
}
$tokenResponse = null;
foreach ($this->enabledGrantTypes as $grantType) {
while ($tokenResponse === null && $grantType = array_shift($this->enabledGrantTypes)) {
/** @var \League\OAuth2\Server\Grant\GrantTypeInterface $grantType */
if ($grantType->canRespondToRequest($request)) {
$tokenResponse = $grantType->respondToRequest(
$request,
@ -142,11 +143,11 @@ class Server implements EmitterAwareInterface
return $tokenResponse;
}
if ($tokenResponse instanceof ResponseTypeInterface === false) {
return OAuthServerException::unsupportedGrantType()->generateHttpResponse($response);
if ($tokenResponse instanceof ResponseTypeInterface) {
return $tokenResponse->generateHttpResponse($response);
}
return $tokenResponse->generateHttpResponse($response);
throw OAuthServerException::unsupportedGrantType();
}
/**

View File

@ -33,9 +33,12 @@ class ServerTest extends \PHPUnit_Framework_TestCase
$server->enableGrantType(new ClientCredentialsGrant(), new \DateInterval('PT1M'));
$response = $server->respondToRequest();
$this->assertTrue($response instanceof ResponseInterface);
$this->assertEquals(400, $response->getStatusCode());
try {
$server->respondToRequest();
} catch (OAuthServerException $e) {
$this->assertEquals('unsupported_grant_type', $e->getErrorType());
$this->assertEquals(400, $e->getHttpStatusCode());
}
}
public function testRespondToRequest()