Passed
Push — master ( e1cb1b...9a76f0 )
by John
15:33 queued 17s
created

ThemeInjectionService   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 26
c 1
b 0
f 0
dl 0
loc 57
rs 10
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A addThemeHeader() 0 10 1
A __construct() 0 6 1
A injectHeaders() 0 23 4
1
<?php
2
/**
3
 * @copyright Copyright (c) 2022 John Molakvoæ <[email protected]>
4
 *
5
 * @author John Molakvoæ <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
namespace OCA\Theming\Service;
24
25
use OCA\Theming\Themes\DefaultTheme;
26
use OCP\IURLGenerator;
27
use OCP\Util;
28
29
class ThemeInjectionService {
30
31
	private IURLGenerator $urlGenerator;
32
	private ThemesService $themesService;
33
	private DefaultTheme $defaultTheme;
34
35
	public function __construct(IURLGenerator $urlGenerator,
36
								ThemesService $themesService,
37
								DefaultTheme $defaultTheme) {
38
		$this->urlGenerator = $urlGenerator;
39
		$this->themesService = $themesService;
40
		$this->defaultTheme = $defaultTheme;
41
	}
42
43
	public function injectHeaders() {
44
		$themes = $this->themesService->getThemes();
45
		$defaultTheme = $themes[$this->defaultTheme->getId()];
46
		$mediaThemes = array_filter($themes, function($theme) {
47
			// Check if the theme provides a media query
48
			return (bool)$theme->getMediaQuery();
49
		});
50
51
		// Default theme fallback
52
		$this->addThemeHeader($defaultTheme->getId());
53
		
54
		// Themes applied by media queries
55
		foreach($mediaThemes as $theme) {
56
			$this->addThemeHeader($theme->getId(), true, $theme->getMediaQuery());
57
		}
58
59
		// Themes 
60
		foreach($this->themesService->getThemes() as $theme) {
61
			// Ignore default theme as already processed first
62
			if ($theme->getId() === $this->defaultTheme->getId()) {
63
				continue;
64
			}
65
			$this->addThemeHeader($theme->getId(), false);
66
		}
67
	}
68
69
	/**
70
	 * Inject theme header into rendered page
71
	 * 
72
	 * @param string $themeId the theme ID
73
	 * @param bool $plain request the :root syntax
74
	 * @param string $media media query to use in the <link> element
75
	 */
76
	private function addThemeHeader(string $themeId, bool $plain = true, string $media = null) {
77
		$linkToCSS = $this->urlGenerator->linkToRoute('theming.Theming.getThemeStylesheet', [
78
			'themeId' => $themeId,
79
			'plain' => $plain,
80
		]);
81
		Util::addHeader('link', [
82
			'rel' => 'stylesheet',
83
			'media' => $media,
84
			'href' => $linkToCSS,
85
			'class' => 'theme'
86
		]);
87
	}
88
}
89