Passed
Push — master ( 7e7284...62fa85 )
by Roeland
17:42 queued 12s
created

TemplateController::create()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
nc 2
nop 3
dl 0
loc 5
c 1
b 0
f 0
cc 2
rs 10
1
<?php
2
/**
3
 * @copyright Copyright (c) 2021 Julius Härtl <[email protected]>
4
 *
5
 * @author Julius Härtl <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
declare(strict_types=1);
25
26
namespace OCA\Files\Controller;
27
28
use OCP\AppFramework\Http\DataResponse;
29
use OCP\AppFramework\OCS\OCSForbiddenException;
30
use OCP\AppFramework\OCSController;
31
use OCP\Files\GenericFileException;
32
use OCP\Files\Template\ITemplateManager;
33
use OCP\IRequest;
34
35
class TemplateController extends OCSController {
36
	protected $templateManager;
37
38
	public function __construct($appName, IRequest $request, ITemplateManager $templateManager) {
39
		parent::__construct($appName, $request);
40
		$this->templateManager = $templateManager;
41
	}
42
43
	/**
44
	 * @NoAdminRequired
45
	 */
46
	public function list(): DataResponse {
47
		return new DataResponse($this->templateManager->listTemplates());
48
	}
49
50
	/**
51
	 * @NoAdminRequired
52
	 * @throws OCSForbiddenException
53
	 */
54
	public function create(string $filePath, string $templatePath = '', string $templateType = 'user'): DataResponse {
55
		try {
56
			return new DataResponse($this->templateManager->createFromTemplate($filePath, $templatePath, $templateType));
57
		} catch (GenericFileException $e) {
58
			throw new OCSForbiddenException($e->getMessage());
59
		}
60
	}
61
62
	/**
63
	 * @NoAdminRequired
64
	 */
65
	public function path(string $templatePath = '', bool $copySystemTemplates = false) {
66
		try {
67
			$templatePath = $this->templateManager->initializeTemplateDirectory($templatePath, null, $copySystemTemplates);
68
			return new DataResponse([
69
				'template_path' => $templatePath,
70
				'templates' => $this->templateManager->listCreators()
71
			]);
72
		} catch (\Exception $e) {
73
			throw new OCSForbiddenException($e->getMessage());
74
		}
75
	}
76
}
77