Completed
Pull Request — master (#32303)
by Victor
11:52
created

convertToOcmFileNotification()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 4
nop 4
dl 0
loc 38
rs 9.312
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
	 * @var Permissions
35
	 */
36
	protected $permissions;
37
38
	/**
39
	 * NotificationManager constructor.
40
	 *
41
	 * @param Permissions $permissions
42
	 */
43
	public function __construct(Permissions $permissions) {
44
		$this->permissions = $permissions;
45
	}
46
47
	/**
48
	 * @param string $type
49
	 *
50
	 * @return FileNotification
51
	 */
52
	public function getFileNotification($type) {
53
		$notification = new FileNotification();
54
		$notification->setNotificationType($type);
55
		return $notification;
56
	}
57
58
	/**
59
	 * @param string $remoteId
60
	 * @param string $token
61
	 * @param string $action
62
	 * @param array $data
63
	 *
64
	 * @return FileNotification
65
	 */
66
	public function convertToOcmFileNotification($remoteId, $token, $action, $data = []) {
67
		$notification = new FileNotification();
68
		$map = [
69
			'accept' => FileNotification::NOTIFICATION_TYPE_SHARE_ACCEPTED,
70
			'decline' => FileNotification::NOTIFICATION_TYPE_SHARE_DECLINED,
71
			'unshare' => FileNotification::NOTIFICATION_TYPE_SHARE_UNSHARED,
72
			'revoke' => FileNotification::NOTIFICATION_TYPE_RESHARE_UNDO,
73
			'permissions' => FileNotification::NOTIFICATION_TYPE_RESHARE_CHANGE_PERMISSION,
74
			'reshare' => FileNotification::NOTIFICATION_TYPE_REQUEST_RESHARE
75
		];
76
		$messages = [
77
			'accept' => "Recipient accepted the share",
78
			'decline' => "Recipient declined the share or unshared it from themself",
79
			'unshare' => "File was unshared",
80
			'revoke' => "Tell the owner (or the sender of a reshare) that the reshare was unshared",
81
			'permissions' => "Tell the owner (or the sender of the reshare) that the permissions changed",
82
			'reshare' => "Recipient of a share ask the owner to reshare the file with another user"
83
		];
84
85
		$notification->setNotificationType($map[$action]);
86
		$notification->setProviderId($remoteId);
87
		$notification->addNotificationData('sharedSecret', $token);
88
		$notification->addNotificationData('message', $messages[$action]);
89
90
		if ($action === 'permissions') {
91
			$ocmPermissions = $this->permissions->toOcmPermissions($data['permissions']);
92
			$notification->addNotificationData('permission', $ocmPermissions);
93
		}
94
95
		if ($action === 'reshare') {
96
			$ocmPermissions = $this->toOcmPermissions($data['permissions']);
97
			$notification->addNotificationData('permission', $ocmPermissions);
98
			$notification->addNotificationData('senderId', $data['senderId']);
99
			$notification->addNotificationData('shareWith', $data['shareWith']);
100
		}
101
102
		return $notification;
103
	}
104
}
105