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

DirectViewController::show()   B

Complexity

Conditions 9
Paths 20

Size

Total Lines 65

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 0
Metric Value
dl 0
loc 65
ccs 0
cts 53
cp 0
rs 7.208
c 0
b 0
f 0
cc 9
nc 20
nop 1
crap 90

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * @copyright Copyright (c) 2018, Roeland Jago Douma <[email protected]>
4
 *
5
 * @author Roeland Jago Douma <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
namespace OCA\Richdocuments\Controller;
24
25
use OCA\Richdocuments\AppConfig;
26
use OCA\Richdocuments\Db\DirectMapper;
27
use OCA\Richdocuments\TemplateManager;
28
use OCA\Richdocuments\TokenManager;
29
use OCP\AppFramework\Controller;
30
use OCP\AppFramework\Db\DoesNotExistException;
31
use OCP\AppFramework\Http;
32
use OCP\AppFramework\Http\ContentSecurityPolicy;
33
use OCP\AppFramework\Http\JSONResponse;
34
use OCP\AppFramework\Http\TemplateResponse;
35
use OCP\Files\IRootFolder;
36
use OCP\Files\Node;
37
use OCP\IConfig;
38
use OCP\IRequest;
39
40
class DirectViewController extends Controller {
41
	/** @var IRootFolder */
42
	private $rootFolder;
43
44
	/** @var TokenManager */
45
	private $tokenManager;
46
47
	/** @var DirectMapper */
48
	private $directMapper;
49
50
	/** @var IConfig */
51
	private $config;
52
53
	/** @var AppConfig */
54
	private $appConfig;
55
56
	/** @var TemplateManager */
57
	private $templateManager;
58
59
	public function __construct($appName,
60
								IRequest $request,
61
								IRootFolder $rootFolder,
62
								TokenManager $tokenManager,
63
								DirectMapper $directMapper,
64
								IConfig $config,
65
								AppConfig $appConfig,
66
								TemplateManager $templateManager) {
67
		parent::__construct($appName, $request);
68
69
		$this->rootFolder = $rootFolder;
70
		$this->tokenManager = $tokenManager;
71
		$this->directMapper = $directMapper;
72
		$this->config = $config;
73
		$this->appConfig = $appConfig;
74
		$this->templateManager = $templateManager;
75
	}
76
77
	/**
78
	 * @NoAdminRequired
79
	 * @NoCSRFRequired
80
	 * @PublicPage
81
	 *
82
	 * @param string $token
83
	 */
84
	public function show($token) {
85
		try {
86
			$direct = $this->directMapper->getByToken($token);
87
		} catch (DoesNotExistException $e) {
0 ignored issues
show
Bug introduced by
The class OCP\AppFramework\Db\DoesNotExistException 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...
88
			//TODO show 404
89
			return new JSONResponse([], Http::STATUS_NOT_FOUND);
90
		}
91
92
		// Delete the token. They are for 1 time use only
93
		$this->directMapper->delete($direct);
94
95
		$folder = $this->rootFolder->getUserFolder($direct->getUid());
96
		if ($this->templateManager->isTemplate($direct->getFileid())) {
97
			$item = $this->templateManager->get($direct->getFileid());
98
			if ($direct->getTemplateDestination() === 0 || $direct->getTemplateDestination() === null) {
99
				return new JSONResponse([], Http::STATUS_BAD_REQUEST);
100
			}
101
102
			try {
103
				list($urlSrc, $token) = $this->tokenManager->getTokenForTemplate($item, $direct->getUid(), $direct->getTemplateDestination());
104
			} catch (\Exception $e) {
105
				return new JSONResponse([], Http::STATUS_BAD_REQUEST);
106
			}
107
108
			$relativePath = '/new.odt';
109
110
		} else {
111
			try {
112
				$item = $folder->getById($direct->getFileid())[0];
113
				if(!($item instanceof Node)) {
0 ignored issues
show
Bug introduced by
The class OCP\Files\Node 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...
114
					throw new \Exception();
115
				}
116
117
				list($urlSrc, $token) = $this->tokenManager->getToken($item->getId(), null, $direct->getUid());
118
			} catch (\Exception $e) {
119
				return new JSONResponse([], Http::STATUS_BAD_REQUEST);
120
			}
121
122
			$relativePath = $folder->getRelativePath($item->getPath());
123
		}
124
125
		try {
126
			$params = [
127
				'permissions' => $item->getPermissions(),
128
				'title' => $item->getName(),
129
				'fileId' => $item->getId() . '_' . $this->config->getSystemValue('instanceid'),
130
				'token' => $token,
131
				'urlsrc' => $urlSrc,
132
				'path' => $relativePath,
133
				'instanceId' => $this->config->getSystemValue('instanceid'),
134
				'canonical_webroot' => $this->appConfig->getAppValue('canonical_webroot'),
135
				'direct' => true,
136
			];
137
138
			$response = new TemplateResponse('richdocuments', 'documents', $params, 'empty');
139
			$policy = new ContentSecurityPolicy();
140
			$policy->allowInlineScript(true);
141
			$policy->addAllowedFrameDomain($this->appConfig->getAppValue('wopi_url'));
142
			$response->setContentSecurityPolicy($policy);
143
			return $response;
144
		} catch (\Exception $e) {
145
			return new JSONResponse([], Http::STATUS_BAD_REQUEST);
146
		}
147
148
	}
149
}
150