wallabag/src/Wallabag/CoreBundle/Helper/Redirect.php

55 lines
1.5 KiB
PHP
Raw Normal View History

<?php
namespace Wallabag\CoreBundle\Helper;
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;
2016-11-07 08:30:37 +00:00
use Wallabag\CoreBundle\Entity\Config;
2022-11-23 14:51:33 +00:00
use Wallabag\UserBundle\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 string $fallback Fallback URL if $url is null
* @param bool $ignoreActionMarkAsRead Ignore configured action when mark as read
*
* @return string
*/
public function to($url, $fallback = '', $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) {
2016-11-07 09:26:05 +00:00
return $url;
}
if (!$ignoreActionMarkAsRead &&
Config::REDIRECT_TO_HOMEPAGE === $user->getConfig()->getActionMarkAsRead()) {
return $this->router->generate('homepage');
}
2016-04-15 07:58:29 +00:00
if (null !== $url) {
return $url;
}
2016-04-15 07:58:29 +00:00
if ('' === $fallback) {
return $this->router->generate('homepage');
}
2016-04-15 07:58:29 +00:00
return $fallback;
}
}