Passed
Push — master ( 11b018...e5557a )
by Maxence
05:01 queued 01:42
created

TemplatesService::initWebsiteFolder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
c 0
b 0
f 0
rs 10
cc 1
eloc 2
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\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 TemplatesService {
39
40
	const TEMPLATES = ['sample_pico', 'empty'];
41
	const TEMPLATE_DIR = __DIR__ . '/../../Pico/templates/';
42
43
	/** @var IL10N */
44
	private $l10n;
45
46
	/** @var ConfigService */
47
	private $configService;
48
49
	/** @var FileService */
50
	private $fileService;
51
52
	/** @var MiscService */
53
	private $miscService;
54
55
	/** @var Folder */
56
	private $websiteFolder;
57
58
	/**
59
	 * TemplatesService constructor.
60
	 *
61
	 * @param IL10N $l10n
62
	 * @param ConfigService $configService
63
	 * @param FileService $fileService
64
	 * @param MiscService $miscService
65
	 */
66 View Code Duplication
	function __construct(
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...
67
		IL10N $l10n, ConfigService $configService, FileService $fileService,
68
		MiscService $miscService
69
	) {
70
		$this->l10n = $l10n;
71
		$this->configService = $configService;
72
		$this->fileService = $fileService;
73
		$this->miscService = $miscService;
74
	}
75
76
77
	/**
78
	 * check if template exist.
79
	 *
80
	 * @param string $template
81
	 *
82
	 * @throws TemplateDoesNotExistException
83
	 */
84
	public function templateHasToExist($template) {
85
		if (!in_array($template, $this->getTemplatesList())) {
86
			throw new TemplateDoesNotExistException($this->l10n->t('Template does not exist'));
87
		}
88
	}
89
90
91
	/**
92
	 * returns all templates available to users.
93
	 *
94
	 * @param bool $customOnly
95
	 *
96
	 * @return array
97
	 */
98 View Code Duplication
	public function getTemplatesList($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...
99
		$templates = [];
100
		if ($customOnly !== true) {
101
			$templates = self::TEMPLATES;
102
		}
103
104
		$customs = json_decode($this->configService->getAppValue(ConfigService::CUSTOM_TEMPLATES), true);
105
		if ($customs !== null) {
106
			$templates = array_merge($templates, $customs);
107
		}
108
109
		return $templates;
110
	}
111
112
113
	/**
114
	 * returns theme from the Pico/templates/ dir that are not available yet to users.
115
	 *
116
	 * @return array
117
	 */
118 View Code Duplication
	public function getNewTemplatesList() {
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...
119
120
		$newTemplates = [];
121
		$currTemplates = $this->getTemplatesList();
122
		$allTemplates = $this->fileService->getDirectoriesFromAppDataFolder(PicoService::DIR_TEMPLATES);
123
		foreach ($allTemplates as $template) {
124
			if (!in_array($template, $currTemplates)) {
125
				$newTemplates[] = $template;
126
			}
127
		}
128
129
		return $newTemplates;
130
	}
131
132
133
	/**
134
	 * Install templates into a new website.
135
	 * Templates will be parsed and formatted in the process.
136
	 *
137
	 * @param Website $website
138
	 */
139
	public function installTemplates(Website $website) {
140
141
		$dir = $this->fileService->getAppDataFolderAbsolutePath(PicoService::DIR_TEMPLATES);
142
		$dir .= MiscService::endSlash($website->getTemplateSource());
143
144
		$files = $this->fileService->getAppDataFiles($dir);
145
146
		$this->initWebsiteFolder($website);
147
		$data = $this->generateData($website);
148
		foreach ($files as $file) {
149
150
			if (substr($file, -1) === '/') {
151
				continue;
152
			}
153
154
			$template = new TemplateFile($dir, $file);
155
			$template->applyData($data);
156
			$this->generateFile($template, $website);
157
		}
158
	}
159
160
161
	/**
162
	 * @param TemplateFile $file
163
	 * @param Website $website
164
	 *
165
	 * @throws WriteAccessException
166
	 */
167
	private function generateFile(TemplateFile $file, Website $website) {
168
		try {
169
			$this->initFolder(pathinfo($website->getPath() . $file->getFilename(), PATHINFO_DIRNAME));
170
171
			if (substr($file->getFilename(), -5) === 'empty') {
172
				return;
173
			}
174
175
			$new = $this->websiteFolder->newFile($website->getPath() . $file->getFilename());
176
			$new->putContent($file->getContent());
177
		} catch (Exception $e) {
178
			throw new WriteAccessException(
179
				$this->l10n->t('Cannot generate template file in this folder')
180
			);
181
		}
182
183
	}
184
185
186
	/**
187
	 * @param Website $website
188
	 */
189
	private function initWebsiteFolder(Website $website) {
190
		$this->websiteFolder = \OC::$server->getUserFolder($website->getUserId());
191
		$this->initFolder($website->getPath());
192
	}
193
194
195
	/**
196
	 * @param Website $website
197
	 *
198
	 * @return array
199
	 */
200
	private function generateData(Website $website) {
201
		return [
202
			'site_title' => $website->getName(),
203
			'base_url'   => \OC::$WEBROOT . $website->getSite()
204
		];
205
	}
206
207
208
	/**
209
	 * @param $path
210
	 */
211
	private function initFolder($path) {
212
213
		if (!$this->websiteFolder->nodeExists($path)) {
214
			$this->websiteFolder->newFolder($path);
215
		}
216
	}
217
218
219
}