Completed
Push — master ( e8cbe7...f3c40f )
by Victor
21:02 queued 11:32
created

ThemeService::makeTheme()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 13
nc 2
nop 3
dl 0
loc 18
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author Philipp Schaffrath <[email protected]>
4
 *
5
 * @copyright Copyright (c) 2017, ownCloud GmbH
6
 * @license AGPL-3.0
7
 *
8
 * This code is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License, version 3,
10
 * as published by the Free Software Foundation.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License, version 3,
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
19
 */
20
namespace OC\Theme;
21
22
use OCP\Theme\IThemeService;
23
24
class ThemeService implements IThemeService {
25
26
	const DEFAULT_THEME_PATH = '/themes/default';
27
28
	/**
29
	 * @var Theme
30
	 */
31
	private $theme;
32
33
	/** @var string */
34
	private $defaultThemeDirectory;
35
36
	/**
37
	 * ThemeService constructor.
38
	 *
39
	 * @param string $themeName
40
	 */
41
	public function __construct($themeName = '') {
42
		$this->defaultThemeDirectory = \OC::$SERVERROOT . self::DEFAULT_THEME_PATH;
43
44
		if ($themeName === '' && $this->defaultThemeExists()) {
45
			$themeName = 'default';
46
		}
47
48
		$this->theme = $this->makeTheme($themeName, false);
49
	}
50
51
	/**
52
	 * @return bool
53
	 */
54
	public function defaultThemeExists() {
55
		return is_dir($this->defaultThemeDirectory);
56
	}
57
58
	/**
59
	 * @return Theme
60
	 */
61
	public function getTheme() {
62
		return $this->theme;
63
	}
64
65
	/**
66
	 * @param string $themeName
67
	 */
68
	public function setAppTheme($themeName = '') {
69
		$this->theme = $this->makeTheme($themeName, true, $this->getTheme());
70
	}
71
72
	/**
73
	 * @param string $themeName
74
	 * @param bool $appTheme
75
	 * @param Theme $theme
76
	 * @return Theme
77
	 */
78
	private function makeTheme($themeName, $appTheme = true, Theme $theme = null) {
79
		$directory = $this->getDirectory($themeName, $appTheme);
80
		$webPath = $this->getWebPath($themeName, $appTheme);
81
82
		if (is_null($theme)) {
83
			$theme = new Theme(
84
				$themeName,
85
				$directory,
86
				$webPath
0 ignored issues
show
Security Bug introduced by
It seems like $webPath defined by $this->getWebPath($themeName, $appTheme) on line 80 can also be of type false; however, OC\Theme\Theme::__construct() does only seem to accept string, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
87
			);
88
		} else {
89
			$theme->setName($themeName);
90
			$theme->setDirectory($directory);
91
			$theme->setWebPath($webPath);
0 ignored issues
show
Security Bug introduced by
It seems like $webPath defined by $this->getWebPath($themeName, $appTheme) on line 80 can also be of type false; however, OC\Theme\Theme::setWebPath() does only seem to accept string, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
92
		}
93
94
		return $theme;
95
	}
96
97
	/**
98
	 * @param string $themeName
99
	 * @param bool $appTheme
100
	 * @return string
101
	 */
102
	private function getDirectory($themeName, $appTheme = true) {
103
		if ($themeName !== '') {
104
			if ($appTheme) {
105
				return substr(\OC_App::getAppPath($themeName), strlen(\OC::$SERVERROOT) + 1);
106
			}
107
			return 'themes/' . $themeName;
108
		}
109
110
		return '';
111
	}
112
113
	/**
114
	 * @param $themeName
115
	 * @param bool $appTheme
116
	 * @return false|string
117
	 */
118
	private function getWebPath($themeName, $appTheme = true) {
119
		if ($themeName !== '') {
120
			if ($appTheme) {
121
				$appWebPath = \OC_App::getAppWebPath($themeName);
122
				return $appWebPath ? $appWebPath : '';
123
			}
124
			return '/themes/' . $themeName;
125
		}
126
127
		return '';
128
	}
129
130
	/**
131
	 * @return Theme[]
132
	 */
133
	public function getAllThemes() {
134
		return array_merge($this->getAllAppThemes(), $this->getAllLegacyThemes());
135
	}
136
137
	/**
138
	 * @return Theme[]
139
	 */
140
	private function getAllAppThemes() {
141
		$themes = [];
142
		foreach (\OC::$server->getAppManager()->getAllApps() as $app) {
143
			if (\OC_App::isType($app, 'theme')) {
144
				$themes[$app] = $this->makeTheme($app);
145
			}
146
		}
147
		return $themes;
148
	}
149
150
	/**
151
	 * @return Theme[]
152
	 */
153
	private function getAllLegacyThemes() {
154
		$themes = [];
155
		if (is_dir(\OC::$SERVERROOT . '/themes')) {
156
			if ($handle = opendir(\OC::$SERVERROOT . '/themes')) {
157
				while (false !== ($entry = readdir($handle))) {
158
					if ($entry === '.' || $entry === '..') {
159
						continue;
160
					}
161
					if (is_dir(\OC::$SERVERROOT . '/themes/' . $entry)) {
162
						$themes[$entry] = $this->makeTheme($entry, false);
163
					}
164
				}
165
				closedir($handle);
166
				return $themes;
167
			}
168
		}
169
		return $themes;
170
	}
171
172
	/**
173
	 * @param string $themeName
174
	 * @return Theme|false
175
	 */
176
	public function findTheme($themeName) {
177
		$allThemes = $this->getAllThemes();
178
		if (array_key_exists($themeName, $allThemes)) {
179
			return $allThemes[$themeName];
180
		}
181
		return false;
182
	}
183
}
184