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

CalendarDeletionReminderUpdaterListener   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 32
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 18 3
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\CalDAV\Reminder\Backend as ReminderBackend;
29
use OCA\DAV\Events\CalendarDeletedEvent;
30
use OCP\EventDispatcher\Event;
31
use OCP\EventDispatcher\IEventListener;
32
use Psr\Log\LoggerInterface;
33
use Throwable;
34
use function sprintf;
35
36
/**
37
 * @template-implements IEventListener<\OCA\DAV\Events\CalendarDeletedEvent>
38
 */
39
class CalendarDeletionReminderUpdaterListener implements IEventListener {
40
41
	/** @var ReminderBackend */
42
	private $reminderBackend;
43
44
	/** @var LoggerInterface */
45
	private $logger;
46
47
	public function __construct(ReminderBackend $reminderBackend,
48
								LoggerInterface $logger) {
49
		$this->reminderBackend = $reminderBackend;
50
		$this->logger = $logger;
51
	}
52
53
	public function handle(Event $event): void {
54
		if (!($event instanceof CalendarDeletedEvent)) {
55
			// Not what we subscribed to
56
			return;
57
		}
58
59
		try {
60
			$this->reminderBackend->cleanRemindersForCalendar(
61
				$event->getCalendarId()
62
			);
63
64
			$this->logger->debug(
65
				sprintf('Reminders of calendar %d cleaned up', $event->getCalendarId())
66
			);
67
		} catch (Throwable $e) {
68
			// Any error with activities shouldn't abort the calendar deletion, so we just log it
69
			$this->logger->error('Error cleaning up reminders of a deleted calendar: ' . $e->getMessage(), [
70
				'exception' => $e,
71
			]);
72
		}
73
	}
74
}
75