Save where a visitor is redirected for any kind of tracked visit

This commit is contained in:
Alejandro Celaya
2024-11-24 13:21:48 +01:00
parent 89f70114e4
commit 86cc2b717c
6 changed files with 32 additions and 13 deletions

View File

@@ -10,7 +10,9 @@ use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Shlinkio\Shlink\Core\Visit\RequestTrackerInterface;
class NotFoundTrackerMiddleware implements MiddlewareInterface
use const Shlinkio\Shlink\REDIRECT_URL_REQUEST_ATTRIBUTE;
readonly class NotFoundTrackerMiddleware implements MiddlewareInterface
{
public function __construct(private RequestTrackerInterface $requestTracker)
{
@@ -18,7 +20,12 @@ class NotFoundTrackerMiddleware implements MiddlewareInterface
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$this->requestTracker->trackNotFoundIfApplicable($request);
return $handler->handle($request);
$response = $handler->handle($request);
$this->requestTracker->trackNotFoundIfApplicable($request->withAttribute(
REDIRECT_URL_REQUEST_ATTRIBUTE,
$response->hasHeader('Location') ? $response->getHeaderLine('Location') : null,
));
return $response;
}
}