Completed
Push — master ( 7f194b...f212c6 )
by Morris
18:03 queued 13s
created

DirectController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 15
nc 1
nop 8
dl 0
loc 17
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
declare(strict_types=1);
3
/**
4
 * @copyright 2018, Roeland Jago Douma <[email protected]>
5
 *
6
 * @author Roeland Jago Douma <[email protected]>
7
 *
8
 * @license GNU AGPL version 3 or any later version
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License as
12
 * published by the Free Software Foundation, either version 3 of the
13
 * License, or (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU Affero General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public License
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
 *
23
 */
24
25
namespace OCA\DAV\Controller;
26
27
use OCA\DAV\Db\Direct;
28
use OCA\DAV\Db\DirectMapper;
29
use OCP\AppFramework\Http\DataResponse;
30
use OCP\AppFramework\OCS\OCSBadRequestException;
31
use OCP\AppFramework\OCS\OCSNotFoundException;
32
use OCP\AppFramework\OCSController;
33
use OCP\AppFramework\Utility\ITimeFactory;
34
use OCP\Files\File;
35
use OCP\Files\IRootFolder;
36
use OCP\IRequest;
37
use OCP\IURLGenerator;
38
use OCP\Security\ISecureRandom;
39
40
class DirectController extends OCSController {
41
42
	/** @var IRootFolder */
43
	private $rootFolder;
44
45
	/** @var string */
46
	private $userId;
47
48
	/** @var DirectMapper */
49
	private $mapper;
50
51
	/** @var ISecureRandom */
52
	private $random;
53
54
	/** @var ITimeFactory */
55
	private $timeFactory;
56
57
	/** @var IURLGenerator */
58
	private $urlGenerator;
59
60
61
	public function __construct(string $appName,
62
								IRequest $request,
63
								IRootFolder $rootFolder,
64
								string $userId,
65
								DirectMapper $mapper,
66
								ISecureRandom $random,
67
								ITimeFactory $timeFactory,
68
								IURLGenerator $urlGenerator) {
69
		parent::__construct($appName, $request);
70
71
		$this->rootFolder = $rootFolder;
72
		$this->userId = $userId;
73
		$this->mapper = $mapper;
74
		$this->random = $random;
75
		$this->timeFactory = $timeFactory;
76
		$this->urlGenerator = $urlGenerator;
77
	}
78
79
	/**
80
	 * @NoAdminRequired
81
	 */
82
	public function getUrl(int $fileId): DataResponse {
83
		$userFolder = $this->rootFolder->getUserFolder($this->userId);
84
85
		$files = $userFolder->getById($fileId);
86
87
		if ($files === []) {
88
			throw new OCSNotFoundException();
89
		}
90
91
		$file = array_shift($files);
92
		if (!($file instanceof File)) {
93
			throw new OCSBadRequestException('Direct download only works for files');
94
		}
95
96
		//TODO: at some point we should use the directdownlaod function of storages
97
		$direct = new Direct();
98
		$direct->setUserId($this->userId);
99
		$direct->setFileId($fileId);
100
101
		$token = $this->random->generate(60, ISecureRandom::CHAR_UPPER . ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_DIGITS);
102
		$direct->setToken($token);
103
		$direct->setExpiration($this->timeFactory->getTime() + 60 * 60 * 8);
104
105
		$this->mapper->insert($direct);
106
107
		$url = $this->urlGenerator->getAbsoluteURL('remote.php/direct/'.$token);
108
109
		return new DataResponse([
110
			'url' => $url,
111
		]);
112
	}
113
}
114