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

CardListener   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 40
c 2
b 0
f 0
dl 0
loc 66
rs 10
wmc 8

2 Methods

Rating   Name   Duplication   Size   Complexity  
B handle() 0 53 7
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\Provider\Card;
28
use OCA\DAV\CardDAV\Activity\Backend as ActivityBackend;
29
use OCA\DAV\Events\CardCreatedEvent;
30
use OCA\DAV\Events\CardDeletedEvent;
31
use OCA\DAV\Events\CardUpdatedEvent;
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 CardListener 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 CardCreatedEvent) {
53
			try {
54
				$this->activityBackend->triggerCardActivity(
55
					Card::SUBJECT_ADD,
56
					$event->getAddressBookData(),
57
					$event->getShares(),
58
					$event->getCardData()
59
				);
60
61
				$this->logger->debug(
62
					sprintf('Activity generated for a new card in addressbook %d', $event->getAddressBookId())
63
				);
64
			} catch (Throwable $e) {
65
				// Any error with activities shouldn't abort the addressbook creation, so we just log it
66
				$this->logger->error('Error generating activities for a new card in addressbook: ' . $e->getMessage(), [
67
					'exception' => $e,
68
				]);
69
			}
70
		} elseif ($event instanceof CardUpdatedEvent) {
71
			try {
72
				$this->activityBackend->triggerCardActivity(
73
					Card::SUBJECT_UPDATE,
74
					$event->getAddressBookData(),
75
					$event->getShares(),
76
					$event->getCardData()
77
				);
78
79
				$this->logger->debug(
80
					sprintf('Activity generated for a changed card in addressbook %d', $event->getAddressBookId())
81
				);
82
			} catch (Throwable $e) {
83
				// Any error with activities shouldn't abort the addressbook update, so we just log it
84
				$this->logger->error('Error generating activities for a changed card in addressbook: ' . $e->getMessage(), [
85
					'exception' => $e,
86
				]);
87
			}
88
		} elseif ($event instanceof CardDeletedEvent) {
89
			try {
90
				$this->activityBackend->triggerCardActivity(
91
					Card::SUBJECT_DELETE,
92
					$event->getAddressBookData(),
93
					$event->getShares(),
94
					$event->getCardData()
95
				);
96
97
				$this->logger->debug(
98
					sprintf('Activity generated for a deleted card in addressbook %d', $event->getAddressBookId())
99
				);
100
			} catch (Throwable $e) {
101
				// Any error with activities shouldn't abort the addressbook deletion, so we just log it
102
				$this->logger->error('Error generating activities for a deleted card in addressbook: ' . $e->getMessage(), [
103
					'exception' => $e,
104
				]);
105
			}
106
		}
107
	}
108
}
109