Completed
Pull Request — master (#551)
by Maxence
02:40
created

CirclesSync   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 127
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 4
dl 0
loc 127
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A configure() 0 14 1
B execute() 0 78 8
1
<?php
2
3
declare(strict_types=1);
4
5
6
/**
7
 * Circles - Bring cloud-users closer together.
8
 *
9
 * This file is licensed under the Affero General Public License version 3 or
10
 * later. See the COPYING file.
11
 *
12
 * @author Maxence Lange <[email protected]>
13
 * @copyright 2017
14
 * @license GNU AGPL version 3 or any later version
15
 *
16
 * This program is free software: you can redistribute it and/or modify
17
 * it under the terms of the GNU Affero General Public License as
18
 * published by the Free Software Foundation, either version 3 of the
19
 * License, or (at your option) any later version.
20
 *
21
 * This program is distributed in the hope that it will be useful,
22
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24
 * GNU Affero General Public License for more details.
25
 *
26
 * You should have received a copy of the GNU Affero General Public License
27
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28
 *
29
 */
30
31
32
namespace OCA\Circles\Command;
33
34
35
use OC\Core\Command\Base;
36
use OCA\Circles\Exceptions\FederatedUserException;
37
use OCA\Circles\Exceptions\FederatedUserNotFoundException;
38
use OCA\Circles\Exceptions\InvalidIdException;
39
use OCA\Circles\Exceptions\MigrationTo22Exception;
40
use OCA\Circles\Exceptions\SingleCircleNotFoundException;
41
use OCA\Circles\Service\SyncService;
42
use Symfony\Component\Console\Input\InputInterface;
43
use Symfony\Component\Console\Input\InputOption;
44
use Symfony\Component\Console\Output\OutputInterface;
45
46
47
/**
48
 * Class CirclesSync
49
 *
50
 * @package OCA\Circles\Command
51
 */
52
class CirclesSync extends Base {
53
54
55
	/** @var SyncService */
56
	private $syncService;
57
58
	/**
59
	 * CirclesSync constructor.
60
	 *
61
	 * @param SyncService $syncService
62
	 */
63
	public function __construct(SyncService $syncService) {
64
		parent::__construct();
65
		$this->syncService = $syncService;
66
	}
67
68
69
	/**
70
	 *
71
	 */
72
	protected function configure() {
73
		parent::configure();
74
		$this->setName('circles:sync')
75
			 ->setDescription('Sync Circles and Members')
76
			 ->addOption('migration', '', InputOption::VALUE_NONE, 'Migrate from Circles 0.21.0')
77
			 ->addOption('users', '', InputOption::VALUE_NONE, 'Sync Nextcloud Users')
78
			 ->addOption('user', '', InputOption::VALUE_REQUIRED, 'Sync only a specific Nextcloud User', '')
79
			 ->addOption('groups', '', InputOption::VALUE_NONE, 'Sync Nextcloud Groups')
80
			 ->addOption('group', '', InputOption::VALUE_REQUIRED, 'Sync only a specific Nextcloud Group', '')
81
			 ->addOption('contacts', '', InputOption::VALUE_NONE, 'Sync Contacts')
82
			 ->addOption('remotes', '', InputOption::VALUE_NONE, 'Sync Remotes')
83
			 ->addOption('remote', '', InputOption::VALUE_NONE, 'Sync only a specific Remote')
84
			 ->addOption('global-scale', '', InputOption::VALUE_NONE, 'Sync GlobalScale');
85
	}
86
87
88
	/**
89
	 * @param InputInterface $input
90
	 * @param OutputInterface $output
91
	 *
92
	 * @return int
93
	 * @throws MigrationTo22Exception
94
	 * @throws FederatedUserException
95
	 * @throws FederatedUserNotFoundException
96
	 * @throws InvalidIdException
97
	 * @throws SingleCircleNotFoundException
98
	 */
99
	protected function execute(InputInterface $input, OutputInterface $output): int {
100
101
		$options = $input->getOptions();
102
		unset($options['output']);
103
		if (empty(array_filter($options))) {
104
			$this->syncService->syncAll();
105
			$output->writeln('- Sync done');
106
107
			return 0;
108
		}
109
110
		if ($input->getOption('migration')) {
111
			// TODO: lock using setAppValue() to avoid duplicate process
112
			if (!$this->syncService->migration()) {
113
				throw new MigrationTo22Exception('Migration already performed successfully');
114
			}
115
			$output->writeln('- Migration went smoothly, enjoy using Circles 22!');
116
		}
117
118
		if ($input->getOption('users')) {
119
			$this->syncService->syncNextcloudUsers();
120
			$output->writeln('- Nextcloud Users synced');
121
		}
122
123
		if (($userId = $input->getOption('user')) !== '') {
124
			$federatedUser = $this->syncService->syncNextcloudUser($userId);
125
			$output->writeln(
126
				'- Nextcloud User <info>' . $userId . '</info>/<info>' . $federatedUser->getSingleId()
127
				. '</info> synced'
128
			);
129
		}
130
131
		if ($input->getOption('groups')) {
132
			$this->syncService->syncNextcloudGroups();
133
			$output->writeln('- Nextcloud Groups synced');
134
		}
135
136
		if (($groupId = $input->getOption('group')) !== '') {
137
			$circle = $this->syncService->syncNextcloudGroup($groupId);
138
			$output->writeln(
139
				'- Nextcloud Group <info>' . $groupId . '</info>/<info>' . $circle->getSingleId()
140
				. '</info> synced'
141
			);
142
		}
143
144
145
//
146
147
148
//			echo json_encode(array_filter($options), JSON_PRETTY_PRINT) . "\n";
149
//			$output->writeln(json_encode($result), JSON_PRETTY_PRINT);
150
151
//		if ($input->getOption('broadcast')) {
152
//
153
//			return 0;
154
//		}
155
//
156
//		$circleId = (string)$input->getArgument('circle_id');
157
//		$instance = $input->getOption('instance');
158
//		if ($instance === '') {
159
//			try {
160
//				$circle = $this->circleService->getCircle($circleId);
161
//			} catch (CircleNotFoundException $e) {
162
//				throw new CircleNotFoundException(
163
//					'unknown circle, use --instance to retrieve the data from a remote instance'
164
//				);
165
//			}
166
//			$instance = $circle->getInstance();
167
//		}
168
//
169
//		if ($this->configService->isLocalInstance($instance)) {
170
//			throw new RemoteNotFoundException('Circle is local');
171
//		}
172
//
173
//		$this->remoteService->syncRemoteCircle($circleId, $instance);
174
175
		return 0;
176
	}
177
178
}
179
180