wallabag/src/Helper/Redirect.php

63 lines
1.7 KiB
PHP
Raw Normal View History

<?php
2024-02-19 00:30:12 +00:00
namespace Wallabag\Helper;
use GuzzleHttp\Psr7\Uri;
2022-11-14 22:11:46 +00:00
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
2016-11-07 09:26:05 +00:00
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
2024-02-19 00:30:12 +00:00
use Wallabag\Entity\Config;
use Wallabag\Entity\User;
2016-04-15 07:58:29 +00:00
/**
* Manage redirections to avoid redirecting to empty routes.
*/
class Redirect
{
private $router;
2016-11-07 09:26:05 +00:00
private $tokenStorage;
2022-11-14 22:11:46 +00:00
public function __construct(UrlGeneratorInterface $router, TokenStorageInterface $tokenStorage)
{
$this->router = $router;
2016-11-07 09:26:05 +00:00
$this->tokenStorage = $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 09:26:05 +00:00
$user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null;
2022-11-23 14:51:33 +00: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 09:26:05 +00:00
return $url;
}
2024-01-01 18:11:01 +00: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 07:58:29 +00:00
}
return $url;
}
}