Passed
Pull Request — master (#77)
by Daniel
47:29
created

TemplatesService::installTemplates()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.6
c 0
b 0
f 0
cc 3
nc 3
nop 1
1
<?php
2
/**
3
 * CMS Pico - Create websites using Pico CMS for Nextcloud.
4
 *
5
 * @copyright Copyright (c) 2017, Maxence Lange (<[email protected]>)
6
 * @copyright Copyright (c) 2019, Daniel Rudolf (<[email protected]>)
7
 *
8
 * @license GNU AGPL version 3 or any later version
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License as
12
 * published by the Free Software Foundation, either version 3 of the
13
 * License, or (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU Affero General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public License
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
 */
23
24
declare(strict_types=1);
25
26
namespace OCA\CMSPico\Service;
27
28
use OCA\CMSPico\Exceptions\TemplateNotFoundException;
29
use OCA\CMSPico\Files\FileInterface;
30
use OCA\CMSPico\Files\FolderInterface;
31
use OCA\CMSPico\Files\StorageFolder;
32
use OCA\CMSPico\Model\TemplateFile;
33
use OCA\CMSPico\Model\Website;
34
use OCP\Files\NotFoundException;
35
36
class TemplatesService
37
{
38
	/** @var ConfigService */
39
	private $configService;
40
41
	/** @var FileService */
42
	private $fileService;
43
44
	/**
45
	 * TemplatesService constructor.
46
	 *
47
	 * @param ConfigService $configService
48
	 * @param FileService   $fileService
49
	 */
50 1
	public function __construct(ConfigService $configService, FileService $fileService)
51
	{
52 1
		$this->configService = $configService;
53 1
		$this->fileService = $fileService;
54 1
	}
55
56
	/**
57
	 * check if template exist.
58
	 *
59
	 * @param string $template
60
	 *
61
	 * @throws TemplateNotFoundException
62
	 */
63 4
	public function assertValidTemplate($template)
64
	{
65 4
		if (!in_array($template, $this->getTemplates())) {
66 1
			throw new TemplateNotFoundException();
67
		}
68 4
	}
69
70
	/**
71
	 * @return string[]
72
	 */
73 4
	public function getTemplates(): array
74
	{
75 4
		return array_merge($this->getSystemTemplates(), $this->getCustomTemplates());
76
	}
77
78
	/**
79
	 * @return string[]
80
	 */
81 4
	public function getSystemTemplates(): array
82
	{
83 4
		$systemTemplatesFolder = $this->fileService->getSystemFolder(PicoService::DIR_TEMPLATES);
84 4
		$systemTemplatesFolder->sync(FolderInterface::SYNC_SHALLOW);
85
86 4
		$systemTemplates = [];
87 4
		foreach ($systemTemplatesFolder as $templateFolder) {
88 4
			if ($templateFolder->isFolder()) {
89 4
				$systemTemplates[] = $templateFolder->getName();
90
			}
91
		}
92
93 4
		return $systemTemplates;
94
	}
95
96
	/**
97
	 * @return string[]
98
	 */
99 4
	public function getCustomTemplates(): array
100
	{
101 4
		$json = $this->configService->getAppValue(ConfigService::CUSTOM_TEMPLATES);
102 4
		return $json ? json_decode($json, true) : [];
103
	}
104
105
	/**
106
	 * @return string[]
107
	 */
108 1
	public function getNewCustomTemplates(): array
109
	{
110 1
		$currentTemplates = $this->getTemplates();
111
112 1
		$customTemplatesFolder = $this->fileService->getAppDataFolder(PicoService::DIR_TEMPLATES);
113 1
		$customTemplatesFolder->sync(FolderInterface::SYNC_SHALLOW);
114
115 1
		$newTemplates = [];
116 1
		foreach ($customTemplatesFolder as $templateFolder) {
117 1
			$template = $templateFolder->getName();
118 1
			if ($templateFolder->isFolder() && !in_array($template, $currentTemplates)) {
119 1
				$newTemplates[] = $template;
120
			}
121
		}
122
123 1
		return $newTemplates;
124
	}
125
126
	/**
127
	 * @param Website $website
128
	 *
129
	 * @throws TemplateNotFoundException
130
	 */
131 3
	public function installTemplate(Website $website)
132
	{
133 3
		$userFolder = new StorageFolder(\OC::$server->getUserFolder($website->getUserId()));
134
135
		try {
136 3
			$userFolder->get($website->getPath());
137
138
			// website folder exists; since we don't want to
139
			// mess around with a user's files, bail out
140
			return;
141 3
		} catch (NotFoundException $e) {
142
			// proceed if the website folder doesn't exist yet
143
		}
144
145 3
		$websiteFolder = $userFolder->newFolder($website->getPath());
146
147 3
		$templateFolder = $this->getTemplateFolder($website);
148 3
		$templateFolder->sync();
149
150 3
		$templateData = $this->getTemplateData($website);
151 3
		foreach (new \RecursiveIteratorIterator($templateFolder) as $file) {
152
			/** @var FileInterface $file */
153 3
			$templateFile = new TemplateFile($file);
154
155
			try {
156 3
				$targetFolder = $websiteFolder->getFolder($templateFile->getParent());
157 3
			} catch (NotFoundException $e) {
158 3
				$targetFolder = $websiteFolder->newFolder($templateFile->getParent());
159
			}
160
161 3
			if ($templateFile->getName() === 'empty') {
162
				continue;
163
			}
164
165 3
			$templateFile->setTemplateData($templateData);
166 3
			$templateFile->copy($targetFolder);
167
		}
168 3
	}
169
170
	/**
171
	 * @param Website $website
172
	 *
173
	 * @return array<string,string>
174
	 */
175 3
	private function getTemplateData(Website $website): array
176
	{
177
		return [
178 3
			'site_title' => $website->getName()
179
		];
180
	}
181
182
	/**
183
	 * @param Website $website
184
	 *
185
	 * @return FolderInterface
186
	 * @throws TemplateNotFoundException
187
	 */
188 3
	public function getTemplateFolder(Website $website): FolderInterface
189
	{
190 3
		$templateName = $website->getTemplateSource();
191 3
		if (!$templateName) {
192
			throw new TemplateNotFoundException();
193
		}
194
195 3
		$systemTemplatesFolder = $this->fileService->getSystemFolder(PicoService::DIR_TEMPLATES);
196 3
		$systemTemplatesFolder->sync(FolderInterface::SYNC_SHALLOW);
197
198 3
		$customTemplatesFolder = $this->fileService->getAppDataFolder(PicoService::DIR_TEMPLATES);
199 3
		$customTemplatesFolder->sync(FolderInterface::SYNC_SHALLOW);
200
201
		try {
202 3
			$templateFolder = $systemTemplatesFolder->getFolder($templateName);
203
		} catch (NotFoundException $e) {
204
			try {
205
				$templateFolder = $customTemplatesFolder->getFolder($templateName);
206
			} catch (NotFoundException $e) {
207
				throw new TemplateNotFoundException();
208
			}
209
		}
210
211 3
		return $templateFolder->fakeRoot();
212
	}
213
}
214