Passed
Push — master ( 4234e3...bd08a9 )
by Angel Fernando Quiroz
07:04
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 League\Flysystem\FilesystemException;
12
use League\Flysystem\FilesystemOperator;
13
use League\Flysystem\UnableToReadFile;
14
use League\MimeTypeDetection\ExtensionMimeTypeDetector;
15
use Symfony\Component\DependencyInjection\Attribute\Autowire;
16
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
17
use Symfony\Component\Routing\RouterInterface;
18
19
final class ThemeHelper
20
{
21
    public const DEFAULT_THEME = 'chamilo';
22
23
    public function __construct(
24
        private readonly AccessUrlHelper $accessUrlHelper,
25
        private readonly SettingsManager $settingsManager,
26
        private readonly UserHelper $userHelper,
27
        private readonly CidReqHelper $cidReqHelper,
28
        private readonly SettingsCourseManager $settingsCourseManager,
29
        private readonly RouterInterface $router,
30
        #[Autowire(service: 'oneup_flysystem.themes_filesystem')] private readonly FilesystemOperator $filesystem,
31
    ) {}
32
33
    /**
34
     * Returns the name of the color theme configured to be applied on the current page.
35
     * The returned name depends on the platform, course or user settings.
36
     */
37
    public function getVisualTheme(): string
38
    {
39
        static $visualTheme;
40
41
        global $lp_theme_css;
42
43
        if (isset($visualTheme)) {
44
            return $visualTheme;
45
        }
46
47
        $accessUrl = $this->accessUrlHelper->getCurrent();
48
49
        $visualTheme = $accessUrl->getActiveColorTheme()?->getColorTheme()->getSlug();
50
51
        if ('true' == $this->settingsManager->getSetting('profile.user_selected_theme')) {
52
            $visualTheme = $this->userHelper->getCurrent()?->getTheme();
53
        }
54
55
        if ('true' == $this->settingsManager->getSetting('course.allow_course_theme')) {
56
            $course = $this->cidReqHelper->getCourseEntity();
57
58
            if ($course) {
59
                $this->settingsCourseManager->setCourse($course);
60
61
                $visualTheme = $this->settingsCourseManager->getCourseSettingValue('course_theme');
62
63
                if (1 === (int) $this->settingsCourseManager->getCourseSettingValue('allow_learning_path_theme')) {
64
                    $visualTheme = $lp_theme_css;
65
                }
66
            }
67
        }
68
69
        if (empty($visualTheme)) {
70
            return self::DEFAULT_THEME;
71
        }
72
73
        return $visualTheme;
74
    }
75
76
    public function getThemeAssetUrl(string $path, bool $absoluteUrl = false): string
77
    {
78
        $themeName = $this->getVisualTheme();
79
80
        try {
81
            if (!$this->filesystem->fileExists($themeName.DIRECTORY_SEPARATOR.$path)) {
82
                return '';
83
            }
84
        } catch (FilesystemException) {
85
            return '';
86
        }
87
88
        return $this->router->generate(
89
            'theme_asset',
90
            ['name' => $themeName, 'path' => $path],
91
            $absoluteUrl ? UrlGeneratorInterface::ABSOLUTE_URL : UrlGeneratorInterface::ABSOLUTE_PATH
92
        );
93
    }
94
95
    public function getThemeAssetLinkTag(string $path, bool $absoluteUrl = false): string
96
    {
97
        $url = $this->getThemeAssetUrl($path, $absoluteUrl);
98
99
        if (empty($url)) {
100
            return '';
101
        }
102
103
        return sprintf('<link rel="stylesheet" href="%s">', $url);
104
    }
105
106
    public function getAssetContents(string $path): string
107
    {
108
        $themeName = $this->getVisualTheme();
109
        $fullPath = $themeName.DIRECTORY_SEPARATOR.$path;
110
111
        try {
112
            if ($this->filesystem->fileExists($fullPath)) {
113
                $stream = $this->filesystem->readStream($fullPath);
114
115
                return stream_get_contents($stream);
116
            }
117
        } catch (FilesystemException|UnableToReadFile) {
118
            return '';
119
        }
120
121
        return '';
122
    }
123
124
    public function getAssetBase64Encoded(string $path): string
125
    {
126
        $visualTheme = $this->getVisualTheme();
127
        $fullPath = $visualTheme.DIRECTORY_SEPARATOR.$path;
128
129
        try {
130
            if ($this->filesystem->fileExists($fullPath)) {
131
                $detector = new ExtensionMimeTypeDetector();
132
                $mimeType = (string) $detector->detectMimeTypeFromFile($fullPath);
133
134
                return 'data:'.$mimeType.';base64,'.base64_encode($this->getAssetContents($path));
135
            }
136
        } catch (FilesystemException) {
137
            return '';
138
        }
139
140
        return '';
141
    }
142
}
143