Completed
Push — master ( 2493d7...51beeb )
by Maxence
03:12 queued 12s
created

CirclesSync::execute()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 9.536
c 0
b 0
f 0
cc 3
nc 3
nop 2
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\ContactAddressBookNotFoundException;
37
use OCA\Circles\Exceptions\ContactFormatException;
38
use OCA\Circles\Exceptions\ContactNotFoundException;
39
use OCA\Circles\Exceptions\FederatedUserException;
40
use OCA\Circles\Exceptions\InvalidIdException;
41
use OCA\Circles\Exceptions\MigrationException;
42
use OCA\Circles\Exceptions\RequestBuilderException;
43
use OCA\Circles\Exceptions\SingleCircleNotFoundException;
44
use OCA\Circles\Service\ConfigService;
45
use OCA\Circles\Service\MigrationService;
46
use OCA\Circles\Service\OutputService;
47
use OCA\Circles\Service\SyncService;
48
use Symfony\Component\Console\Input\InputInterface;
49
use Symfony\Component\Console\Input\InputOption;
50
use Symfony\Component\Console\Output\OutputInterface;
51
52
53
/**
54
 * Class CirclesSync
55
 *
56
 * @package OCA\Circles\Command
57
 */
58
class CirclesSync extends Base {
59
60
61
	/** @var SyncService */
62
	private $syncService;
63
64
	/** @var MigrationService */
65
	private $migrationService;
66
67
	/** @var OutputService */
68
	private $outputService;
69
70
	/** @var ConfigService */
71
	private $configService;
72
73
74
	/**
75
	 * CirclesSync constructor.
76
	 *
77
	 * @param SyncService $syncService
78
	 * @param OutputService $outputService
79
	 * @param ConfigService $configService
80
	 */
81
	public function __construct(
82
		SyncService $syncService,
83
		MigrationService $migrationService,
84
		OutputService $outputService,
85
		ConfigService $configService
86
	) {
87
		parent::__construct();
88
89
		$this->syncService = $syncService;
90
		$this->migrationService = $migrationService;
91
		$this->outputService = $outputService;
92
		$this->configService = $configService;
93
	}
94
95
96
	/**
97
	 *
98
	 */
99
	protected function configure() {
100
		parent::configure();
101
		$this->setName('circles:sync')
102
			 ->setDescription('Sync Circles and Members')
103
			 ->addOption('migration', '', InputOption::VALUE_NONE, 'Migrate from Circles 0.21.0')
104
			 ->addOption('force', '', InputOption::VALUE_NONE, 'Force migration')
105
			 ->addOption('force-run', '', InputOption::VALUE_NONE, 'Force migration run')
106
			 ->addOption('apps', '', InputOption::VALUE_NONE, 'Sync Apps')
107
			 ->addOption('users', '', InputOption::VALUE_NONE, 'Sync Nextcloud Users')
108
			 ->addOption('groups', '', InputOption::VALUE_NONE, 'Sync Nextcloud Groups')
109
			 ->addOption('contacts', '', InputOption::VALUE_NONE, 'Sync Contacts')
110
			 ->addOption('remotes', '', InputOption::VALUE_NONE, 'Sync Remotes')
111
			 ->addOption('global-scale', '', InputOption::VALUE_NONE, 'Sync GlobalScale');
112
	}
113
114
115
	/**
116
	 * @param InputInterface $input
117
	 * @param OutputInterface $output
118
	 *
119
	 * @return int
120
	 * @throws ContactAddressBookNotFoundException
121
	 * @throws ContactFormatException
122
	 * @throws ContactNotFoundException
123
	 * @throws FederatedUserException
124
	 * @throws InvalidIdException
125
	 * @throws MigrationException
126
	 * @throws RequestBuilderException
127
	 * @throws SingleCircleNotFoundException
128
	 */
129
	protected function execute(InputInterface $input, OutputInterface $output): int {
130
		$this->outputService->setOccOutput($output);
131
132
		if ($input->getOption('migration')) {
133
			if ($input->getOption('force-run')) {
134
				$this->configService->setAppValue(ConfigService::MIGRATION_RUN, '0');
135
			}
136
137
			$this->migrationService->migration($input->getOption('force'));
138
139
			$output->writeln('');
140
			$output->writeln('Migration done');
141
142
			return 0;
143
		}
144
145
		$sync = $this->filterSync($input);
146
		$this->syncService->sync($sync);
147
148
		$output->writeln('');
149
		$output->writeln('Sync done');
150
151
		return 0;
152
	}
153
154
155
	private function filterSync(InputInterface $input): int {
156
		$sync = 0;
157
		if ($input->getOption('apps')) {
158
			$sync += SyncService::SYNC_APPS;
159
		}
160
		if ($input->getOption('users')) {
161
			$sync += SyncService::SYNC_USERS;
162
		}
163
		if ($input->getOption('groups')) {
164
			$sync += SyncService::SYNC_GROUPS;
165
		}
166
		if ($input->getOption('global-scale')) {
167
			$sync += SyncService::SYNC_GLOBALSCALE;
168
		}
169
		if ($input->getOption('remotes')) {
170
			$sync += SyncService::SYNC_REMOTES;
171
		}
172
		if ($input->getOption('contacts')) {
173
			$sync += SyncService::SYNC_CONTACTS;
174
		}
175
		if ($sync === 0) {
176
			$sync = SyncService::SYNC_ALL;
177
		}
178
179
		return $sync;
180
	}
181
182
}
183
184