Passed
Push — master ( ed7adc...d02aa6 )
by Yannick
09:57
created

ThemeHelper::getPreferredLogoUrl()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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