Passed
Pull Request — master (#5614)
by Angel Fernando Quiroz
08:35
created

ThemeHelper::getVisualTheme()   B

Complexity

Conditions 7
Paths 17

Size

Total Lines 37
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 7
eloc 18
c 2
b 0
f 0
nc 17
nop 0
dl 0
loc 37
rs 8.8333
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
declare(strict_types=1);
6
7
namespace Chamilo\CoreBundle\ServiceHelper;
8
9
use Chamilo\CoreBundle\Settings\SettingsManager;
10
use Chamilo\CourseBundle\Settings\SettingsCourseManager;
11
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
12
use Symfony\Component\Routing\RouterInterface;
13
14
final class ThemeHelper
15
{
16
    public const DEFAULT_THEME = 'chamilo';
17
18
    public function __construct(
19
        private readonly AccessUrlHelper $accessUrlHelper,
20
        private readonly SettingsManager $settingsManager,
21
        private readonly UserHelper $userHelper,
22
        private readonly CidReqHelper $cidReqHelper,
23
        private readonly SettingsCourseManager $settingsCourseManager,
24
        private readonly RouterInterface $router,
25
    ) {}
26
27
    /**
28
     * Returns the name of the color theme configured to be applied on the current page.
29
     * The returned name depends on the platform, course or user settings.
30
     */
31
    public function getVisualTheme(): string
32
    {
33
        static $visualTheme;
34
35
        global $lp_theme_css;
36
37
        if (isset($visualTheme)) {
38
            return $visualTheme;
39
        }
40
41
        $accessUrl = $this->accessUrlHelper->getCurrent();
42
43
        $visualTheme = $accessUrl->getActiveColorTheme()?->getColorTheme()->getSlug();
44
45
        if ('true' == $this->settingsManager->getSetting('profile.user_selected_theme')) {
46
            $visualTheme = $this->userHelper->getCurrent()?->getTheme();
47
        }
48
49
        if ('true' == $this->settingsManager->getSetting('course.allow_course_theme')) {
50
            $course = $this->cidReqHelper->getCourseEntity();
51
52
            if ($course) {
53
                $this->settingsCourseManager->setCourse($course);
54
55
                $visualTheme = $this->settingsCourseManager->getCourseSettingValue('course_theme');
56
57
                if (1 === (int) $this->settingsCourseManager->getCourseSettingValue('allow_learning_path_theme')) {
58
                    $visualTheme = $lp_theme_css;
59
                }
60
            }
61
        }
62
63
        if (empty($visualTheme)) {
64
            return self::DEFAULT_THEME;
65
        }
66
67
        return $visualTheme;
68
    }
69
70
    public function getThemeAssetUrl(string $path, bool $absolute = false): string
71
    {
72
        $themeName = $this->getVisualTheme();
73
74
        if (empty($themeName)) {
75
            return '';
76
        }
77
78
        return $this->router->generate(
79
            'theme_asset',
80
            ['name' => $themeName, 'path' => $path],
81
            $absolute ? UrlGeneratorInterface::ABSOLUTE_URL : UrlGeneratorInterface::ABSOLUTE_PATH
82
        );
83
    }
84
85
    public function getThemeAssetLinkTag(string $path, bool $absoluteUrl = false): string
86
    {
87
        $url = $this->getThemeAssetUrl($path, $absoluteUrl);
88
89
        if (empty($url)) {
90
            return '';
91
        }
92
93
        return sprintf('<link rel="stylesheet" href="%s">', $url);
94
    }
95
}
96