Passed
Push — master ( 9beda0...9f7fca )
by Georg
13:29 queued 11s
created

RemoteWipeActivityListener::publishActivity()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 14
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 16
rs 9.7998
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @copyright 2019 Christoph Wurst <[email protected]>
7
 *
8
 * @author 2019 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 OC\Authentication\Listeners;
27
28
use BadMethodCallException;
29
use OC\Authentication\Events\ARemoteWipeEvent;
30
use OC\Authentication\Events\RemoteWipeFinished;
31
use OC\Authentication\Events\RemoteWipeStarted;
32
use OC\Authentication\Token\IToken;
33
use OCP\Activity\IManager as IActvityManager;
34
use OCP\EventDispatcher\Event;
35
use OCP\EventDispatcher\IEventListener;
36
use OCP\ILogger;
37
38
class RemoteWipeActivityListener implements IEventListener {
39
40
	/** @var IActvityManager */
41
	private $activityManager;
42
43
	/** @var ILogger */
44
	private $logger;
45
46
	public function __construct(IActvityManager $activityManager,
47
								ILogger $logger) {
48
		$this->activityManager = $activityManager;
49
		$this->logger = $logger;
50
	}
51
52
	public function handle(Event $event): void {
53
		if ($event instanceof RemoteWipeStarted) {
54
			$this->publishActivity('remote_wipe_start', $event->getToken());
55
		} else if ($event instanceof RemoteWipeFinished) {
56
			$this->publishActivity('remote_wipe_finish', $event->getToken());
57
		}
58
	}
59
60
	private function publishActivity(string $event, IToken $token): void {
61
		$activity = $this->activityManager->generateEvent();
62
		$activity->setApp('core')
63
			->setType('security')
64
			->setAuthor($token->getUID())
65
			->setAffectedUser($token->getUID())
66
			->setSubject($event, [
67
				'name' => $token->getName(),
68
			]);
69
		try {
70
			$this->activityManager->publish($activity);
71
		} catch (BadMethodCallException $e) {
72
			$this->logger->logException($e, [
73
				'app' => 'core',
74
				'level' => ILogger::WARN,
75
				'message' => 'could not publish activity',
76
			]);
77
		}
78
	}
79
80
}
81