]>
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 | /** | |
5dbf3f23 KD |
24 | * @param string $url URL to redirect |
25 | * @param string $fallback Fallback URL if $url is null | |
26 | * @param bool $ignoreActionMarkAsRead Ignore configured action when mark as read | |
af497a64 NL |
27 | * |
28 | * @return string | |
29 | */ | |
5dbf3f23 | 30 | public function to($url, $fallback = '', $ignoreActionMarkAsRead = false) |
af497a64 | 31 | { |
65cd8a4a NL |
32 | $user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null; |
33 | ||
2a1ceb67 | 34 | if (null === $user || !\is_object($user)) { |
65cd8a4a NL |
35 | return $url; |
36 | } | |
37 | ||
5dbf3f23 KD |
38 | if (!$ignoreActionMarkAsRead && |
39 | Config::REDIRECT_TO_HOMEPAGE === $user->getConfig()->getActionMarkAsRead()) { | |
a42f38d9 NL |
40 | return $this->router->generate('homepage'); |
41 | } | |
42 | ||
4086e078 NL |
43 | if (null !== $url) { |
44 | return $url; | |
45 | } | |
af497a64 | 46 | |
4086e078 NL |
47 | if ('' === $fallback) { |
48 | return $this->router->generate('homepage'); | |
af497a64 NL |
49 | } |
50 | ||
4086e078 | 51 | return $fallback; |
af497a64 NL |
52 | } |
53 | } |