Passed
Push — master ( f72674...fb5eeb )
by Roeland
11:55
created

prepareCollections()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 4
nop 1
dl 0
loc 13
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
/**
4
 * @copyright Copyright (c) 2018 Joas Schilling <[email protected]>
5
 *
6
 * @license GNU AGPL version 3 or any later version
7
 *
8
 * This program is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License as
10
 * published by the Free Software Foundation, either version 3 of the
11
 * License, or (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU Affero General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Affero General Public License
19
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 *
21
 */
22
23
namespace OC\Core\Controller;
24
25
use Exception;
26
use OCP\AppFramework\Http;
27
use OCP\AppFramework\OCSController;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, OC\Core\Controller\OCSController. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
28
use OCP\AppFramework\Http\DataResponse;
29
use OCP\Collaboration\Resources\CollectionException;
30
use OCP\Collaboration\Resources\ICollection;
31
use OCP\Collaboration\Resources\IManager;
32
use OCP\Collaboration\Resources\IResource;
33
use OCP\Collaboration\Resources\ResourceException;
34
use OCP\ILogger;
35
use OCP\IRequest;
36
use OCP\IUserSession;
37
38
class CollaborationResourcesController extends OCSController {
39
40
	/** @var IManager */
41
	private $manager;
42
	/** @var IUserSession */
43
	private $userSession;
44
	/** @var ILogger */
45
	private $logger;
46
47
	public function __construct(
48
		string $appName,
49
		IRequest $request,
50
		IManager $manager,
51
		IUserSession $userSession,
52
		ILogger $logger
53
	) {
54
		parent::__construct($appName, $request);
55
56
		$this->manager = $manager;
57
		$this->userSession = $userSession;
58
		$this->logger = $logger;
59
	}
60
61
	/**
62
	 * @param int $collectionId
63
	 * @return ICollection
64
	 * @throws CollectionException when the collection was not found for the user
65
	 */
66
	protected function getCollection(int $collectionId): ICollection {
67
		$collection = $this->manager->getCollectionForUser($collectionId, $this->userSession->getUser());
68
69
		if (!$collection->canAccess($this->userSession->getUser())) {
70
			throw new CollectionException('Not found');
71
		}
72
73
		return $collection;
74
	}
75
76
	/**
77
	 * @NoAdminRequired
78
	 *
79
	 * @param int $collectionId
80
	 * @return DataResponse
81
	 */
82
	public function listCollection(int $collectionId): DataResponse {
83
		try {
84
			$collection = $this->getCollection($collectionId);
85
		} catch (CollectionException $e) {
86
			return new DataResponse([], Http::STATUS_NOT_FOUND);
87
		}
88
89
		return $this->respondCollection($collection);
90
	}
91
92
	/**
93
	 * @NoAdminRequired
94
	 *
95
	 * @param string $filter
96
	 * @return DataResponse
97
	 */
98
	public function searchCollections(string $filter): DataResponse {
99
		try {
100
			$collections = $this->manager->searchCollections($this->userSession->getUser(), $filter);
0 ignored issues
show
Bug introduced by
The method searchCollections() does not exist on OCP\Collaboration\Resources\IManager. Since it exists in all sub-types, consider adding an abstract or default implementation to OCP\Collaboration\Resources\IManager. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

100
			/** @scrutinizer ignore-call */ 
101
   $collections = $this->manager->searchCollections($this->userSession->getUser(), $filter);
Loading history...
101
		} catch (CollectionException $e) {
102
			return new DataResponse([], Http::STATUS_NOT_FOUND);
103
		}
104
105
		return new DataResponse($this->prepareCollections($collections));
106
	}
107
108
	/**
109
	 * @NoAdminRequired
110
	 *
111
	 * @param int $collectionId
112
	 * @param string $resourceType
113
	 * @param string $resourceId
114
	 * @return DataResponse
115
	 */
116
	public function addResource(int $collectionId, string $resourceType, string $resourceId): DataResponse {
117
		try {
118
			$collection = $this->getCollection($collectionId);
119
		} catch (CollectionException $e) {
120
			return new DataResponse([], Http::STATUS_NOT_FOUND);
121
		}
122
123
		$resource = $this->manager->createResource($resourceType, $resourceId);
124
125
		if (!$resource->canAccess($this->userSession->getUser())) {
126
			return new DataResponse([], Http::STATUS_NOT_FOUND);
127
		}
128
129
		try {
130
			$collection->addResource($resource);
131
		} catch (ResourceException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
132
		}
133
134
		return $this->respondCollection($collection);
135
	}
136
137
	/**
138
	 * @NoAdminRequired
139
	 *
140
	 * @param int $collectionId
141
	 * @param string $resourceType
142
	 * @param string $resourceId
143
	 * @return DataResponse
144
	 */
145
	public function removeResource(int $collectionId, string $resourceType, string $resourceId): DataResponse {
146
		try {
147
			$collection = $this->getCollection($collectionId);
148
		} catch (CollectionException $e) {
149
			return new DataResponse([], Http::STATUS_NOT_FOUND);
150
		}
151
152
		try {
153
			$resource = $this->manager->getResourceForUser($resourceType, $resourceId, $this->userSession->getUser());
154
		} catch (CollectionException $e) {
155
			return new DataResponse([], Http::STATUS_NOT_FOUND);
156
		}
157
158
		$collection->removeResource($resource);
159
160
		return $this->respondCollection($collection);
161
	}
162
163
	/**
164
	 * @NoAdminRequired
165
	 *
166
	 * @param string $resourceType
167
	 * @param string $resourceId
168
	 * @return DataResponse
169
	 */
170
	public function getCollectionsByResource(string $resourceType, string $resourceId): DataResponse {
171
		try {
172
			$resource = $this->manager->getResourceForUser($resourceType, $resourceId, $this->userSession->getUser());
173
		} catch (ResourceException $e) {
174
			$resource = $this->manager->createResource($resourceType, $resourceId);
175
		}
176
177
		if (!$resource->canAccess($this->userSession->getUser())) {
178
			return new DataResponse([], Http::STATUS_NOT_FOUND);
179
		}
180
181
		return new DataResponse($this->prepareCollections($resource->getCollections()));
182
	}
183
184
	/**
185
	 * @NoAdminRequired
186
	 *
187
	 * @param string $baseResourceType
188
	 * @param string $baseResourceId
189
	 * @param string $name
190
	 * @return DataResponse
191
	 */
192
	public function createCollectionOnResource(string $baseResourceType, string $baseResourceId, string $name): DataResponse {
193
		if (!isset($name[0]) || isset($name[64])) {
194
			return new DataResponse([], Http::STATUS_BAD_REQUEST);
195
		}
196
197
		try {
198
			$resource = $this->manager->createResource($baseResourceType, $baseResourceId);
199
		} catch (CollectionException $e) {
200
			return new DataResponse([], Http::STATUS_NOT_FOUND);
201
		}
202
203
		if (!$resource->canAccess($this->userSession->getUser())) {
204
			return new DataResponse([], Http::STATUS_NOT_FOUND);
205
		}
206
207
		$collection = $this->manager->newCollection($name);
208
		$collection->addResource($resource);
209
210
		return $this->respondCollection($collection);
211
	}
212
213
	/**
214
	 * @NoAdminRequired
215
	 *
216
	 * @param int $collectionId
217
	 * @param string $collectionName
218
	 * @return DataResponse
219
	 */
220
	public function renameCollection(int $collectionId, string $collectionName): DataResponse {
221
		try {
222
			$collection = $this->getCollection($collectionId);
223
		} catch (CollectionException $exception) {
224
			return new DataResponse([], Http::STATUS_NOT_FOUND);
225
		}
226
227
		$collection->setName($collectionName);
228
229
		return $this->respondCollection($collection);
230
	}
231
232
	protected function respondCollection(ICollection $collection): DataResponse {
233
		try {
234
			return new DataResponse($this->prepareCollection($collection));
235
		} catch (CollectionException $e) {
236
			return new DataResponse([], Http::STATUS_NOT_FOUND);
237
		} catch (Exception $e) {
238
			$this->logger->logException($e);
239
			return new DataResponse([], Http::STATUS_INTERNAL_SERVER_ERROR);
240
		}
241
	}
242
243
	protected function prepareCollections(array $collections): array {
244
		$result = [];
245
246
		foreach ($collections as $collection) {
247
			try {
248
				$result[] = $this->prepareCollection($collection);
249
			} catch (CollectionException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
250
			} catch (Exception $e) {
251
				$this->logger->logException($e);
252
			}
253
		}
254
255
		return $result;
256
	}
257
258
	protected function prepareCollection(ICollection $collection): array {
259
		if (!$collection->canAccess($this->userSession->getUser())) {
260
			throw new CollectionException('Can not access collection');
261
		}
262
263
		return [
264
			'id' => $collection->getId(),
265
			'name' => $collection->getName(),
266
			'resources' => $this->prepareResources($collection->getResources()),
267
		];
268
	}
269
270
	protected function prepareResources(array $resources): ?array {
271
		$result = [];
272
273
		foreach ($resources as $resource) {
274
			try {
275
				$result[] = $this->prepareResource($resource);
276
			} catch (ResourceException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
277
			} catch (Exception $e) {
278
				$this->logger->logException($e);
279
			}
280
		}
281
282
		return $result;
283
	}
284
285
	protected function prepareResource(IResource $resource): array {
286
		if (!$resource->canAccess($this->userSession->getUser())) {
287
			throw new ResourceException('Can not access resource');
288
		}
289
290
		return $resource->getRichObject();
291
	}
292
}
293