Completed
Push — master ( ed4ed7...0864f5 )
by Lukas
115:48 queued 101:14
created

ApiController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 15

Duplication

Lines 16
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 15
nc 1
nop 8
dl 16
loc 16
rs 9.4285
c 0
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Christoph Wurst <[email protected]>
6
 * @author Joas Schilling <[email protected]>
7
 * @author Lukas Reschke <[email protected]>
8
 * @author Morris Jobke <[email protected]>
9
 * @author Roeland Jago Douma <[email protected]>
10
 * @author Thomas Müller <[email protected]>
11
 * @author Tobias Kaminsky <[email protected]>
12
 * @author Vincent Petry <[email protected]>
13
 *
14
 * @license AGPL-3.0
15
 *
16
 * This code is free software: you can redistribute it and/or modify
17
 * it under the terms of the GNU Affero General Public License, version 3,
18
 * as published by the Free Software Foundation.
19
 *
20
 * This program is distributed in the hope that it will be useful,
21
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23
 * GNU Affero General Public License for more details.
24
 *
25
 * You should have received a copy of the GNU Affero General Public License, version 3,
26
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
27
 *
28
 */
29
30
namespace OCA\Files\Controller;
31
32
use OCP\AppFramework\Http;
33
use OCP\AppFramework\Controller;
34
use OCP\Files\Folder;
35
use OCP\IConfig;
36
use OCP\IRequest;
37
use OCP\AppFramework\Http\DataResponse;
38
use OCP\AppFramework\Http\DataDisplayResponse;
39
use OCP\AppFramework\Http\Response;
40
use OCA\Files\Service\TagService;
41
use OCP\IPreview;
42
use OCP\Share\IManager;
43
use OC\Files\Node\Node;
44
use OCP\IUserSession;
45
46
/**
47
 * Class ApiController
48
 *
49
 * @package OCA\Files\Controller
50
 */
