aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/CoreBundle/Helper/DetectActiveTheme.php
blob: 679186c08415f3b9cd54c35edeafe1ea68ec5490 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php

namespace Wallabag\CoreBundle\Helper;

use Liip\ThemeBundle\Helper\DeviceDetectionInterface;
use Symfony\Component\Security\Core\SecurityContextInterface;
use Wallabag\UserBundle\Entity\User;

/**
 * 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;
    protected $defaultTheme;

    /**
     * @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;
        $this->defaultTheme = $defaultTheme;
    }

    public function setUserAgent($userAgent)
    {
    }

    /**
     * This should return the active theme for the logged in user.
     *
     * Default theme for:
     *     - anonymous user
     *     - user without a config (shouldn't happen ..)
     *
     * @return string
     */
    public function getType()
    {
        $user = $this->securityContext->getToken()->getUser();

        if (!$user instanceof User) {
            return $this->defaultTheme;
        }

        $config = $user->getConfig();

        if (!$config) {
            return $this->defaultTheme;
        }

        return $config->getTheme();
    }
}