Created middleware used with short codes creation actions to handle content negotiation

This commit is contained in:
Alejandro Celaya 2018-05-03 18:26:31 +02:00
parent 334710e92c
commit 59f10619ba
4 changed files with 56 additions and 3 deletions

View file

@ -36,6 +36,7 @@ return [
Middleware\CrossDomainMiddleware::class => InvokableFactory::class,
Middleware\PathVersionMiddleware::class => InvokableFactory::class,
Middleware\CheckAuthenticationMiddleware::class => ConfigAbstractFactory::class,
Middleware\ShortCode\CreateShortCodeContentNegotiationMiddleware::class => InvokableFactory::class,
],
],

View file

@ -1,6 +1,8 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\Rest;
use Shlinkio\Shlink\Rest\Action;
return [
@ -9,8 +11,12 @@ return [
Action\AuthenticateAction::getRouteDef(),
// Short codes
Action\ShortCode\CreateShortCodeAction::getRouteDef(),
Action\ShortCode\SingleStepCreateShortCodeAction::getRouteDef(),
Action\ShortCode\CreateShortCodeAction::getRouteDef([
Middleware\ShortCode\CreateShortCodeContentNegotiationMiddleware::class,
]),
Action\ShortCode\SingleStepCreateShortCodeAction::getRouteDef([
Middleware\ShortCode\CreateShortCodeContentNegotiationMiddleware::class,
]),
Action\ShortCode\EditShortCodeAction::getRouteDef(),
Action\ShortCode\ResolveUrlAction::getRouteDef(),
Action\ShortCode\ListShortCodesAction::getRouteDef(),

View file

@ -77,7 +77,6 @@ abstract class AbstractCreateShortCodeAction extends AbstractRestAction
->withScheme($this->domainConfig['schema'])
->withHost($this->domainConfig['hostname']);
// TODO Make response to be generated based on Accept header
return new JsonResponse([
'longUrl' => (string) $longUrl,
'shortUrl' => (string) $shortUrl,

View file

@ -0,0 +1,47 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\Rest\Middleware\ShortCode;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Zend\Diactoros\Response;
use Zend\Diactoros\Response\JsonResponse;
class CreateShortCodeContentNegotiationMiddleware implements MiddlewareInterface
{
private const PLAIN_TEXT = 'text';
private const JSON = 'json';
/**
* Process an incoming server request and return a response, optionally delegating
* response creation to a handler.
* @throws \RuntimeException
* @throws \InvalidArgumentException
*/
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
/** @var JsonResponse $response */
$response = $handler->handle($request);
$acceptedType = $this->determineAcceptedType($request);
if ($acceptedType === self::JSON) {
return $response;
}
// If requested, return a plain text response containing the short URL only
$resp = (new Response())->withHeader('Content-Type', 'text/plain');
$body = $resp->getBody();
$body->write($response->getPayload()['shortUrl'] ?? '');
$body->rewind();
return $resp;
}
private function determineAcceptedType(ServerRequestInterface $request): string
{
$accepts = \explode(',', $request->getHeaderLine('Accept'));
$accept = \strtolower(\array_shift($accepts));
return \strpos($accept, 'text/plain') !== false ? self::PLAIN_TEXT : self::JSON;
}
}