Passed
Pull Request — master (#5614)
by Angel Fernando Quiroz
07:17
created

ThemeHelper::getVisualTheme()   B

Complexity

Conditions 7
Paths 17

Size

Total Lines 37
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 18
nc 17
nop 0
dl 0
loc 37
rs 8.8333
c 1
b 0
f 0
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\Bridge\Twig\Extension\AssetExtension;
12
use Symfony\Component\DependencyInjection\Attribute\Autowire;
13
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
14
15
final class ThemeHelper
16
{
17
    public const DEFAULT_THEME = 'chamilo';
18
19
    public function __construct(
20
        private readonly AccessUrlHelper $accessUrlHelper,
21
        private readonly SettingsManager $settingsManager,
22
        private readonly UserHelper $userHelper,
23
        private readonly CidReqHelper $cidReqHelper,
24
        private readonly SettingsCourseManager $settingsCourseManager,
25
        private readonly UrlGeneratorInterface $urlGenerator,
26
        #[Autowire(service: 'twig.extension.assets')]
27
        private readonly AssetExtension $assetExtension,
28
    ) {}
29
30
    /**
31
     * Returns the name of the color theme configured to be applied on the current page.
32
     * The returned name depends on the platform, course or user settings.
33
     */
34
    public function getVisualTheme(): string
35
    {
36
        static $visualTheme;
37
38
        if (isset($visualTheme)) {
39
            return $visualTheme;
40
        }
41
42
        $accessUrl = $this->accessUrlHelper->getCurrent();
43
44
        $visualTheme = $accessUrl->getActiveColorTheme()?->getColorTheme()->getSlug();
45
46
        if ('true' == $this->settingsManager->getSetting('profile.user_selected_theme')) {
47
            $visualTheme = $this->userHelper->getCurrent()?->getTheme();
48
        }
49
50
        if ('true' == $this->settingsManager->getSetting('course.allow_course_theme')) {
51
            $course = $this->cidReqHelper->getCourseEntity();
52
53
            if ($course) {
54
                $this->settingsCourseManager->setCourse($course);
55
56
                $visualTheme = $this->settingsCourseManager->getSetting('course_theme');
57
58
                if (1 === (int) $this->settingsCourseManager->getSetting('allow_learning_path_theme')) {
59
                    global $lp_theme_css;
60
61
                    $visualTheme = $lp_theme_css;
62
                }
63
            }
64
        }
65
66
        if (empty($visualTheme)) {
67
            return self::DEFAULT_THEME;
68
        }
69
70
        return $visualTheme;
71
    }
72
73
    public function getThemeAssetUrl(string $path, bool $absolute = false): string
74
    {
75
        $themeName = $this->getVisualTheme();
76
77
        if (empty($themeName)) {
78
            return '';
79
        }
80
81
        $base = '/';
82
83
        if ($absolute) {
84
            $base = $this->urlGenerator->generate('index', [], UrlGeneratorInterface::ABSOLUTE_URL);
85
        }
86
87
        return $this->assetExtension->getAssetUrl($base."build/css/themes/$themeName/$path");
88
    }
89
90
    public function getThemeAssetLinkTag(string $path, bool $absoluteUrl = false): string
91
    {
92
        $url = $this->getThemeAssetUrl($path, $absoluteUrl);
93
94
        if (empty($url)) {
95
            return '';
96
        }
97
98
        return sprintf('<link rel="stylesheet" href="%s">', $url);
99
    }
100
}
101