Completed
Push — master ( 9373da...430223 )
by Maxence
03:17 queued 12s
created

FileShare   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 143
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 8
dl 0
loc 143
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A verify() 0 4 1
B manage() 0 61 2
A result() 0 21 1
1
<?php
2
3
declare(strict_types=1);
4
5
6
/**
7
 * Circles - Bring cloud-users closer together.
8
 *
9
 * This file is licensed under the Affero General Public License version 3 or
10
 * later. See the COPYING file.
11
 *
12
 * @author Maxence Lange <[email protected]>
13
 * @copyright 2021
14
 * @license GNU AGPL version 3 or any later version
15
 *
16
 * This program is free software: you can redistribute it and/or modify
17
 * it under the terms of the GNU Affero General Public License as
18
 * published by the Free Software Foundation, either version 3 of the
19
 * License, or (at your option) any later version.
20
 *
21
 * This program is distributed in the hope that it will be useful,
22
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24
 * GNU Affero General Public License for more details.
25
 *
26
 * You should have received a copy of the GNU Affero General Public License
27
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28
 *
29
 */
30
31
32
namespace OCA\Circles\FederatedItems\Files;
33
34
35
use daita\MySmallPhpTools\Exceptions\InvalidItemException;
36
use daita\MySmallPhpTools\Exceptions\UnknownTypeException;
37
use daita\MySmallPhpTools\Traits\Nextcloud\nc22\TNC22Logger;
38
use daita\MySmallPhpTools\Traits\TStringTools;
39
use OCA\Circles\Db\MountRequest;
40
use OCA\Circles\Exceptions\CircleNotFoundException;
41
use OCA\Circles\IFederatedItem;
42
use OCA\Circles\IFederatedItemAsyncProcess;
43
use OCA\Circles\IFederatedItemMemberEmpty;
44
use OCA\Circles\Model\Federated\FederatedEvent;
45
use OCA\Circles\Model\Mount;
46
use OCA\Circles\Model\ShareWrapper;
47
use OCA\Circles\Service\ConfigService;
48
use OCA\Circles\Service\EventService;
49
50
51
/**
52
 * Class FileShare
53
 *
54
 * @package OCA\Circles\FederatedItems\Files
55
 */
56
class FileShare implements
57
	IFederatedItem,
58
	IFederatedItemAsyncProcess,
59
	IFederatedItemMemberEmpty {
60
61
62
	use TStringTools;
63
	use TNC22Logger;
64
65
66
	/** @var MountRequest */
67
	private $mountRequest;
68
69
	/** @var EventService */
70
	private $eventService;
71
72
	/** @var ConfigService */
73
	private $configService;
74
75
76
	/**
77
	 * FileShare constructor.
78
	 *
79
	 * @param MountRequest $mountRequest
80
	 * @param ConfigService $configService
81
	 */
82
	public function __construct(
83
		MountRequest $mountRequest,
84
		EventService $eventService,
85
		ConfigService $configService
86
	) {
87
		$this->mountRequest = $mountRequest;
88
		$this->eventService = $eventService;
89
		$this->configService = $configService;
90
	}
91
92
93
	/**
94
	 * @param FederatedEvent $event
95
	 */
96
	public function verify(FederatedEvent $event): void {
97
		// TODO: check and improve
98
		// TODO: Could we use a share lock ?
99
	}
100
101
102
	/**
103
	 * @param FederatedEvent $event
104
	 *
105
	 * @throws InvalidItemException
106
	 * @throws UnknownTypeException
107
	 * @throws CircleNotFoundException
108
	 */
109
	public function manage(FederatedEvent $event): void {
110
		if ($this->configService->isLocalInstance($event->getOrigin())) {
111
			return;
112
		}
113
114
		/** @var ShareWrapper $wrappedShare */
115
		$wrappedShare = $event->getData()->gObj('wrappedShare', ShareWrapper::class);
116
		$mount = new Mount();
117
		$mount->fromShare($wrappedShare);
118
		$mount->setMountId($this->token(15));
119
120
		$this->mountRequest->save($mount);
121
		$this->eventService->federatedShareCreated($wrappedShare, $mount);
122
123
//		$this->mountRequest->create($mount);
124
//		$circle = $event->getDeprecatedCircle();
125
//
126
//		// if event is not local, we create a federated file to the right instance of Nextcloud, using the right token
127
//		if (!$this->configService->isLocalInstance($event->getSource())) {
128
//			try {
129
//				$share = $this->getShareFromData($event->getData());
130
//			} catch (Exception $e) {
131
//				return;
132
//			}
133
//
134
//			$data = $event->getData();
135
//			$token = $data->g('gs_federated');
136
//			$filename = $data->g('gs_filename');
137
//
138
//			$gsShare = new GSShare($share->getSharedWith(), $token);
139
//			$gsShare->setOwner($share->getShareOwner());
140
//			$gsShare->setInstance($event->getSource());
141
//			$gsShare->setParent(-1);
142
//			$gsShare->setMountPoint($filename);
143
//
144
//			$this->gsSharesRequest->create($gsShare);
145
//		} else {
146
//			// if the event is local, we send mail to mail-as-members
147
//			$members = $this->membersRequest->forceGetMembers(
148
//				$circle->getUniqueId(), DeprecatedMember::LEVEL_MEMBER, DeprecatedMember::TYPE_MAIL, true
149
//			);
150
//
151
//			foreach ($members as $member) {
152
//				$this->sendShareToContact($event, $circle, $member->getMemberId(), [$member->getUserId()]);
153
//			}
154
//		}
155
//
156
//		// we also fill the event's result for further things, like contact-as-members
157
//		$members = $this->membersRequest->forceGetMembers(
158
//			$circle->getUniqueId(), DeprecatedMember::LEVEL_MEMBER, DeprecatedMember::TYPE_CONTACT, true
159
//		);
160
//
161
//		$accounts = [];
162
//		foreach ($members as $member) {
163
//			if ($member->getInstance() === '') {
164
//				$accounts[] = $this->miscService->getInfosFromContact($member);
165
//			}
166
//		}
167
//
168
//		$event->setResult(new SimpleDataStore(['contacts' => $accounts]));
169
	}
170
171
172
	/**
173
	 * @param FederatedEvent $event
174
	 * @param array $results
175
	 */
176
	public function result(FederatedEvent $event, array $results): void {
177
//		$event = null;
178
//		$contacts = [];
179
//		foreach (array_keys($events) as $instance) {
180
//			$event = $events[$instance];
181
//			$contacts = array_merge(
182
//				$contacts, $event->getResult()
183
//								 ->gArray('contacts')
184
//			);
185
//		}
186
//
187
//		if ($event === null || !$event->hasCircle()) {
188
//			return;
189
//		}
190
//
191
//		$circle = $event->getDeprecatedCircle();
192
//
193
//		foreach ($contacts as $contact) {
194
//			$this->sendShareToContact($event, $circle, $contact['memberId'], $contact['emails']);
195
//		}
196
	}
197
198
}
199
200