Passed
Pull Request — master (#6396)
by Angel Fernando Quiroz
08:39
created

ThemeHelper::getAssetBase64Encoded()   A

Complexity

Conditions 3
Paths 6

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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