Passed
Pull Request — master (#99)
by Daniel
34:06
created

PluginsController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 10
cc 1
nc 1
nop 4
crap 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\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
		$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, Http::STATUS_OK);
78
	}
79
80
	/**
81
	 * @param string $item
82
	 *
83
	 * @return DataResponse
84
	 */
85 2
	public function addCustomPlugin(string $item): DataResponse
86
	{
87
		try {
88 2
			$this->pluginsService->publishCustomPlugin($item);
89
90 2
			return $this->getPlugins();
91
		} catch (PluginNotFoundException $e) {
92
			return $this->createErrorResponse($e, [ 'error' => $this->l10n->t('Plugin not found.') ]);
93
		} catch (PluginAlreadyExistsException $e) {
94
			return $this->createErrorResponse($e, [ 'error' => $this->l10n->t('Plugin exists already.') ]);
95
		} catch (\Exception $e) {
96
			return $this->createErrorResponse($e);
97
		}
98
	}
99
100
	/**
101
	 * @param string $item
102
	 *
103
	 * @return DataResponse
104
	 */
105 1
	public function updateCustomPlugin(string $item): DataResponse
106
	{
107
		try {
108 1
			$this->pluginsService->depublishCustomPlugin($item);
109 1
			$this->pluginsService->publishCustomPlugin($item);
110
111 1
			return $this->getPlugins();
112
		} catch (PluginNotFoundException $e) {
113
			return $this->createErrorResponse($e, [ 'error' => $this->l10n->t('Plugin not found.') ]);
114
		} catch (\Exception $e) {
115
			return $this->createErrorResponse($e);
116
		}
117
	}
118
119
	/**
120
	 * @param string $item
121
	 *
122
	 * @return DataResponse
123
	 */
124 1
	public function removeCustomPlugin(string $item): DataResponse
125
	{
126
		try {
127 1
			$this->pluginsService->depublishCustomPlugin($item);
128
129 1
			return $this->getPlugins();
130
		} catch (PluginNotFoundException $e) {
131
			return $this->createErrorResponse($e, [ 'error' => $this->l10n->t('Plugin not found.') ]);
132
		} catch (\Exception $e) {
133
			return $this->createErrorResponse($e);
134
		}
135
	}
136
137
	/**
138
	 * @param string $name
139
	 *
140
	 * @return DataResponse
141
	 */
142
	public function copyDummyPlugin(string $name): DataResponse
143
	{
144
		try {
145
			$this->pluginsService->copyDummyPlugin($name);
146
147
			return $this->getPlugins();
148
		} catch (PluginNotFoundException $e) {
149
			return $this->createErrorResponse($e, [ 'error' => $this->l10n->t('Plugin not found.') ]);
150
		} catch (PluginAlreadyExistsException $e) {
151
			return $this->createErrorResponse($e, [ 'error' => $this->l10n->t('Plugin exists already.') ]);
152
		} catch (\Exception $e) {
153
			return $this->createErrorResponse($e);
154
		}
155
	}
156
}
157