wallabag/src/Wallabag/CoreBundle/Helper/DetectActiveTheme.php

66 lines
1.6 KiB
PHP
Raw Normal View History

<?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;
use Wallabag\UserBundle\Entity\User;
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
*/
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-03-01 07:22:29 +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-11-06 23:17:37 +00:00
$this->tokenStorage = $tokenStorage;
2015-03-01 07:22:29 +00:00
$this->defaultTheme = $defaultTheme;
}
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:
* - anonymous user
2015-03-01 07:22:29 +00:00
* - user without a config (shouldn't happen ..)
*
* @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();
if (!$user instanceof User) {
2015-03-01 07:22:29 +00:00
return $this->defaultTheme;
}
$config = $user->getConfig();
if (!$config) {
2015-03-01 07:22:29 +00:00
return $this->defaultTheme;
}
return $config->getTheme();
}
}