Completed
Push — master ( f2b8d7...c7becd )
by Roeland
11:44 queued 10:17
created

OCSController::getTemplates()   A

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\TemplateManager;
28
use OCP\AppFramework\Http\DataResponse;
29
use OCP\AppFramework\OCS\OCSBadRequestException;
30
use OCP\AppFramework\OCS\OCSNotFoundException;
31
use OCP\Files\File;
32
use OCP\Files\Folder;
33
use OCP\Files\IRootFolder;
34
use OCP\Files\NotFoundException;
35
use OCP\IRequest;
36
use OCP\IURLGenerator;
37
38
class OCSController extends \OCP\AppFramework\OCSController {
39
40
	/** @var IRootFolder */
41
	private $rootFolder;
42
43
	/** @var string */
44
	private $userId;
45
46
	/** @var DirectMapper */
47
	private $directMapper;
48
49
	/** @var IURLGenerator */
50
	private $urlGenerator;
51
52
	/** @var TemplateManager */
53
	private $manager;
54
55
	/**
56
	 * OCS controller
57
	 *
58
	 * @param string $appName
59
	 * @param IRequest $request
60
	 * @param IRootFolder $rootFolder
61
	 * @param string $userId
62
	 * @param DirectMapper $directMapper
63
	 * @param IURLGenerator $urlGenerator
64
	 * @param TemplateManager $manager
65
	 */
66 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...
67
		IRequest $request,
68
		IRootFolder $rootFolder,
69
		$userId,
70
		DirectMapper $directMapper,
71
		IURLGenerator $urlGenerator,
72
		TemplateManager $manager) {
73
		parent::__construct($appName, $request);
74
75
		$this->rootFolder   = $rootFolder;
76
		$this->userId       = $userId;
77
		$this->directMapper = $directMapper;
78
		$this->urlGenerator = $urlGenerator;
79
		$this->manager      = $manager;
80
	}
81
82
	/**
83
	 * @NoAdminRequired
84
	 *
85
	 * Init an editing session
86
	 *
87
	 * @param int $fileId
88
	 * @return DataResponse
89
	 * @throws OCSNotFoundException|OCSBadRequestException
90
	 */
91
	public function create($fileId) {
92
		try {
93
			$userFolder = $this->rootFolder->getUserFolder($this->userId);
94
			$nodes      = $userFolder->getById($fileId);
95
96
			if ($nodes === []) {
97
				throw new OCSNotFoundException();
98
			}
99
100
			$node = $nodes[0];
101
			if ($node instanceof Folder) {
0 ignored issues
show
Bug introduced by
The class OCP\Files\Folder does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
102
				throw new OCSBadRequestException('Cannot view folder');
103
			}
104
105
			//TODO check if we can even edit this file with collabora
106
107
			$direct = $this->directMapper->newDirect($this->userId, $fileId);
108
109
			return new DataResponse([
110
				'url' => $this->urlGenerator->linkToRouteAbsolute('richdocuments.directView.show', [
111
					'token' => $direct->getToken()
112
				])
113
			]);
114
		} catch (NotFoundException $e) {
0 ignored issues
show
Bug introduced by
The class OCP\Files\NotFoundException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
115
			throw new OCSNotFoundException();
116
		}
117
	}
118
119
	/**
120
	 * @NoAdminRequired
121
	 *
122
	 * @param string $type The template type
123
	 * @return DataResponse
124
	 * @throws OCSBadRequestException
125
	 */
126
	public function getTemplates($type) {
127
		if (array_key_exists($type, TemplateManager::$tplTypes)) {
128
			$templates = $this->manager->getAllFormatted($type);
129
			return new DataResponse($templates);
130
		}
131
		throw new OCSBadRequestException('Wrong type');
132
	}
133
134
	/**
135
	 * @NoAdminRequired
136
	 *
137
	 * @param string $path Where to create the document
138
	 * @param int $template The template id
139
	 */
140
	public function createFromTemplate($path, $template) {
141
		if ($path === null || $template === null) {
142
			throw new OCSBadRequestException('path and template must be set');
143
		}
144
145
		if (!$this->manager->isTemplate($template)) {
146
			throw new OCSBadRequestException('Invalid template provided');
147
		}
148
149
		$info = pathinfo($path);
150
151
		$userFolder = $this->rootFolder->getUserFolder($this->userId);
152
		$folder = $userFolder->get($info['dirname']);
153
		$name = $folder->getNonExistingName($info['basename']);
154
		$file = $folder->newFile($name);
155
156
		try {
157
			$direct = $this->directMapper->newDirect($this->userId, $template, $file->getId());
158
159
			return new DataResponse([
160
				'url' => $this->urlGenerator->linkToRouteAbsolute('richdocuments.directView.show', [
161
					'token' => $direct->getToken()
162
				])
163
			]);
164
		} catch (NotFoundException $e) {
0 ignored issues
show
Bug introduced by
The class OCP\Files\NotFoundException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
165
			throw new OCSNotFoundException();
166
		}
167
	}
168
}
169