Files
wallabag/src/Helper/Redirect.php

60 lines
1.6 KiB
PHP
Raw Normal View History

<?php
2024-02-19 01:30:12 +01:00
namespace Wallabag\Helper;
use GuzzleHttp\Psr7\Uri;
2022-11-14 23:11:46 +01:00
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
2016-11-07 10:26:05 +01:00
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
2024-02-19 01:30:12 +01:00
use Wallabag\Entity\Config;
use Wallabag\Entity\User;
2016-04-15 09:58:29 +02:00
/**
* Manage redirections to avoid redirecting to empty routes.
*/
class Redirect
{
public function __construct(
2025-04-05 13:59:36 +02:00
private readonly UrlGeneratorInterface $router,
private readonly TokenStorageInterface $tokenStorage,
) {
}
/**
* @param string $url URL to redirect
* @param bool $ignoreActionMarkAsRead Ignore configured action when mark as read
*
* @return string
*/
public function to($url, $ignoreActionMarkAsRead = false)
{
2016-11-07 10:26:05 +01:00
$user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null;
2022-11-23 15:51:33 +01:00
if (!$user instanceof User) {
if (null === $url) {
return $this->router->generate('homepage');
}
if (!Uri::isAbsolutePathReference(new Uri($url))) {
return $this->router->generate('homepage');
}
2016-11-07 10:26:05 +01:00
return $url;
}
2024-01-01 19:11:01 +01:00
if (!$ignoreActionMarkAsRead
&& Config::REDIRECT_TO_HOMEPAGE === $user->getConfig()->getActionMarkAsRead()) {
return $this->router->generate('homepage');
}
if (null === $url) {
return $this->router->generate('homepage');
}
if (!Uri::isAbsolutePathReference(new Uri($url))) {
return $this->router->generate('homepage');
2016-04-15 09:58:29 +02:00
}
return $url;
}
}