]> git.immae.eu Git - github/wallabag/wallabag.git/blobdiff - src/Wallabag/CoreBundle/Helper/Redirect.php
php-cs-fixer
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Helper / Redirect.php
index 59172db40110d628a223a750af6a88fc718ded36..9d1a6345d37dca0ca1509fe57761c3e8c64da26b 100644 (file)
@@ -3,7 +3,7 @@
 namespace Wallabag\CoreBundle\Helper;
 
 use Symfony\Component\Routing\Router;
-use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
+use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
 use Wallabag\CoreBundle\Entity\Config;
 
 /**
@@ -12,23 +12,31 @@ use Wallabag\CoreBundle\Entity\Config;
 class Redirect
 {
     private $router;
-    private $actionMarkAsRead;
+    private $tokenStorage;
 
-    public function __construct(Router $router, TokenStorage $token)
+    public function __construct(Router $router, TokenStorageInterface $tokenStorage)
     {
         $this->router = $router;
-        $this->actionMarkAsRead = $token->getToken()->getUser()->getConfig()->getActionMarkAsRead();
+        $this->tokenStorage = $tokenStorage;
     }
 
     /**
-     * @param string $url      URL to redirect
-     * @param string $fallback Fallback URL if $url is null
+     * @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 = '')
+    public function to($url, $fallback = '', $ignoreActionMarkAsRead = false)
     {
-        if (Config::REDIRECT_TO_HOMEPAGE === $this->actionMarkAsRead) {
+        $user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null;
+
+        if (null === $user || !\is_object($user)) {
+            return $url;
+        }
+
+        if (!$ignoreActionMarkAsRead &&
+              Config::REDIRECT_TO_HOMEPAGE === $user->getConfig()->getActionMarkAsRead()) {
             return $this->router->generate('homepage');
         }