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

sendNotification()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 8
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 10
rs 10
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 OC\Authentication\Events\ARemoteWipeEvent;
29
use OC\Authentication\Events\RemoteWipeFinished;
30
use OC\Authentication\Events\RemoteWipeStarted;
31
use OC\Authentication\Token\IToken;
32
use OCP\AppFramework\Utility\ITimeFactory;
33
use OCP\EventDispatcher\Event;
34
use OCP\EventDispatcher\IEventListener;
35
use OCP\Notification\IManager as INotificationManager;
36
37
class RemoteWipeNotificationsListener implements IEventListener {
38
39
	/** @var INotificationManager */
40
	private $notificationManager;
41
42
	/** @var ITimeFactory */
43
	private $timeFactory;
44
45
	public function __construct(INotificationManager $notificationManager,
46
								ITimeFactory $timeFactory) {
47
		$this->notificationManager = $notificationManager;
48
		$this->timeFactory = $timeFactory;
49
	}
50
51
	public function handle(Event $event): void {
52
		if ($event instanceof RemoteWipeStarted) {
53
			$this->sendNotification('remote_wipe_start', $event->getToken());
54
		} else if ($event instanceof RemoteWipeFinished) {
55
			$this->sendNotification('remote_wipe_finish', $event->getToken());
56
		}
57
	}
58
59
	private function sendNotification(string $event, IToken $token): void {
60
		$notification = $this->notificationManager->createNotification();
61
		$notification->setApp('auth')
62
			->setUser($token->getUID())
63
			->setDateTime($this->timeFactory->getDateTime())
64
			->setObject('token', $token->getId())
65
			->setSubject($event, [
66
				'name' => $token->getName(),
67
			]);
68
		$this->notificationManager->notify($notification);
69
	}
70
71
}
72