Passed
Push — master ( cb6173...c886d0 )
by Daniel
53:00
created

TemplatesController::addCustomTemplate()   A

Complexity

Conditions 4
Paths 7

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 8.7414

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 12
ccs 3
cts 9
cp 0.3333
rs 9.9666
cc 4
nc 7
nop 1
crap 8.7414
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\Controller;
27
28
use OCA\CMSPico\AppInfo\Application;
29
use OCA\CMSPico\Exceptions\TemplateAlreadyExistsException;
30
use OCA\CMSPico\Exceptions\TemplateNotFoundException;
31
use OCA\CMSPico\Service\TemplatesService;
32
use OCP\AppFramework\Controller;
33
use OCP\AppFramework\Http;
34
use OCP\AppFramework\Http\DataResponse;
35
use OCP\IL10N;
36
use OCP\ILogger;
37
use OCP\IRequest;
38
39
class TemplatesController extends Controller
40
{
41
	use ControllerTrait;
42
43
	/** @var IL10N */
44
	private $l10n;
45
46
	/** @var TemplatesService */
47
	private $templatesService;
48
49
	/**
50
	 * TemplatesController constructor.
51
	 *
52
	 * @param IRequest         $request
53
	 * @param IL10N            $l10n
54
	 * @param ILogger          $logger
55
	 * @param TemplatesService $templatesService
56
	 */
57 1
	public function __construct(IRequest $request, IL10N $l10n, ILogger $logger, TemplatesService $templatesService)
58
	{
59 1
		parent::__construct(Application::APP_NAME, $request);
60
61 1
		$this->l10n = $l10n;
62 1
		$this->logger = $logger;
63 1
		$this->templatesService = $templatesService;
64 1
	}
65
66
	/**
67
	 * @return DataResponse
68
	 */
69 5
	public function getTemplates(): DataResponse
70
	{
71
		try {
72
			$data = [
73 5
				'systemItems' => $this->templatesService->getSystemTemplates(),
74 5
				'customItems' => $this->templatesService->getCustomTemplates(),
75 5
				'newItems' => $this->templatesService->getNewCustomTemplates(),
76
			];
77
78 5
			return new DataResponse($data);
79
		} catch (\Throwable $e) {
80
			return $this->createErrorResponse($e);
81
		}
82
	}
83
84
	/**
85
	 * @param string $item
86
	 *
87
	 * @return DataResponse
88
	 */
89 1
	public function addCustomTemplate(string $item): DataResponse
90
	{
91
		try {
92 1
			$this->templatesService->registerCustomTemplate($item);
93
94 1
			return $this->getTemplates();
95
		} catch (TemplateNotFoundException $e) {
96
			return $this->createErrorResponse($e, [ 'error' => $this->l10n->t('Template not found.') ]);
97
		} catch (TemplateAlreadyExistsException $e) {
98
			return $this->createErrorResponse($e, [ 'error' => $this->l10n->t('Template exists already.') ]);
99
		} catch (\Throwable $e) {
100
			return $this->createErrorResponse($e);
101
		}
102
	}
103
104
	/**
105
	 * @param string $item
106
	 *
107
	 * @return DataResponse
108
	 */
109 1
	public function removeCustomTemplate(string $item): DataResponse
110
	{
111
		try {
112 1
			$this->templatesService->removeCustomTemplate($item);
113
114 1
			return $this->getTemplates();
115
		} catch (TemplateNotFoundException $e) {
116
			return $this->createErrorResponse($e, [ 'error' => $this->l10n->t('Template not found.') ]);
117
		} catch (\Throwable $e) {
118
			return $this->createErrorResponse($e);
119
		}
120
	}
121
122
	/**
123
	 * @param string $item
124
	 * @param string $name
125
	 *
126
	 * @return DataResponse
127
	 */
128 1
	public function copyTemplate(string $item, string $name): DataResponse
129
	{
130
		try {
131 1
			$this->templatesService->copyTemplate($item, $name);
132
133 1
			return $this->getTemplates();
134
		} catch (TemplateNotFoundException $e) {
135
			return $this->createErrorResponse($e, [ 'error' => $this->l10n->t('Template not found.') ]);
136
		} catch (TemplateAlreadyExistsException $e) {
137
			return $this->createErrorResponse($e, [ 'error' => $this->l10n->t('Template exists already.') ]);
138
		} catch (\Throwable $e) {
139
			return $this->createErrorResponse($e);
140
		}
141
	}
142
}
143