2015-02-23 21:55:06 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Wallabag\CoreBundle\Helper;
|
|
|
|
|
|
|
|
use Liip\ThemeBundle\Helper\DeviceDetectionInterface;
|
2015-11-06 23:17:37 +00:00
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
|
2015-10-02 12:51:41 +00:00
|
|
|
use Wallabag\UserBundle\Entity\User;
|
2015-02-23 21:55:06 +00:00
|
|
|
|
2015-03-01 07:22:29 +00:00
|
|
|
/**
|
|
|
|
* This class intend to detect the active theme for the logged in user.
|
|
|
|
* It will retrieve the configured theme of the user.
|
|
|
|
*
|
|
|
|
* If no user where logged in, it will returne the default theme
|
|
|
|
*/
|
2015-02-23 21:55:06 +00:00
|
|
|
class DetectActiveTheme implements DeviceDetectionInterface
|
|
|
|
{
|
2015-11-06 23:17:37 +00:00
|
|
|
protected $tokenStorage;
|
2015-03-01 07:22:29 +00:00
|
|
|
protected $defaultTheme;
|
2015-02-23 21:55:06 +00:00
|
|
|
|
2015-03-01 07:22:29 +00:00
|
|
|
/**
|
2015-11-06 23:27:41 +00:00
|
|
|
* @param TokenStorageInterface $tokenStorage Needed to retrieve the current user
|
|
|
|
* @param string $defaultTheme Default theme when user isn't logged in
|
2015-03-01 07:22:29 +00:00
|
|
|
*/
|
2015-11-06 23:17:37 +00:00
|
|
|
public function __construct(TokenStorageInterface $tokenStorage, $defaultTheme)
|
2015-02-23 21:55:06 +00:00
|
|
|
{
|
2015-11-06 23:17:37 +00:00
|
|
|
$this->tokenStorage = $tokenStorage;
|
2015-03-01 07:22:29 +00:00
|
|
|
$this->defaultTheme = $defaultTheme;
|
2015-02-23 21:55:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function setUserAgent($userAgent)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This should return the active theme for the logged in user.
|
2015-03-01 07:22:29 +00:00
|
|
|
*
|
|
|
|
* Default theme for:
|
2015-02-23 21:55:06 +00:00
|
|
|
* - anonymous user
|
2015-03-01 07:22:29 +00:00
|
|
|
* - user without a config (shouldn't happen ..)
|
2015-02-23 21:55:06 +00:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getType()
|
|
|
|
{
|
2015-11-06 23:17:37 +00:00
|
|
|
$token = $this->tokenStorage->getToken();
|
2015-10-06 18:51:40 +00:00
|
|
|
|
|
|
|
if (is_null($token)) {
|
|
|
|
return $this->defaultTheme;
|
|
|
|
}
|
|
|
|
|
|
|
|
$user = $token->getUser();
|
2015-02-23 21:55:06 +00:00
|
|
|
|
|
|
|
if (!$user instanceof User) {
|
2015-03-01 07:22:29 +00:00
|
|
|
return $this->defaultTheme;
|
2015-02-23 21:55:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$config = $user->getConfig();
|
|
|
|
|
|
|
|
if (!$config) {
|
2015-03-01 07:22:29 +00:00
|
|
|
return $this->defaultTheme;
|
2015-02-23 21:55:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $config->getTheme();
|
|
|
|
}
|
|
|
|
}
|