OCSController::getTemplates()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 0
cts 7
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 6
1
<?php
2
/**
3
 * @copyright Copyright (c) 2018, Roeland Jago Douma <[email protected]>
4
 *
5
 * @author Roeland Jago Douma <[email protected]>
6
 * @author John Molakvoæ <[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
namespace OCA\Richdocuments\Controller;
25
26
use OCA\Richdocuments\Db\DirectMapper;
27
use OCA\Richdocuments\Service\FederationService;
28
use OCA\Richdocuments\TemplateManager;
29
use OCP\AppFramework\Http\DataResponse;
30
use OCP\AppFramework\OCS\OCSBadRequestException;
31
use OCP\AppFramework\OCS\OCSNotFoundException;
32
use OCP\Files\File;
33
use OCP\Files\Folder;
34
use OCP\Files\IRootFolder;
35
use OCP\Files\NotFoundException;
36
use OCP\IRequest;
37
use OCP\IURLGenerator;
38
39
class OCSController extends \OCP\AppFramework\OCSController {
40
41
	/** @var IRootFolder */
42
	private $rootFolder;
43
44
	/** @var string */
45
	private $userId;
46
47
	/** @var DirectMapper */
48
	private $directMapper;
49
50
	/** @var IURLGenerator */
51
	private $urlGenerator;
52
53
	/** @var TemplateManager */
54
	private $manager;
55
56
	/** @var FederationService */
57
	private $federationService;
58
59
	/**
60
	 * OCS controller
61
	 *
62
	 * @param string $appName
63
	 * @param IRequest $request
64
	 * @param IRootFolder $rootFolder
65
	 * @param string $userId
66
	 * @param DirectMapper $directMapper
67
	 * @param IURLGenerator $urlGenerator
68
	 * @param TemplateManager $manager
69
	 */
70 View Code Duplication
	public function __construct(string $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...
71
		IRequest $request,
72
		IRootFolder $rootFolder,
73
		$userId,
74
		DirectMapper $directMapper,
75
		IURLGenerator $urlGenerator,
76
		TemplateManager $manager,
77
		FederationService $federationService
78
	) {
79
		parent::__construct($appName, $request);
80
81
		$this->rootFolder   = $rootFolder;
82
		$this->userId       = $userId;
83
		$this->directMapper = $directMapper;
84
		$this->urlGenerator = $urlGenerator;
85
		$this->manager      = $manager;
86
		$this->federationService = $federationService;
87
	}
88
89
	/**
90
	 * @NoAdminRequired
91
	 *
92
	 * Init an editing session
93
	 *
94
	 * @param int $fileId
95
	 * @return DataResponse
96
	 * @throws OCSNotFoundException|OCSBadRequestException
97
	 */
98
	public function create($fileId) {
99
		try {
100
			$userFolder = $this->rootFolder->getUserFolder($this->userId);
101
			$nodes      = $userFolder->getById($fileId);
102
103
			if ($nodes === []) {
104
				throw new OCSNotFoundException();
105
			}
106
107
			$node = $nodes[0];
108
			if ($node instanceof Folder) {
109
				throw new OCSBadRequestException('Cannot view folder');
110
			}
111
112
			$direct = $this->directMapper->newDirect($this->userId, $fileId);
113
114
			return new DataResponse([
115
				'url' => $this->urlGenerator->linkToRouteAbsolute('richdocuments.directView.show', [
116
					'token' => $direct->getToken()
117
				])
118
			]);
119
		} catch (NotFoundException $e) {
120
			throw new OCSNotFoundException();
121
		}
122
	}
123
124
	/**
125
	 * @NoAdminRequired
126
	 *
127
	 * @param string $type The template type
128
	 * @return DataResponse
129
	 * @throws OCSBadRequestException
130
	 */
131
	public function getTemplates($type) {
132
		if (array_key_exists($type, TemplateManager::$tplTypes)) {
133
			$templates = $this->manager->getAllFormatted($type);
134
			return new DataResponse($templates);
135
		}
136
		throw new OCSBadRequestException('Wrong type');
137
	}
138
139
	/**
140
	 * @NoAdminRequired
141
	 *
142
	 * @param string $path Where to create the document
143
	 * @param int $template The template id
144
	 */
145
	public function createFromTemplate($path, $template) {
146
		if ($path === null || $template === null) {
147
			throw new OCSBadRequestException('path and template must be set');
148
		}
149
150
		if (!$this->manager->isTemplate($template)) {
151
			throw new OCSBadRequestException('Invalid template provided');
152
		}
153
154
		$info = $this->mb_pathinfo($path);
155
156
		$userFolder = $this->rootFolder->getUserFolder($this->userId);
157
		$folder = $userFolder->get($info['dirname']);
158
		$name = $folder->getNonExistingName($info['basename']);
0 ignored issues
show
Bug introduced by
The method getNonExistingName() does not seem to exist on object<OCP\Files\Node>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
159
		$file = $folder->newFile($name);
0 ignored issues
show
Bug introduced by
The method newFile() does not seem to exist on object<OCP\Files\Node>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
160
161
		try {
162
			$direct = $this->directMapper->newDirect($this->userId, $template, $file->getId());
163
164
			return new DataResponse([
165
				'url' => $this->urlGenerator->linkToRouteAbsolute('richdocuments.directView.show', [
166
					'token' => $direct->getToken()
167
				])
168
			]);
169
		} catch (NotFoundException $e) {
170
			throw new OCSNotFoundException();
171
		}
172
	}
173
174
	private function mb_pathinfo($filepath) {
175
		$result = [];
176
		preg_match('%^(.*?)[\\\\/]*(([^/\\\\]*?)(\.([^\.\\\\/]+?)|))[\\\\/\.]*$%im', ltrim('/' . $filepath), $matches);
177
		if($matches[1]) {
178
			$result['dirname'] = $matches[1];
179
		}
180
		if($matches[2]) {
181
			$result['basename'] = $matches[2];
182
		}
183
		if($matches[5]) {
184
			$result['extension'] = $matches[5];
185
		}
186
		if($matches[3]) {
187
			$result['filename'] = $matches[3];
188
		}
189
		return $result;
190
	}
191
}
192