Completed
Push — master ( d4ce93...77c8bc )
by Maxence
02:50
created

LinksController::createLink()   A

Complexity

Conditions 2
Paths 4

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 10
nc 4
nop 2
1
<?php
2
/**
3
 * Circles - Bring cloud-users closer together.
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 *
8
 * @author Maxence Lange <[email protected]>
9
 * @copyright 2017
10
 * @license GNU AGPL version 3 or any later version
11
 *
12
 * This program is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License as
14
 * published by the Free Software Foundation, either version 3 of the
15
 * License, or (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License
23
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24
 *
25
 */
26
27
namespace OCA\Circles\Controller;
28
29
use Exception;
30
use OCA\Circles\Exceptions\CircleNameFirstCharException;
31
use OCA\Circles\Exceptions\CircleNameTooShortException;
32
use OCA\Circles\Exceptions\CircleTypeDisabledException;
33
use OCA\Circles\Exceptions\FederatedCircleNotAllowedException;
34
use OCA\Circles\Service\CirclesService;
35
use OCA\Circles\Service\ConfigService;
36
use OCA\Circles\Service\FederatedLinkCreationService;
37
use OCA\Circles\Service\FederatedLinkService;
38
use OCA\Circles\Service\MiscService;
39
use OCP\AppFramework\Controller;
40
use OCP\AppFramework\Http\DataResponse;
41
use OCP\IL10N;
42
use OCP\IRequest;
43
44
class LinksController extends Controller {
45
46
	/** @var IL10N */
47
	protected $l10n;
48
49
	/** @var ConfigService */
50
	protected $configService;
51
52
	/** @var CirclesService */
53
	protected $circlesService;
54
55
	/** @var FederatedLinkService */
56
	protected $federatedLinkService;
57
58
	/** @var FederatedLinkCreationService */
59
	protected $federatedLinkCreationService;
60
61
	/** @var MiscService */
62
	protected $miscService;
63
64
65
	/**
66
	 * BaseController constructor.
67
	 *
68
	 * @param string $appName
69
	 * @param IRequest $request
70
	 * @param string $UserId
71
	 * @param IL10N $l10n
72
	 * @param ConfigService $configService
73
	 * @param CirclesService $circlesService
74
	 * @param FederatedLinkService $federatedLinkService
75
	 * @param FederatedLinkCreationService $federatedLinkCreationService
76
	 * @param MiscService $miscService
77
	 */
78
	public function __construct(
0 ignored issues
show
Coding Style Naming introduced by
The parameter $UserId is not named in camelCase.

This check marks parameter names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
79
		$appName,
80
		IRequest $request,
81
		$UserId,
82
		IL10N $l10n,
83
		ConfigService $configService,
84
		CirclesService $circlesService,
85
		FederatedLinkService $federatedLinkService,
86
		FederatedLinkCreationService $federatedLinkCreationService,
87
		MiscService $miscService
88
	) {
89
		parent::__construct($appName, $request);
90
91
		$this->l10n = $l10n;
92
		$this->configService = $configService;
93
		$this->circlesService = $circlesService;
94
		$this->federatedLinkService = $federatedLinkService;
95
		$this->federatedLinkCreationService = $federatedLinkCreationService;
96
		$this->miscService = $miscService;
97
	}
98
99
100
	/**
101
	 * link()
102
	 *
103
	 * Called from the UI to create a initiate the process of linking 2 [remote] circles.
104
	 * $remote format: <circle_name>@<remote_host>
105
	 *
106
	 * @NoAdminRequired
107
	 * @NoSubAdminRequired
108
	 *
109
	 * @param string $uniqueId
110
	 * @param string $remote
111
	 *
112
	 * @return DataResponse
113
	 */
114
	public function createLink($uniqueId, $remote) {
115
		try {
116
			$link = $this->federatedLinkCreationService->createLinkWithRemoteCircle($uniqueId, $remote);
117
			$links = $this->circlesService->detailsCircle($uniqueId)
118
										  ->getLinks();
119
120
			return $this->miscService->success(
121
				['circle_id' => $uniqueId, 'remote' => $remote, 'link' => $link, 'links' => $links]
122
			);
123
		} catch (\Exception $e) {
124
			return $this->miscService->fail(
125
				['circle_id' => $uniqueId, 'remote' => $remote, 'error' => $e->getMessage()]
126
			);
127
		}
128
	}
129
130
131
	/**
132
	 * updateLinkStatus();
133
	 *
134
	 * Modify a link status. Used to confirm/dismiss a request or putting down a link.
135
	 * The function will modify local status and broadcast the status to the remote.
136
	 *
137
	 * Note: should be moved to a LinkController
138
	 *
139
	 * @NoAdminRequired
140
	 * @NoSubAdminRequired
141
	 *
142
	 * @param string $linkId
143
	 * @param int $status
144
	 *
145
	 * @return DataResponse
146
	 * @throws FederatedCircleNotAllowedException
147
	 */
148
	public function updateLinkStatus($linkId, $status) {
149
150
		if (!$this->configService->isFederatedCirclesAllowed()) {
151
			throw new FederatedCircleNotAllowedException(
152
				$this->l10n->t("Federated circles are not allowed on this Nextcloud")
153
			);
154
		}
155
156
		try {
157
			$links = $this->federatedLinkService->linkStatus($linkId, (int)$status);
158
159
			return $this->miscService->success(['link_id' => $linkId, 'links' => $links]);
160
		} catch (\Exception $e) {
161
			return $this->miscService->fail(['link_id' => $linkId, 'error' => $e->getMessage()]);
162
		}
163
	}
164
165
166
}
167
168