Completed
Pull Request — master (#544)
by Maxence
02:26
created

CirclesRemoteInit::init()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 78

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 78
rs 7.8577
c 0
b 0
f 0
cc 6
nc 8
nop 2

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php declare(strict_types=1);
2
3
4
/**
5
 * Circles - Bring cloud-users closer together.
6
 *
7
 * This file is licensed under the Affero General Public License version 3 or
8
 * later. See the COPYING file.
9
 *
10
 * @author Maxence Lange <[email protected]>
11
 * @copyright 2017
12
 * @license GNU AGPL version 3 or any later version
13
 *
14
 * This program is free software: you can redistribute it and/or modify
15
 * it under the terms of the GNU Affero General Public License as
16
 * published by the Free Software Foundation, either version 3 of the
17
 * License, or (at your option) any later version.
18
 *
19
 * This program is distributed in the hope that it will be useful,
20
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 * GNU Affero General Public License for more details.
23
 *
24
 * You should have received a copy of the GNU Affero General Public License
25
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
26
 *
27
 */
28
29
30
namespace OCA\Circles\Command;
31
32
use daita\MySmallPhpTools\Exceptions\SignatoryException;
33
use daita\MySmallPhpTools\Traits\Nextcloud\nc21\TNC21WellKnown;
34
use daita\MySmallPhpTools\Traits\TStringTools;
35
use OC\Core\Command\Base;
36
use OCA\Circles\Service\ConfigService;
37
use OCA\Circles\Service\RemoteService;
38
use OCP\IL10N;
39
use Symfony\Component\Console\Input\InputInterface;
40
use Symfony\Component\Console\Input\InputOption;
41
use Symfony\Component\Console\Output\OutputInterface;
42
use Symfony\Component\Console\Question\ConfirmationQuestion;
43
use Symfony\Component\Console\Question\Question;
44
45
46
/**
47
 * Class CirclesRemote
48
 *
49
 * @package OCA\Circles\Command
50
 */
51
class CirclesRemoteInit extends Base {
52
53
54
	use TNC21WellKnown;
55
	use TStringTools;
56
57
58
	/** @var IL10N */
59
	private $l10n;
60
61
	/** @var RemoteService */
62
	private $remoteService;
63
64
	/** @var ConfigService */
65
	private $configService;
66
67
68
	/**
69
	 * CirclesList constructor.
70
	 *
71
	 * @param RemoteService $remoteService
72
	 * @param ConfigService $configService
73
	 */
74
	public function __construct(RemoteService $remoteService, ConfigService $configService) {
75
		parent::__construct();
76
77
		$this->remoteService = $remoteService;
78
		$this->configService = $configService;
79
	}
80
81
82
	protected function configure() {
83
		parent::configure();
84
		$this->setName('circles:remote:init')
85
			 ->setDescription('init remote features')
86
			 ->addOption('reset', '', InputOption::VALUE_NONE, 'stop Federated Circles');
87
	}
88
89
90
	/**
91
	 * @param InputInterface $input
92
	 * @param OutputInterface $output
93
	 *
94
	 * @return int
95
	 * @throws SignatoryException
96
	 */
97
	protected function execute(InputInterface $input, OutputInterface $output): int {
98
		if ($input->getOption('reset')) {
99
			$this->reset($input, $output);
100
101
			return 0;
102
		}
103
104
		try {
105
			$signatory = $this->remoteService->getAppSignatory();
106
			$output->writeln('Looks like Federated Circles is already enabled:');
107
			$output->writeln('');
108
			$output->writeln(
109
				'<info>' . json_encode($signatory, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)
110
				. '</info>'
111
			);
112
113
			$output->writeln('');
114
			$output->writeln(
115
				'Run <comment>./occ circles:remote:init --reset</comment> to disable the feature'
116
			);
117
118
			return 0;
119
		} catch (SignatoryException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
120
		}
121
122
		$this->init($input, $output);
123
124
		return 0;
125
	}
126
127
128
	private function reset(InputInterface $input, OutputInterface $output): void {
129
		try {
130
			$this->remoteService->getAppSignatory();
131
		} catch (SignatoryException $e) {
132
			$output->writeln('<error>Federated Circles not enabled</error>');
133
134
			return;
135
		}
136
137
		$output->writeln(
138
			'<comment>Warning</comment>: Reset will disable Federated Circles but also reset your current identity'
139
		);
140
141
		$output->writeln('');
142
		$helper = $this->getHelper('question');
143
		$question = new ConfirmationQuestion(
144
			'<info>Do you want to reset/disable Federated Circles ?</info> (y/N) ', false, '/^(y|Y)/i'
145
		);
146
147
		if ($helper->ask($input, $output, $question)) {
148
			$this->remoteService->resetAppSignatory();
149
			$output->writeln('Federated Circles have been reset');
150
		}
151
	}
152
153
154
	/**
155
	 * @param InputInterface $input
156
	 * @param OutputInterface $output
157
	 *
158
	 * @throws SignatoryException
159
	 */
160
	private function init(InputInterface $input, OutputInterface $output): void {
161
		$output->writeln(
162
			'Federated Circles is a new feature that allows you to open your circles to other instances of Nextcloud'
163
		);
164
		$output->writeln('This feature is a Work in Progress and still in development state.');
165
		$output->writeln('');
166
167
		$helper = $this->getHelper('question');
168
		$question = new ConfirmationQuestion(
169
			'<info>Do you want to enable this feature ?</info> (y/N) ', false, '/^(y|Y)/i'
170
		);
171
172
		if (!$helper->ask($input, $output, $question)) {
173
			$output->writeln('Federated Circles NOT enabled');
174
175
			return;
176
		}
177
178
179
		$output->writeln('');
180
		$output->writeln(
181
			'Please understand that the feature is <comment>in ALPHA version</comment>, is <comment>not enabled by default</comment>, and <comment>should not be enable</comment> unless you really need it'
182
		);
183
		$helper = $this->getHelper('question');
184
		$question = new ConfirmationQuestion(
185
			'<info>I understand this but I really want this feature!</info> (y/N) ', false, '/^(y|Y)/i'
186
		);
187
188
		if (!$helper->ask($input, $output, $question)) {
189
			$output->writeln('Federated Circles NOT enabled');
190
191
			return;
192
		}
193
194
195
		$currInstance = $this->configService->getLocalInstance();
196
		$output->writeln('');
197
		$output->writeln('The domain name of your instance is: <info>' . $currInstance . '</info>');
198
		$helper = $this->getHelper('question');
199
		$question =
200
			new Question('<info>Change your domain name:</info> (' . $currInstance . ') ', $currInstance);
201
202
		$newInstance = $helper->ask($input, $output, $question);
203
204
		if ($newInstance !== $currInstance) {
205
			$this->configService->setAppValue(ConfigService::LOCAL_CLOUD_ID, $newInstance);
206
		}
207
208
209
		$output->writeln('');
210
		$currScheme = $this->configService->getAppValue(ConfigService::LOCAL_CLOUD_SCHEME);
211
		$output->writeln('Current protocol is <info>' . strtoupper($currScheme) . '</info>');
212
		$question =
213
			new Question('<info>Change the used protocol:</info> (' . $currScheme . ') ', $currScheme);
214
		$newScheme = strtolower($helper->ask($input, $output, $question));
215
216
		if ($newScheme !== $currScheme) {
217
			if (!in_array($newScheme, ['http', 'https'])) {
218
				$output->writeln('<error>protocol can only be \'https\' or \'http\'</error>');
219
220
				return;
221
			}
222
223
			$this->configService->setAppValue(ConfigService::LOCAL_CLOUD_SCHEME, $newScheme);
224
		}
225
226
		$output->writeln('');
227
		$output->writeln('');
228
		$output->writeln('Federated Circles is now <info>enable</info>.');
229
		$output->writeln('Your identity: ');
230
231
		$app = $this->remoteService->getAppSignatory(true);
232
233
		$output->writeln(
234
			'<info>' . json_encode($app, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . '</info>'
235
		);
236
		$output->writeln('');
237
	}
238
239
}
240
241