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

CalendarDeletionActivityUpdaterListener::handle()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 11
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 19
rs 9.9
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\Activity\Backend as ActivityBackend;
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 CalendarDeletionActivityUpdaterListener implements IEventListener {
40
41
	/** @var ActivityBackend */
42
	private $activityBackend;
43
44
	/** @var LoggerInterface */
45
	private $logger;
46
47
	public function __construct(ActivityBackend $activityBackend,
48
								LoggerInterface $logger) {
49
		$this->activityBackend = $activityBackend;
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->activityBackend->onCalendarDelete(
61
				$event->getCalendarData(),
62
				$event->getShares()
63
			);
64
65
			$this->logger->debug(
66
				sprintf('Activity generated for deleted calendar %d', $event->getCalendarId())
67
			);
68
		} catch (Throwable $e) {
69
			// Any error with activities shouldn't abort the calendar deletion, so we just log it
70
			$this->logger->error('Error generating activities for a deleted calendar: ' . $e->getMessage(), [
71
				'exception' => $e,
72
			]);
73
		}
74
	}
75
}
76