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

PluginsController::addCustomPlugin()   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\PluginAlreadyExistsException;
30
use OCA\CMSPico\Exceptions\PluginNotFoundException;
31
use OCA\CMSPico\Service\PluginsService;
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 PluginsController extends Controller
40
{
41
	use ControllerTrait;
42
43
	/** @var IL10N */
44
	private $l10n;
45
46
	/** @var PluginsService */
47
	private $pluginsService;
48
49
	/**
50
	 * PluginsController constructor.
51
	 *
52
	 * @param IRequest       $request
53
	 * @param IL10N          $l10n
54
	 * @param ILogger        $logger
55
	 * @param PluginsService $pluginsService
56
	 */
57 1
	public function __construct(IRequest $request, IL10N $l10n, ILogger $logger, PluginsService $pluginsService)
58
	{
59 1
		parent::__construct(Application::APP_NAME, $request);
60
61 1
		$this->l10n = $l10n;
62 1
		$this->logger = $logger;
63 1
		$this->pluginsService = $pluginsService;
64 1
	}
65
66
	/**
67
	 * @return DataResponse
68
	 */
69 6
	public function getPlugins(): DataResponse
70
	{
71
		try {
72
			$data = [
73 6
				'systemItems' => $this->pluginsService->getSystemPlugins(),
74 6
				'customItems' => $this->pluginsService->getCustomPlugins(),
75 6
				'newItems' => $this->pluginsService->getNewCustomPlugins(),
76
			];
77
78 6
			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 2
	public function addCustomPlugin(string $item): DataResponse
90
	{
91
		try {
92 2
			$this->pluginsService->publishCustomPlugin($item);
93
94 2
			return $this->getPlugins();
95
		} catch (PluginNotFoundException $e) {
96
			return $this->createErrorResponse($e, [ 'error' => $this->l10n->t('Plugin not found.') ]);
97
		} catch (PluginAlreadyExistsException $e) {
98
			return $this->createErrorResponse($e, [ 'error' => $this->l10n->t('Plugin 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 updateCustomPlugin(string $item): DataResponse
110
	{
111
		try {
112 1
			$this->pluginsService->depublishCustomPlugin($item);
113 1
			$this->pluginsService->publishCustomPlugin($item);
114
115 1
			return $this->getPlugins();
116
		} catch (PluginNotFoundException $e) {
117
			return $this->createErrorResponse($e, [ 'error' => $this->l10n->t('Plugin not found.') ]);
118
		} catch (\Throwable $e) {
119
			return $this->createErrorResponse($e);
120
		}
121
	}
122
123
	/**
124
	 * @param string $item
125
	 *
126
	 * @return DataResponse
127
	 */
128 1
	public function removeCustomPlugin(string $item): DataResponse
129
	{
130
		try {
131 1
			$this->pluginsService->depublishCustomPlugin($item);
132
133 1
			return $this->getPlugins();
134
		} catch (PluginNotFoundException $e) {
135
			return $this->createErrorResponse($e, [ 'error' => $this->l10n->t('Plugin not found.') ]);
136
		} catch (\Throwable $e) {
137
			return $this->createErrorResponse($e);
138
		}
139
	}
140
}
141