Passed
Push — master ( fa41e0...4bd63f )
by Daniel
07:07 queued 12s
created

PluginsController::copyDummyPlugin()   A

Complexity

Conditions 4
Paths 7

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
eloc 9
c 0
b 0
f 0
dl 0
loc 12
ccs 0
cts 9
cp 0
rs 9.9666
cc 4
nc 7
nop 1
crap 20
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\PluginAlreadyExistsException;
30
use OCA\CMSPico\Exceptions\PluginNotFoundException;
31
use OCA\CMSPico\Service\PluginsService;
32
use OCP\AppFramework\Controller;
33
use OCP\AppFramework\Http\DataResponse;
34
use OCP\IL10N;
35
use OCP\ILogger;
36
use OCP\IRequest;
37
38
class PluginsController extends Controller
39
{
40
	use ControllerTrait;
41
42
	/** @var IL10N */
43
	private $l10n;
44
45
	/** @var PluginsService */
46
	private $pluginsService;
47
48
	/**
49
	 * PluginsController constructor.
50
	 *
51
	 * @param IRequest       $request
52
	 * @param IL10N          $l10n
53
	 * @param ILogger        $logger
54
	 * @param PluginsService $pluginsService
55
	 */
56 1
	public function __construct(IRequest $request, IL10N $l10n, ILogger $logger, PluginsService $pluginsService)
57
	{
58 1
		parent::__construct(Application::APP_NAME, $request);
59
60 1
		$this->l10n = $l10n;
61 1
		$this->logger = $logger;
62 1
		$this->pluginsService = $pluginsService;
63 1
	}
64
65
	/**
66
	 * @return DataResponse
67
	 */
68 6
	public function getPlugins(): DataResponse
69
	{
70
		try {
71
			$data = [
72 6
				'systemItems' => $this->pluginsService->getSystemPlugins(),
73 6
				'customItems' => $this->pluginsService->getCustomPlugins(),
74 6
				'newItems' => $this->pluginsService->getNewCustomPlugins(),
75
			];
76
77 6
			return new DataResponse($data);
78
		} catch (\Throwable $e) {
79
			return $this->createErrorResponse($e);
80
		}
81
	}
82
83
	/**
84
	 * @param string $item
85
	 *
86
	 * @return DataResponse
87
	 */
88 2
	public function addCustomPlugin(string $item): DataResponse
89
	{
90
		try {
91 2
			$this->pluginsService->publishCustomPlugin($item);
92
93 2
			return $this->getPlugins();
94
		} catch (PluginNotFoundException $e) {
95
			return $this->createErrorResponse($e, [ 'error' => $this->l10n->t('Plugin not found.') ]);
96
		} catch (PluginAlreadyExistsException $e) {
97
			return $this->createErrorResponse($e, [ 'error' => $this->l10n->t('Plugin exists already.') ]);
98
		} catch (\Throwable $e) {
99
			return $this->createErrorResponse($e);
100
		}
101
	}
102
103
	/**
104
	 * @param string $item
105
	 *
106
	 * @return DataResponse
107
	 */
108 1
	public function updateCustomPlugin(string $item): DataResponse
109
	{
110
		try {
111 1
			$this->pluginsService->depublishCustomPlugin($item);
112 1
			$this->pluginsService->publishCustomPlugin($item);
113
114 1
			return $this->getPlugins();
115
		} catch (PluginNotFoundException $e) {
116
			return $this->createErrorResponse($e, [ 'error' => $this->l10n->t('Plugin not found.') ]);
117
		} catch (\Throwable $e) {
118
			return $this->createErrorResponse($e);
119
		}
120
	}
121
122
	/**
123
	 * @param string $item
124
	 *
125
	 * @return DataResponse
126
	 */
127 1
	public function removeCustomPlugin(string $item): DataResponse
128
	{
129
		try {
130 1
			$this->pluginsService->depublishCustomPlugin($item);
131
132 1
			return $this->getPlugins();
133
		} catch (PluginNotFoundException $e) {
134
			return $this->createErrorResponse($e, [ 'error' => $this->l10n->t('Plugin not found.') ]);
135
		} catch (\Throwable $e) {
136
			return $this->createErrorResponse($e);
137
		}
138
	}
139
140
	/**
141
	 * @param string $name
142
	 *
143
	 * @return DataResponse
144
	 */
145
	public function copyDummyPlugin(string $name): DataResponse
146
	{
147
		try {
148
			$this->pluginsService->copyDummyPlugin($name);
149
150
			return $this->getPlugins();
151
		} catch (PluginNotFoundException $e) {
152
			return $this->createErrorResponse($e, [ 'error' => $this->l10n->t('Plugin not found.') ]);
153
		} catch (PluginAlreadyExistsException $e) {
154
			return $this->createErrorResponse($e, [ 'error' => $this->l10n->t('Plugin exists already.') ]);
155
		} catch (\Exception $e) {
156
			return $this->createErrorResponse($e);
157
		}
158
	}
159
}
160