51
class ApiController extends Controller {
52
	/** @var TagService */
53
	private $tagService;
54
	/** @var IManager **/
55
	private $shareManager;
56
	/** @var IPreview */
57
	private $previewManager;
58
	/** IUserSession */
59
	private $userSession;
60
	/** IConfig */
61
	private $config;
62
	/** @var Folder */
63
	private $userFolder;
64
65
	/**
66
	 * @param string $appName
67
	 * @param IRequest $request
68
	 * @param IUserSession $userSession
69
	 * @param TagService $tagService
70
	 * @param IPreview $previewManager
71
	 * @param IManager $shareManager
72
	 * @param IConfig $config
73
	 * @param Folder $userFolder
74
	 */
75 View Code Duplication
	public function __construct($appName,
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
76
								IRequest $request,
77
								IUserSession $userSession,
78
								TagService $tagService,
79
								IPreview $previewManager,
80
								IManager $shareManager,
81
								IConfig $config,
82
								Folder $userFolder) {
83
		parent::__construct($appName, $request);
84
		$this->userSession = $userSession;
85
		$this->tagService = $tagService;
86
		$this->previewManager = $previewManager;
87
		$this->shareManager = $shareManager;
88
		$this->config = $config;
89
		$this->userFolder = $userFolder;
90
	}
91
92
	/**
93
	 * Gets a thumbnail of the specified file
94
	 *
95
	 * @since API version 1.0
96
	 *
97
	 * @NoAdminRequired
98
	 * @NoCSRFRequired
99
	 * @StrictCookieRequired
100
	 *
101
	 * @param int $x
102
	 * @param int $y
103
	 * @param string $file URL-encoded filename
104
	 * @return DataResponse|DataDisplayResponse
105
	 */
106
	public function getThumbnail($x, $y, $file) {
107
		if($x < 1 || $y < 1) {
108
			return new DataResponse(['message' => 'Requested size must be numeric and a positive value.'], Http::STATUS_BAD_REQUEST);
109
		}
110
111
		$preview = $this->previewManager->createPreview('files/'.$file, $x, $y, true);
112
		if ($preview->valid()) {
113
			return new DataDisplayResponse($preview->data(), Http::STATUS_OK, ['Content-Type' => 'image/png']);
114
		} else {
115
			return new DataResponse(['message' => 'File not found.'], Http::STATUS_NOT_FOUND);
116
		}
117
	}
118
119
	/**
120
	 * Updates the info of the specified file path
121
	 * The passed tags are absolute, which means they will
122
	 * replace the actual tag selection.
123
	 *
124
	 * @NoAdminRequired
125
	 *
126
	 * @param string $path path
127
	 * @param array|string $tags array of tags
128
	 * @return DataResponse
129
	 */
130
	public function updateFileTags($path, $tags = null) {
131
		$result = [];
132
		// if tags specified or empty array, update tags
133
		if (!is_null($tags)) {
134
			try {
135
				$this->tagService->updateFileTags($path, $tags);
0 ignored issues
show
Bug introduced by
It seems like $tags defined by parameter $tags on line 130 can also be of type string; however, OCA\Files\Service\TagService::updateFileTags() does only seem to accept array, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
136
			} catch (\OCP\Files\NotFoundException $e) {
137
				return new DataResponse([
138
					'message' => $e->getMessage()
139
				], Http::STATUS_NOT_FOUND);
140
			} catch (\OCP\Files\StorageNotAvailableException $e) {
141
				return new DataResponse([
142
					'message' => $e->getMessage()
143
				], Http::STATUS_SERVICE_UNAVAILABLE);
144
			} catch (\Exception $e) {
145
				return new DataResponse([
146
					'message' => $e->getMessage()
147
				], Http::STATUS_NOT_FOUND);
148
			}
149
			$result['tags'] = $tags;
150
		}
151
		return new DataResponse($result);
152
	}
153
154
	/**
155
	 * @param \OCP\Files\Node[] $nodes
156
	 * @return array
157
	 */
158
	private function formatNodes(array $nodes) {
159
		return array_values(array_map(function (Node $node) {
160
			/** @var \OC\Files\Node\Node $shareTypes */
161
			$shareTypes = $this->getShareTypes($node);
162
			$file = \OCA\Files\Helper::formatFileInfo($node->getFileInfo());
163
			$parts = explode('/', dirname($node->getPath()), 4);
164
			if (isset($parts[3])) {
165
				$file['path'] = '/' . $parts[3];
166
			} else {
167
				$file['path'] = '/';
168
			}
169
			if (!empty($shareTypes)) {
170
				$file['shareTypes'] = $shareTypes;
171
			}
172
			return $file;
173
		}, $nodes));
174
	}
175
176
	/**
177
	 * Returns a list of recently modifed files.
178
	 *
179
	 * @NoAdminRequired
180
	 *
181
	 * @return DataResponse
182
	 */
183
	public function getRecentFiles() {
184
		$nodes = $this->userFolder->getRecent(100);
185
		$files = $this->formatNodes($nodes);
186
		return new DataResponse(['files' => $files]);
187
	}
188
189
	/**
190
	 * Return a list of share types for outgoing shares
191
	 *
192
	 * @param Node $node file node
193
	 *
194
	 * @return int[] array of share types
195
	 */
196 View Code Duplication
	private function getShareTypes(Node $node) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
197
		$userId = $this->userSession->getUser()->getUID();
198
		$shareTypes = [];
199
		$requestedShareTypes = [
200
			\OCP\Share::SHARE_TYPE_USER,
201
			\OCP\Share::SHARE_TYPE_GROUP,
202
			\OCP\Share::SHARE_TYPE_LINK,
203
			\OCP\Share::SHARE_TYPE_REMOTE
204
		];
205
		foreach ($requestedShareTypes as $requestedShareType) {
206
			// one of each type is enough to find out about the types
207
			$shares = $this->shareManager->getSharesBy(
208
				$userId,
209
				$requestedShareType,
210
				$node,
211
				false,
212
				1
213
			);
214
			if (!empty($shares)) {
215
				$shareTypes[] = $requestedShareType;
216
			}
217
		}
218
		return $shareTypes;
219
	}
220
221
	/**
222
	 * Change the default sort mode
223
	 *
224
	 * @NoAdminRequired
225
	 *
226
	 * @param string $mode
227
	 * @param string $direction
228
	 * @return Response
229
	 */
230
	public function updateFileSorting($mode, $direction) {
231
		$allowedMode = ['name', 'size', 'mtime'];
232
		$allowedDirection = ['asc', 'desc'];
233
		if (!in_array($mode, $allowedMode) || !in_array($direction, $allowedDirection)) {
234
			$response = new Response();
235
			$response->setStatus(Http::STATUS_UNPROCESSABLE_ENTITY);
236
			return $response;
237
		}
238
		$this->config->setUserValue($this->userSession->getUser()->getUID(), 'files', 'file_sorting', $mode);
239
		$this->config->setUserValue($this->userSession->getUser()->getUID(), 'files', 'file_sorting_direction', $direction);
240
		return new Response();
241
	}
242
243
	/**
244
	 * Toggle default for showing/hiding hidden files
245
	 *
246
	 * @NoAdminRequired
247
	 *
248
	 * @param bool $show
249
	 */
250
	public function showHiddenFiles($show) {
251
		$this->config->setUserValue($this->userSession->getUser()->getUID(), 'files', 'show_hidden', (int) $show);
252
		return new Response();
253
	}
254
255
}
256