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;
use Symfony\Component\Security\Core\SecurityContextInterface;
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
{
protected $securityContext;
2015-03-01 07:22:29 +00:00
protected $defaultTheme;
2015-03-01 07:22:29 +00:00
/**
* @param SecurityContextInterface $securityContext Needed to retrieve the current user
* @param string $defaultTheme Default theme when user isn't logged in
*/
public function __construct(SecurityContextInterface $securityContext, $defaultTheme)
{
$this->securityContext = $securityContext;
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-10-06 18:51:40 +00:00
$token = $this->securityContext->getToken();
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();
}
}