Passed
Push — master ( cfdece...554c78 )
by Morris
12:32
created

ListCalendars::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2018, Georg Ehrke
4
 *
5
 * @author Georg Ehrke <[email protected]>
6
 * @author Thomas Citharel <[email protected]>
7
 *
8
 * @license AGPL-3.0
9
 *
10
 * This code is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License, version 3,
12
 * as published by the Free Software Foundation.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License, version 3,
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
21
 *
22
 */
23
namespace OCA\DAV\Command;
24
25
use OCA\DAV\CalDAV\BirthdayService;
26
use OCA\DAV\CalDAV\CalDavBackend;
27
use OCA\DAV\Connector\Sabre\Principal;
28
use OCP\IConfig;
29
use OCP\IDBConnection;
30
use OCP\IGroupManager;
31
use OCP\IUserManager;
32
use OCP\IUserSession;
33
use OCP\Share\IManager;
34
use Symfony\Component\Console\Command\Command;
35
use Symfony\Component\Console\Helper\Table;
36
use Symfony\Component\Console\Input\InputArgument;
37
use Symfony\Component\Console\Input\InputInterface;
38
use Symfony\Component\Console\Output\OutputInterface;
39
40
class ListCalendars extends Command {
41
42
	/** @var IUserManager */
43
	protected $userManager;
44
45
	/** @var CalDavBackend */
46
	private $caldav;
47
48
	/**
49
	 * @param IUserManager $userManager
50
	 * @param CalDavBackend $caldav
51
	 */
52
	function __construct(IUserManager $userManager, CalDavBackend $caldav) {
53
		parent::__construct();
54
		$this->userManager = $userManager;
55
		$this->caldav = $caldav;
56
	}
57
58
	protected function configure() {
59
		$this
60
			->setName('dav:list-calendars')
61
			->setDescription('List all calendars of a user')
62
			->addArgument('uid',
63
				InputArgument::REQUIRED,
64
				'User for whom all calendars will be listed');
65
	}
66
67
	protected function execute(InputInterface $input, OutputInterface $output) {
68
		$user = $input->getArgument('uid');
69
		if (!$this->userManager->userExists($user)) {
0 ignored issues
show
Bug introduced by
It seems like $user can also be of type string[]; however, parameter $uid of OCP\IUserManager::userExists() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

69
		if (!$this->userManager->userExists(/** @scrutinizer ignore-type */ $user)) {
Loading history...
70
			throw new \InvalidArgumentException("User <$user> is unknown.");
71
		}
72
73
		$calendars = $this->caldav->getCalendarsForUser("principals/users/$user");
74
75
		$calendarTableData = [];
76
		foreach($calendars as $calendar) {
77
			// skip birthday calendar
78
			if ($calendar['uri'] === BirthdayService::BIRTHDAY_CALENDAR_URI) {
79
				continue;
80
			}
81
82
			$readOnly = false;
83
			$readOnlyIndex = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only';
84
			if (isset($calendar[$readOnlyIndex])) {
85
				$readOnly = $calendar[$readOnlyIndex];
86
			}
87
88
			$calendarTableData[] = [
89
				$calendar['uri'],
90
				$calendar['{DAV:}displayname'],
91
				$calendar['{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal'],
92
				$calendar['{' . \OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD . '}owner-displayname'],
93
				$readOnly ? ' x ' : ' ✓ ',
94
			];
95
		}
96
97
		if (count($calendarTableData) > 0) {
98
			$table = new Table($output);
99
			$table->setHeaders(['uri', 'displayname', 'owner\'s userid', 'owner\'s displayname', 'writable'])
100
				->setRows($calendarTableData);
101
102
			$table->render();
103
		} else {
104
			$output->writeln("<info>User <$user> has no calendars</info>");
105
		}
106
	}
107
108
}
109