]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/CoreBundle/Helper/Redirect.php
Added tests
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Helper / Redirect.php
CommitLineData
af497a64
NL
1<?php
2
3namespace Wallabag\CoreBundle\Helper;
4
5use Symfony\Component\Routing\Router;
65cd8a4a 6use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
f052f1fd 7use Wallabag\CoreBundle\Entity\Config;
af497a64 8
4086e078
NL
9/**
10 * Manage redirections to avoid redirecting to empty routes.
11 */
af497a64
NL
12class Redirect
13{
14 private $router;
65cd8a4a 15 private $tokenStorage;
a42f38d9 16 private $actionMarkAsRead;
af497a64 17
65cd8a4a 18 public function __construct(Router $router, TokenStorageInterface $tokenStorage)
af497a64
NL
19 {
20 $this->router = $router;
65cd8a4a 21 $this->tokenStorage = $tokenStorage;
af497a64
NL
22 }
23
24 /**
25 * @param string $url URL to redirect
26 * @param string $fallback Fallback URL if $url is null
27 *
28 * @return string
29 */
30 public function to($url, $fallback = '')
31 {
65cd8a4a
NL
32 $user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null;
33
34 if (null === $user || !is_object($user)) {
35 return $url;
36 }
37
38 if (Config::REDIRECT_TO_HOMEPAGE === $user->getConfig()->getActionMarkAsRead()) {
a42f38d9
NL
39 return $this->router->generate('homepage');
40 }
41
4086e078
NL
42 if (null !== $url) {
43 return $url;
44 }
af497a64 45
4086e078
NL
46 if ('' === $fallback) {
47 return $this->router->generate('homepage');
af497a64
NL
48 }
49
4086e078 50 return $fallback;
af497a64
NL
51 }
52}