Completed
Push — master ( 5a81b7...a6ad21 )
by Thomas
25:30
created

ThemeService::getThemeDirectory()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 7
rs 9.4285
c 1
b 0
f 0
1
<?php
2
3
namespace OC\Theme;
4
5
class ThemeService {
6
7
	/**
8
	 * @var Theme
9
	 */
10
	private $theme;
11
12
	/** @var string */
13
	private $defaultThemeDirectory;
14
15
	/**
16
	 * ThemeService constructor.
17
	 *
18
	 * @param string $themeName
19
	 * @param string $defaultThemeDirectory
20
	 */
21
	public function __construct($themeName = '', $defaultThemeDirectory = '') {
22
		$this->setDefaultThemeDirectory($defaultThemeDirectory);
23
		$this->createTheme($themeName);
24
	}
25
26
	/**
27
	 * @param string $defaultThemeDirectory
28
	 */
29
	private function setDefaultThemeDirectory($defaultThemeDirectory = '') {
30
		if ($defaultThemeDirectory === '') {
31
			$this->defaultThemeDirectory = \OC::$SERVERROOT . '/themes/default';
32
		} else {
33
			$this->defaultThemeDirectory = $defaultThemeDirectory;
34
		}
35
	}
36
37
	/**
38
	 * @param string $themeName
39
	 */
40
	private function createTheme($themeName = '') {
41
		if ($themeName === '' && $this->defaultThemeExists()) {
42
			$themeName = 'default';
43
		}
44
45
		$this->theme = new Theme($themeName, $this->getThemeDirectory($themeName));
46
	}
47
48
	/**
49
	 * @param string $themeName
50
	 * @return string
51
	 */
52
	private function getThemeDirectory($themeName) {
53
		if ($themeName !== '') {
54
			return 'themes/' . $themeName . '/';
55
		} else {
56
			return '';
57
		}
58
	}
59
60
	/**
61
	 * @return bool
62
	 */
63
	private function defaultThemeExists() {
64
		if (is_dir($this->defaultThemeDirectory)) {
65
			return true;
66
		}
67
68
		return false;
69
	}
70
71
	/**
72
	 * @return Theme
73
	 */
74
	public function getTheme() {
75
		return $this->theme;
76
	}
77
78
	/**
79
	 * @param string $appName
80
	 */
81
	public function setAppTheme($appName = '') {
82
		if ($appName !== '') {
83
			$this->theme->setDirectory(
84
				ltrim(\OC_App::getAppWebPath($appName), '/') . '/'
85
			);
86
		}
87
	}
88
}
89