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

46 lines
1.1 KiB
PHP
Raw Normal View History

<?php
namespace Wallabag\CoreBundle\Helper;
use Symfony\Component\Routing\Router;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
2016-11-07 08:30:37 +00:00
use Wallabag\CoreBundle\Entity\Config;
2016-04-15 07:58:29 +00:00
/**
* Manage redirections to avoid redirecting to empty routes.
*/
class Redirect
{
private $router;
private $actionMarkAsRead;
public function __construct(Router $router, TokenStorage $token)
{
$this->router = $router;
$this->actionMarkAsRead = $token->getToken()->getUser()->getConfig()->getActionMarkAsRead();
}
/**
* @param string $url URL to redirect
* @param string $fallback Fallback URL if $url is null
*
* @return string
*/
public function to($url, $fallback = '')
{
2016-11-07 08:30:37 +00:00
if (Config::REDIRECT_TO_HOMEPAGE === $this->actionMarkAsRead) {
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;
}
}