1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Shlinkio\Shlink\Rest\Middleware\ShortUrl; |
6
|
|
|
|
7
|
|
|
use Laminas\Diactoros\Response; |
8
|
|
|
use Laminas\Diactoros\Response\JsonResponse; |
9
|
|
|
use Psr\Http\Message\ResponseInterface; |
10
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
11
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
12
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
13
|
|
|
|
14
|
|
|
use function array_shift; |
15
|
|
|
use function explode; |
16
|
|
|
use function strpos; |
17
|
|
|
use function strtolower; |
18
|
|
|
|
19
|
|
|
class CreateShortUrlContentNegotiationMiddleware implements MiddlewareInterface |
20
|
|
|
{ |
21
|
|
|
private const PLAIN_TEXT = 'text'; |
22
|
|
|
private const JSON = 'json'; |
23
|
|
|
|
24
|
13 |
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
25
|
|
|
{ |
26
|
13 |
|
$response = $handler->handle($request); |
27
|
|
|
|
28
|
|
|
// If the response is not JSON, return it as is |
29
|
13 |
|
if (! $response instanceof JsonResponse) { |
30
|
1 |
|
return $response; |
31
|
|
|
} |
32
|
|
|
|
33
|
12 |
|
$query = $request->getQueryParams(); |
34
|
12 |
|
$acceptedType = isset($query['format']) |
35
|
7 |
|
? $this->determineAcceptTypeFromQuery($query) |
36
|
12 |
|
: $this->determineAcceptTypeFromHeader($request->getHeaderLine('Accept')); |
37
|
|
|
|
38
|
|
|
// If JSON was requested, return the response from next handler as is |
39
|
12 |
|
if ($acceptedType === self::JSON) { |
40
|
6 |
|
return $response; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
// If requested, return a plain text response containing the short URL only |
44
|
6 |
|
$resp = (new Response())->withHeader('Content-Type', 'text/plain'); |
45
|
6 |
|
$body = $resp->getBody(); |
46
|
6 |
|
$body->write($this->determineBody($response)); |
47
|
6 |
|
$body->rewind(); |
48
|
6 |
|
return $resp; |
49
|
|
|
} |
50
|
|
|
|
51
|
7 |
|
private function determineAcceptTypeFromQuery(array $query): string |
52
|
|
|
{ |
53
|
7 |
|
if (! isset($query['format'])) { |
54
|
|
|
return self::JSON; |
55
|
|
|
} |
56
|
|
|
|
57
|
7 |
|
$format = strtolower($query['format']); |
58
|
7 |
|
return $format === 'txt' ? self::PLAIN_TEXT : self::JSON; |
59
|
|
|
} |
60
|
|
|
|
61
|
5 |
|
private function determineAcceptTypeFromHeader(string $acceptValue): string |
62
|
|
|
{ |
63
|
5 |
|
$accepts = explode(',', $acceptValue); |
64
|
5 |
|
$accept = strtolower(array_shift($accepts)); |
65
|
5 |
|
return strpos($accept, 'text/plain') !== false ? self::PLAIN_TEXT : self::JSON; |
66
|
|
|
} |
67
|
|
|
|
68
|
6 |
|
private function determineBody(JsonResponse $resp): string |
69
|
|
|
{ |
70
|
6 |
|
$payload = $resp->getPayload(); |
71
|
6 |
|
return $payload['shortUrl'] ?? $payload['error'] ?? ''; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|