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