Passed
Push — master ( 1979c6...dfbac0 )
by Christoph
14:02 queued 12s
created

DeleteCalendar::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 4
dl 0
loc 11
rs 10
1
<?php
2
3
declare(strict_types=1);
4
/**
5
 *
6
 * @copyright Copyright (c) 2021, Mattia Narducci ([email protected])
7
 *
8
 * @license GNU AGPL version 3 or any later version
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License as
12
 * published by the Free Software Foundation, either version 3 of the
13
 * License, or (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU Affero General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public License
21
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
22
 *
23
 */
24
25
namespace OCA\DAV\Command;
26
27
use OCA\DAV\CalDAV\BirthdayService;
28
use OCA\DAV\CalDAV\CalDavBackend;
29
use OCA\DAV\CalDAV\Calendar;
30
use OCP\IConfig;
31
use OCP\IL10N;
32
use OCP\IUserManager;
33
use Symfony\Component\Console\Command\Command;
34
use Symfony\Component\Console\Input\InputArgument;
35
use Symfony\Component\Console\Input\InputInterface;
36
use Symfony\Component\Console\Input\InputOption;
37
use Symfony\Component\Console\Output\OutputInterface;
38
39
class DeleteCalendar extends Command {
40
	/** @var CalDavBackend */
41
	private $calDav;
42
43
	/** @var IConfig */
44
	private $config;
45
46
	/** @var IL10N */
47
	private $l10n;
48
49
	/** @var IUserManager */
50
	private $userManager;
51
52
	/**
53
	 * @param CalDavBackend $calDav
54
	 * @param IConfig $config
55
	 * @param IL10N $l10n
56
	 * @param IUserManager $userManager
57
	 */
58
	public function __construct(
59
		CalDavBackend $calDav,
60
		IConfig $config,
61
		IL10N $l10n,
62
		IUserManager $userManager
63
	) {
64
		parent::__construct();
65
		$this->calDav = $calDav;
66
		$this->config = $config;
67
		$this->l10n = $l10n;
68
		$this->userManager = $userManager;
69
	}
70
71
	protected function configure(): void {
72
		$this
73
			->setName('dav:delete-calendar')
74
			->setDescription('Delete a dav calendar')
75
			->addArgument('uid',
76
				InputArgument::REQUIRED,
77
				'User who owns the calendar')
78
			->addArgument('name',
79
				InputArgument::OPTIONAL,
80
				'Name of the calendar to delete')
81
			->addOption('birthday',
82
				null,
83
				InputOption::VALUE_NONE,
84
				'Delete the birthday calendar')
85
			->addOption('force',
86
				'f',
87
				InputOption::VALUE_NONE,
88
				'Force delete skipping trashbin');
89
	}
90
91
	protected function execute(
92
		InputInterface $input,
93
		OutputInterface $output
94
	): int {
95
		/** @var string $user **/
96
		$user = $input->getArgument('uid');
97
		if (!$this->userManager->userExists($user)) {
98
			throw new \InvalidArgumentException(
99
				'User <' . $user . '> is unknown.');
100
		}
101
102
		$birthday = $input->getOption('birthday');
103
		if ($birthday !== false) {
104
			$name = BirthdayService::BIRTHDAY_CALENDAR_URI;
105
		} else {
106
			/** @var string $name **/
107
			$name = $input->getArgument('name');
108
			if (!$name) {
109
				throw new \InvalidArgumentException(
110
					'Please specify a calendar name or --birthday');
111
			}
112
		}
113
114
		$calendarInfo = $this->calDav->getCalendarByUri(
115
			'principals/users/' . $user,
116
			$name);
117
		if ($calendarInfo === null) {
118
			throw new \InvalidArgumentException(
119
				'User <' . $user . '> has no calendar named <' . $name . '>. You can run occ dav:list-calendars to list calendars URIs for this user.');
120
		}
121
122
		$calendar = new Calendar(
123
			$this->calDav,
124
			$calendarInfo,
125
			$this->l10n,
126
			$this->config);
127
128
		$force = $input->getOption('force');
129
		if ($force) {
130
			$calendar->disableTrashbin();
131
		}
132
133
		$calendar->delete();
134
135
		return 0;
136
	}
137
}
138