-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathHandler.php
44 lines (40 loc) · 1.28 KB
/
Handler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<?php
declare(strict_types=1);
namespace Stuff\Webclient\Extension\Redirect;
use Nyholm\Psr7\Response;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
class Handler implements RequestHandlerInterface
{
/**
* @param ServerRequestInterface $request
* @return ResponseInterface
*/
public function handle(ServerRequestInterface $request): ResponseInterface
{
$status = 200;
$query = $request->getQueryParams();
$headers = [
'Content-Type' => 'text/plain',
];
$visit = 0;
if (array_key_exists('visit', $query) && (int)$query['visit'] > 0) {
$visit = (int)$query['visit'];
}
$visit++;
$redirects = 0;
if (array_key_exists('redirects', $query) && (int)$query['redirects'] > 0) {
$redirects = (int)$query['redirects'] - 1;
}
if ($redirects > 0) {
$get = http_build_query([
'redirects' => $redirects,
'visit' => $visit,
]);
$headers['Location'] = $request->getUri()->withQuery($get)->__toString();
$status = 302;
}
return new Response($status, $headers, (string)$visit);
}
}