Passed
Push — master ( bce941...17d0da )
by John
67:35 queued 55:34
created

AddressbookListener   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 46
c 1
b 0
f 0
dl 0
loc 78
rs 10
wmc 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B handle() 0 65 9
A __construct() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
/**
5
 * @copyright Copyright (c) 2021 Joas Schilling <[email protected]>
6
 *
7
 * @author Joas Schilling <[email protected]>
8
 *
9
 * @license GNU AGPL version 3 or any later version
10
 *
11
 * This program is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU Affero General Public License as
13
 * published by the Free Software Foundation, either version 3 of the
14
 * License, or (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU Affero General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU Affero General Public License
22
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23
 */
24
25
namespace OCA\DAV\Listener;
26
27
use OCA\DAV\CardDAV\Activity\Backend as ActivityBackend;
28
use OCA\DAV\Events\AddressBookCreatedEvent;
29
use OCA\DAV\Events\AddressBookDeletedEvent;
30
use OCA\DAV\Events\AddressBookShareUpdatedEvent;
31
use OCA\DAV\Events\AddressBookUpdatedEvent;
32
use OCP\EventDispatcher\Event;
33
use OCP\EventDispatcher\IEventListener;
34
use Psr\Log\LoggerInterface;
35
use Throwable;
36
use function sprintf;
37
38
class AddressbookListener implements IEventListener {
39
	/** @var ActivityBackend */
40
	private $activityBackend;
41
42
	/** @var LoggerInterface */
43
	private $logger;
44
45
	public function __construct(ActivityBackend $activityBackend,
46
								LoggerInterface $logger) {
47
		$this->activityBackend = $activityBackend;
48
		$this->logger = $logger;
49
	}
50
51
	public function handle(Event $event): void {
52
		if ($event instanceof AddressBookCreatedEvent) {
53
			try {
54
				$this->activityBackend->onAddressbookCreate(
55
					$event->getAddressBookData()
56
				);
57
58
				$this->logger->debug(
59
					sprintf('Activity generated for new addressbook %d', $event->getAddressBookId())
60
				);
61
			} catch (Throwable $e) {
62
				// Any error with activities shouldn't abort the addressbook creation, so we just log it
63
				$this->logger->error('Error generating activities for a new addressbook: ' . $e->getMessage(), [
64
					'exception' => $e,
65
				]);
66
			}
67
		} elseif ($event instanceof AddressBookUpdatedEvent) {
68
			try {
69
				$this->activityBackend->onAddressbookUpdate(
70
					$event->getAddressBookData(),
71
					$event->getShares(),
72
					$event->getMutations()
73
				);
74
75
				$this->logger->debug(
76
					sprintf('Activity generated for changed addressbook %d', $event->getAddressBookId())
77
				);
78
			} catch (Throwable $e) {
79
				// Any error with activities shouldn't abort the addressbook update, so we just log it
80
				$this->logger->error('Error generating activities for a changed addressbook: ' . $e->getMessage(), [
81
					'exception' => $e,
82
				]);
83
			}
84
		} elseif ($event instanceof AddressBookDeletedEvent) {
85
			try {
86
				$this->activityBackend->onAddressbookDelete(
87
					$event->getAddressBookData(),
88
					$event->getShares()
89
				);
90
91
				$this->logger->debug(
92
					sprintf('Activity generated for deleted addressbook %d', $event->getAddressBookId())
93
				);
94
			} catch (Throwable $e) {
95
				// Any error with activities shouldn't abort the addressbook deletion, so we just log it
96
				$this->logger->error('Error generating activities for a deleted addressbook: ' . $e->getMessage(), [
97
					'exception' => $e,
98
				]);
99
			}
100
		} elseif ($event instanceof AddressBookShareUpdatedEvent) {
101
			try {
102
				$this->activityBackend->onAddressbookUpdateShares(
103
					$event->getAddressBookData(),
104
					$event->getOldShares(),
105
					$event->getAdded(),
106
					$event->getRemoved()
107
				);
108
109
				$this->logger->debug(
110
					sprintf('Activity generated for (un)sharing addressbook %d', $event->getAddressBookId())
111
				);
112
			} catch (Throwable $e) {
113
				// Any error with activities shouldn't abort the addressbook creation, so we just log it
114
				$this->logger->error('Error generating activities for (un)sharing addressbook: ' . $e->getMessage(), [
115
					'exception' => $e,
116
				]);
117
			}
118
		}
119
	}
120
}
121