Completed
Pull Request — master (#32303)
by Victor
09:30
created

NotificationManager::toOcmPermissions()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 8
nop 1
dl 0
loc 16
rs 9.4222
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author Viktar Dubiniuk <[email protected]>
4
 *
5
 * @copyright Copyright (c) 2018, ownCloud GmbH
6
 * @license AGPL-3.0
7
 *
8
 * This code is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License, version 3,
10
 * as published by the Free Software Foundation.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License, version 3,
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
19
 *
20
 */
21
22
namespace OCA\FederatedFileSharing\Ocm;
23
24
use OCA\FederatedFileSharing\Ocm\Notification\FileNotification;
25
use OCP\Constants;
26
27
/**
28
 * Class NotificationManager
29
 *
30
 * @package OCA\FederatedFileSharing\Ocm
31
 */
32
class NotificationManager {
33
	/**
34
	 * @param string $type
35
	 *
36
	 * @return FileNotification
37
	 */
38
	public function getFileNotification($type) {
39
		$notification = new FileNotification();
40
		$notification->setNotificationType($type);
41
		return $notification;
42
	}
43
44
	/**
45
	 * @param string $remoteId
46
	 * @param string $token
47
	 * @param string $action
48
	 * @param array $data
49
	 *
50
	 * @return FileNotification
51
	 */
52
	public function convertToOcmFileNotification($remoteId, $token, $action, $data = []) {
53
		$notification = new FileNotification();
54
		$map = [
55
			'accept' => FileNotification::NOTIFICATION_TYPE_SHARE_ACCEPTED,
56
			'decline' => FileNotification::NOTIFICATION_TYPE_SHARE_DECLINED,
57
			'unshare' => FileNotification::NOTIFICATION_TYPE_SHARE_UNSHARED,
58
			'revoke' => FileNotification::NOTIFICATION_TYPE_RESHARE_UNDO,
59
			'permissions' => FileNotification::NOTIFICATION_TYPE_RESHARE_CHANGE_PERMISSION
60
		];
61
		$notification->setNotificationType($map[$action]);
62
		$notification->setProviderId($remoteId);
63
		$notification->addNotificationData('sharedSecret', $token);
64
		if ($action === 'permissions') {
65
			$ocmPermissions = $this->toOcmPermissions($data['permissions']);
66
			$notification->addNotificationData('permission', $ocmPermissions);
67
		}
68
69
		return $notification;
70
	}
71
72
	/**
73
	 * Maps numeric permissions to an array of string permissions
74
	 *
75
	 * @param int $permissions
76
	 * @return array
77
	 */
78
	protected function toOcmPermissions($permissions) {
79
		$permissions = (int) $permissions;
80
		$ocmPermissions = [];
81
		if ($permissions & Constants::PERMISSION_READ) {
82
			$ocmPermissions[] = 'read';
83
		}
84
		if (($permissions & Constants::PERMISSION_CREATE)
85
			|| ($permissions & Constants::PERMISSION_UPDATE)
86
		) {
87
			$ocmPermissions[] = 'write';
88
		}
89
		if ($permissions & Constants::PERMISSION_SHARE) {
90
			$ocmPermissions[] = 'share';
91
		}
92
		return $ocmPermissions;
93
	}
94
}
95