Passed
Push — master ( c59208...8079ea )
by Maxence
02:12
created

ThemesService   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 82
Duplicated Lines 31.71 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
c 1
b 0
f 0
dl 26
loc 82
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getDirectoriesFromThemesDir() 13 13 4
A __construct() 0 4 1
A getNewThemesList() 0 12 3
A getThemesList() 12 12 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\WriteAccessException;
33
use OCA\CMSPico\Model\TemplateFile;
34
use OCA\CMSPico\Model\Website;
35
use OCP\Files\Folder;
36
use OCP\IL10N;
37
38
class ThemesService {
39
40
	const THEMES = ['default'];
41
	const THEMES_DIR = __DIR__ . '/../../Pico/themes/';
42
43
	/** @var IL10N */
44
	private $l10n;
45
46
	/** @var ConfigService */
47
	private $configService;
48
49
	/** @var MiscService */
50
	private $miscService;
51
52
	/**
53
	 * ThemesService constructor.
54
	 *
55
	 * @param IL10N $l10n
56
	 * @param ConfigService $configService
57
	 * @param MiscService $miscService
58
	 */
59
	function __construct(IL10N $l10n, ConfigService $configService, MiscService $miscService) {
60
		$this->l10n = $l10n;
61
		$this->configService = $configService;
62
		$this->miscService = $miscService;
63
	}
64
65
66
	/**
67
	 * @param bool $customOnly
68
	 *
69
	 * @return array
70
	 */
71 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...
72
		$themes = [];
73
		if ($customOnly !== true) {
74
			$themes = self::THEMES;
75
		}
76
77
		$customs = json_decode($this->configService->getAppValue(ConfigService::CUSTOM_THEMES), true);
78
		if ($customs !== null) {
79
			$themes = array_merge($themes, $customs);
80
		}
81
82
		return $themes;
83
	}
84
85
86
	/**
87
	 * @return array
88
	 */
89
	public function getNewThemesList() {
90
91
		$newThemes = [];
92
		$currThemes = $this->getThemesList();
93
		$allThemes = $this->getDirectoriesFromThemesDir();
94
		foreach ($allThemes as $theme) {
95
			if (!in_array($theme, $currThemes)) {
96
				$newThemes[] = $theme;
97
			}
98
		}
99
100
		return $newThemes;
101
	}
102
103
104
	/**
105
	 * @return array
106
	 */
107 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...
108
109
		$allThemes = [];
110
		foreach (new DirectoryIterator(self::THEMES_DIR) as $file) {
111
112
			if (!$file->isDir() || substr($file->getFilename(), 0, 1) === '.') {
113
				continue;
114
			}
115
116
			$allThemes[] = $file->getFilename();
117
		}
118
119
		return $allThemes;
120
	}
121
122
}