Passed
Push — master ( d11704...85c314 )
by Maxence
02:03
created

ThemesService::hasToBeAValidTheme()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 2
eloc 3
nc 1
nop 1
1
<?php
2
/**
3
 * CMS Pico - Integration of Pico within your files to create websites.
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 *
8
 * @author Maxence Lange <[email protected]>
9
 * @copyright 2017
10
 * @license GNU AGPL version 3 or any later version
11
 *
12
 * This program is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License as
14
 * published by the Free Software Foundation, either version 3 of the
15
 * License, or (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License
23
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24
 *
25
 */
26
27
namespace OCA\CMSPico\Service;
28
29
use DirectoryIterator;
30
use Exception;
31
use OCA\CMSPico\Exceptions\TemplateDoesNotExistException;
32
use OCA\CMSPico\Exceptions\ThemeDoesNotExistException;
33
use OCA\CMSPico\Exceptions\WriteAccessException;
34
use OCA\CMSPico\Model\TemplateFile;
35
use OCA\CMSPico\Model\Website;
36
use OCP\Files\Folder;
37
use OCP\IL10N;
38
39
class ThemesService {
40
41
	const THEMES = ['default'];
42
	const THEMES_DIR = __DIR__ . '/../../Pico/themes/';
43
44
	/** @var IL10N */
45
	private $l10n;
46
47
	/** @var ConfigService */
48
	private $configService;
49
50
	/** @var MiscService */
51
	private $miscService;
52
53
	/**
54
	 * ThemesService constructor.
55
	 *
56
	 * @param IL10N $l10n
57
	 * @param ConfigService $configService
58
	 * @param MiscService $miscService
59
	 */
60
	function __construct(IL10N $l10n, ConfigService $configService, MiscService $miscService) {
61
		$this->l10n = $l10n;
62
		$this->configService = $configService;
63
		$this->miscService = $miscService;
64
	}
65
66
67
	/**
68
	 * @param bool $customOnly
69
	 *
70
	 * @return array
71
	 */
72 View Code Duplication
	public function getThemesList($customOnly = false) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
73
		$themes = [];
74
		if ($customOnly !== true) {
75
			$themes = self::THEMES;
76
		}
77
78
		$customs = json_decode($this->configService->getAppValue(ConfigService::CUSTOM_THEMES), true);
79
		if ($customs !== null) {
80
			$themes = array_merge($themes, $customs);
81
		}
82
83
		return $themes;
84
	}
85
86
87
	/**
88
	 * @param $theme
89
	 *
90
	 * @throws ThemeDoesNotExistException
91
	 */
92
	public function hasToBeAValidTheme($theme) {
93
		$themes = $this->getThemesList();
94
		if (!in_array($theme, $themes)) {
95
			throw new ThemeDoesNotExistException($this->l10n->t('Theme does not exist'));
96
		}
97
	}
98
99
	/**
100
	 * @return array
101
	 */
102
	public function getNewThemesList() {
103
104
		$newThemes = [];
105
		$currThemes = $this->getThemesList();
106
		$allThemes = $this->getDirectoriesFromThemesDir();
107
		foreach ($allThemes as $theme) {
108
			if (!in_array($theme, $currThemes)) {
109
				$newThemes[] = $theme;
110
			}
111
		}
112
113
		return $newThemes;
114
	}
115
116
117
	/**
118
	 * @return array
119
	 */
120 View Code Duplication
	private function getDirectoriesFromThemesDir() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
121
122
		$allThemes = [];
123
		foreach (new DirectoryIterator(self::THEMES_DIR) as $file) {
124
125
			if (!$file->isDir() || substr($file->getFilename(), 0, 1) === '.') {
126
				continue;
127
			}
128
129
			$allThemes[] = $file->getFilename();
130
		}
131
132
		return $allThemes;
133
	}
134
135
}