Passed
Pull Request — master (#5614)
by Angel Fernando Quiroz
11:40
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 League\Flysystem\FilesystemException;
12
use League\Flysystem\FilesystemOperator;
13
use Symfony\Component\DependencyInjection\Attribute\Autowire;
14
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
15
use Symfony\Component\Routing\RouterInterface;
16
17
final class ThemeHelper
18
{
19
    public const DEFAULT_THEME = 'chamilo';
20
21
    public function __construct(
22
        private readonly AccessUrlHelper $accessUrlHelper,
23
        private readonly SettingsManager $settingsManager,
24
        private readonly UserHelper $userHelper,
25
        private readonly CidReqHelper $cidReqHelper,
26
        private readonly SettingsCourseManager $settingsCourseManager,
27
        private readonly RouterInterface $router,
28
        #[Autowire(service: 'oneup_flysystem.themes_filesystem')] private readonly FilesystemOperator $filesystem,
29
    ) {}
30
31
    /**
32
     * Returns the name of the color theme configured to be applied on the current page.
33
     * The returned name depends on the platform, course or user settings.
34
     */
35
    public function getVisualTheme(): string
36
    {
37
        static $visualTheme;
38
39
        global $lp_theme_css;
40
41
        if (isset($visualTheme)) {
42
            return $visualTheme;
43
        }
44
45
        $accessUrl = $this->accessUrlHelper->getCurrent();
46
47
        $visualTheme = $accessUrl->getActiveColorTheme()?->getColorTheme()->getSlug();
48
49
        if ('true' == $this->settingsManager->getSetting('profile.user_selected_theme')) {
50
            $visualTheme = $this->userHelper->getCurrent()?->getTheme();
51
        }
52
53
        if ('true' == $this->settingsManager->getSetting('course.allow_course_theme')) {
54
            $course = $this->cidReqHelper->getCourseEntity();
55
56
            if ($course) {
57
                $this->settingsCourseManager->setCourse($course);
58
59
                $visualTheme = $this->settingsCourseManager->getCourseSettingValue('course_theme');
60
61
                if (1 === (int) $this->settingsCourseManager->getCourseSettingValue('allow_learning_path_theme')) {
62
                    $visualTheme = $lp_theme_css;
63
                }
64
            }
65
        }
66
67
        if (empty($visualTheme)) {
68
            return self::DEFAULT_THEME;
69
        }
70
71
        return $visualTheme;
72
    }
73
74
    public function getThemeAssetUrl(string $path, bool $absolute = false): string
75
    {
76
        $themeName = $this->getVisualTheme();
77
78
        try {
79
            if (!$this->filesystem->fileExists($themeName.DIRECTORY_SEPARATOR.$path)) {
80
                return '';
81
            }
82
        } catch (FilesystemException) {
83
            return '';
84
        }
85
86
        return $this->router->generate(
87
            'theme_asset',
88
            ['name' => $themeName, 'path' => $path],
89
            $absolute ? UrlGeneratorInterface::ABSOLUTE_URL : UrlGeneratorInterface::ABSOLUTE_PATH
90
        );
91
    }
92
93
    public function getThemeAssetLinkTag(string $path, bool $absoluteUrl = false): string
94
    {
95
        $url = $this->getThemeAssetUrl($path, $absoluteUrl);
96
97
        if (empty($url)) {
98
            return '';
99
        }
100
101
        return sprintf('<link rel="stylesheet" href="%s">', $url);
102
    }
103
}
104