Completed
Pull Request — master (#32303)
by Victor
10:59
created

convertToOcmFileNotification()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 4
nop 4
dl 0
loc 28
rs 9.472
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
			'reshare' => FileNotification::NOTIFICATION_TYPE_REQUEST_RESHARE
61
		];
62
		$notification->setNotificationType($map[$action]);
63
		$notification->setProviderId($remoteId);
64
		$notification->addNotificationData('sharedSecret', $token);
65
66
		if ($action === 'permissions') {
67
			$ocmPermissions = $this->toOcmPermissions($data['permissions']);
68
			$notification->addNotificationData('permission', $ocmPermissions);
69
		}
70
71
		if ($action === 'reshare') {
72
			$ocmPermissions = $this->toOcmPermissions($data['permissions']);
73
			$notification->addNotificationData('permission', $ocmPermissions);
74
			$notification->addNotificationData('senderId', $data['senderId']);
75
			$notification->addNotificationData('shareWith', $data['shareWith']);
76
		}
77
78
		return $notification;
79
	}
80
81
	/**
82
	 * Maps numeric permissions to an array of string permissions
83
	 *
84
	 * @param int $permissions
85
	 * @return array
86
	 */
87
	protected function toOcmPermissions($permissions) {
88
		$permissions = (int) $permissions;
89
		$ocmPermissions = [];
90
		if ($permissions & Constants::PERMISSION_READ) {
91
			$ocmPermissions[] = 'read';
92
		}
93
		if (($permissions & Constants::PERMISSION_CREATE)
94
			|| ($permissions & Constants::PERMISSION_UPDATE)
95
		) {
96
			$ocmPermissions[] = 'write';
97
		}
98
		if ($permissions & Constants::PERMISSION_SHARE) {
99
			$ocmPermissions[] = 'share';
100
		}
101
		return $ocmPermissions;
102
	}
103
}
104