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

73 lines
1.9 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.
*
2022-10-04 00:31:43 +00:00
* If no user where logged in, it will return the default theme
2015-03-01 07:22:29 +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;
protected $themes;
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
* @param array $themes Themes come from the LiipThemeBundle (liip_theme.themes)
2015-03-01 07:22:29 +00:00
*/
public function __construct(TokenStorageInterface $tokenStorage, $defaultTheme, $themes)
{
2015-11-06 23:17:37 +00:00
$this->tokenStorage = $tokenStorage;
2015-03-01 07:22:29 +00:00
$this->defaultTheme = $defaultTheme;
$this->themes = $themes;
}
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
2017-07-01 07:52:38 +00:00
if (null === $token) {
2015-10-06 18:51:40 +00:00
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;
}
if (!\in_array($config->getTheme(), $this->themes, true)) {
return $this->defaultTheme;
}
return $config->getTheme();
}
}