Passed
Push — master ( fdf54e...22ba8f )
by Morris
25:54 queued 12s
created

CalendarDeletionDefaultUpdaterListener   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
c 1
b 0
f 0
dl 0
loc 44
rs 10
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 27 5
A __construct() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * @copyright 2021 Christoph Wurst <[email protected]>
7
 *
8
 * @author 2021 Christoph Wurst <[email protected]>
9
 *
10
 * @license GNU AGPL version 3 or any later version
11
 *
12
 * This program is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License as
14
 * published by the Free Software Foundation, either version 3 of the
15
 * License, or (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License
23
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24
 */
25
26
namespace OCA\DAV\Listener;
27
28
use OCA\DAV\Events\CalendarDeletedEvent;
29
use OCP\EventDispatcher\Event;
30
use OCP\EventDispatcher\IEventListener;
31
use OCP\IConfig;
32
use Psr\Log\LoggerInterface;
33
use Throwable;
34
use function strpos;
35
36
/**
37
 * @template-implements IEventListener<\OCA\DAV\Events\CalendarDeletedEvent>
38
 */
39
class CalendarDeletionDefaultUpdaterListener implements IEventListener {
40
41
	/** @var IConfig */
42
	private $config;
43
44
	/** @var LoggerInterface */
45
	private $logger;
46
47
	public function __construct(IConfig $config,
48
								LoggerInterface $logger) {
49
		$this->config = $config;
50
		$this->logger = $logger;
51
	}
52
53
	/**
54
	 * In case the user has set their default calendar to the deleted one
55
	 */
56
	public function handle(Event $event): void {
57
		if (!($event instanceof CalendarDeletedEvent)) {
58
			// Not what we subscribed to
59
			return;
60
		}
61
62
		try {
63
			$principalUri = $event->getCalendarData()['principaluri'];
64
			if (strpos($principalUri, 'principals/users') !== 0) {
65
				$this->logger->debug('Default calendar needs no update because the deleted calendar does not belong to a user principal');
66
				return;
67
			}
68
69
			[, $uid] = \Sabre\Uri\split($principalUri);
70
			$uri = $event->getCalendarData()['uri'];
71
			if ($this->config->getUserValue($uid, 'dav', 'defaultCalendar') !== $uri) {
72
				$this->logger->debug('Default calendar needs no update because the deleted calendar is no the user\'s default one');
73
				return;
74
			}
75
76
			$this->config->deleteUserValue($uid, 'dav', 'defaultCalendar');
77
78
			$this->logger->debug('Default user calendar reset');
79
		} catch (Throwable $e) {
80
			// Any error with activities shouldn't abort the calendar deletion, so we just log it
81
			$this->logger->error('Error generating activities for a deleted calendar: ' . $e->getMessage(), [
82
				'exception' => $e,
83
			]);
84
		}
85
	}
86
}
87