Completed
Push — master ( 1709bb...6a652c )
by Maxence
01:46
created

SyncContact::addLinkedGroups()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 9
Ratio 100 %

Importance

Changes 0
Metric Value
dl 9
loc 9
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
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 Exception;
33
use OC\Core\Command\Base;
34
use OCA\Circles\Service\DavService;
35
use OCA\DAV\CardDAV\CardDavBackend;
36
use OCP\IUserManager;
37
use Symfony\Component\Console\Input\InputInterface;
38
use Symfony\Component\Console\Input\InputOption;
39
use Symfony\Component\Console\Output\OutputInterface;
40
41
42
/**
43
 * Class SyncContact
44
 *
45
 * @package OCA\Circles\Command
46
 */
47
class SyncContact extends Base {
48
49
50
	/** @var IUserManager */
51
	private $userManager;
52
53
	/** @var CardDavBackend */
54
	private $cardDavBackend;
55
56
	/** @var DavService */
57
	private $davService;
58
59
60
	/**
61
	 * Groups constructor.
62
	 *
63
	 * @param IUserManager $userManager
64
	 * @param CardDavBackend $cardDavBackend
65
	 * @param DavService $davService
66
	 */
67
	public function __construct(
68
		IUserManager $userManager, CardDavBackend $cardDavBackend, DavService $davService
69
	) {
70
		parent::__construct();
71
72
		$this->userManager = $userManager;
73
		$this->cardDavBackend = $cardDavBackend;
74
		$this->davService = $davService;
75
	}
76
77
78
	protected function configure() {
79
		parent::configure();
80
		$this->setName('circles:contacts:sync')
81
			 ->addOption('info', '', InputOption::VALUE_NONE, 'get info about contacts')
82
			 ->addOption('status', '', InputOption::VALUE_NONE, 'get info about sync')
83
			 ->setDescription('sync contacts, when using the Circles app as a backend of the Contact app');
84
	}
85
86
87
	/**
88
	 * @param InputInterface $input
89
	 * @param OutputInterface $output
90
	 *
91
	 * @return int|void|null
92
	 * @throws Exception
93
	 */
94
	protected function execute(InputInterface $input, OutputInterface $output) {
95
		if ($input->getOption('info')) {
96
			$this->displayInfo($output);
97
98
			return;
99
		}
100
101
		if ($input->getOption('status')) {
102
			$this->displayStatus($output);
103
104
			return;
105
		}
106
107
		$this->davService->migration();
108
109
		$output->writeln('migration done');
110
	}
111
112
113
	/**
114
	 * @param OutputInterface $output
115
	 */
116
	private function displayInfo(OutputInterface $output) {
117
		$users = $this->userManager->search('');
118
119
		$tCards = $tBooks = 0;
120
		$knownBooks = [];
121
		foreach ($users as $user) {
122
			$books = $this->cardDavBackend->getAddressBooksForUser('principals/users/' . $user->getUID());
123
			$output->writeln(
124
				'- User <info>' . $user->getUID() . '</info> have ' . sizeof($books) . ' address books:'
125
			);
126
127
			$tBooks += sizeof($books);
128
			foreach ($books as $book) {
129
				$bookId = $book['id'];
130
				$owner = $this->davService->getOwnerFromAddressBook($bookId);
131
132
				$cards = $this->cardDavBackend->getCards($bookId);
133
134
				if (!in_array($bookId, $knownBooks)) {
135
					$tCards += sizeof($cards);
136
				}
137
138
				$shared = '';
139
				if ($owner !== $user->getUID()) {
140
					$shared = ' (shared by <info>' . $owner . '</info>)';
141
				}
142
143
				$output->writeln(
144
					'  <comment>*</comment> book #' . $bookId . $shared . ' contains '
145
					. sizeof($cards)
146
					. ' entries'
147
				);
148
149
				$knownBooks[] = $bookId;
150
			}
151
		}
152
153
		$output->writeln('');
154
		$output->writeln('with a total of ' . $tBooks . ' address books and ' . $tCards . ' contact entries');
155
	}
156
157
158
	/**
159
	 * @param OutputInterface $output
160
	 */
161
	private function displayStatus(OutputInterface $output) {
162
		$output->writeln('not yet available');
163
	}
164
165
}
166
167