Completed
Push — master ( 125f9f...2c140b )
by Julius
02:26 queued 10s
created

DirectViewController::show()   C

Complexity

Conditions 10
Paths 24

Size

Total Lines 81

Duplication

Lines 5
Ratio 6.17 %

Code Coverage

Tests 0
CRAP Score 110

Importance

Changes 0
Metric Value
dl 5
loc 81
ccs 0
cts 68
cp 0
rs 6.5477
c 0
b 0
f 0
cc 10
nc 24
nop 1
crap 110

How to fix   Long Method    Complexity   

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\Service\FederationService;
28
use OCA\Richdocuments\TemplateManager;
29
use OCA\Richdocuments\TokenManager;
30
use OCP\AppFramework\Controller;
31
use OCP\AppFramework\Db\DoesNotExistException;
32
use OCP\AppFramework\Http;
33
use OCP\AppFramework\Http\ContentSecurityPolicy;
34
use OCP\AppFramework\Http\JSONResponse;
35
use OCP\AppFramework\Http\RedirectResponse;
36
use OCP\AppFramework\Http\TemplateResponse;
37
use OCP\Files\IRootFolder;
38
use OCP\Files\Node;
39
use OCP\Files\NotFoundException;
40
use OCP\IConfig;
41
use OCP\IRequest;
42
43
class DirectViewController extends Controller {
44
	/** @var IRootFolder */
45
	private $rootFolder;
46
47
	/** @var TokenManager */
48
	private $tokenManager;
49
50
	/** @var DirectMapper */
51
	private $directMapper;
52
53
	/** @var IConfig */
54
	private $config;
55
56
	/** @var AppConfig */
57
	private $appConfig;
58
59
	/** @var TemplateManager */
60
	private $templateManager;
61
62
	/** @var FederationService */
63
	private $federationService;
64
65 View Code Duplication
	public function __construct(
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...
66
		$appName,
67
		IRequest $request,
68
		IRootFolder $rootFolder,
69
		TokenManager $tokenManager,
70
		DirectMapper $directMapper,
71
		IConfig $config,
72
		AppConfig $appConfig,
73
		TemplateManager $templateManager,
74
		FederationService $federationService
75
	) {
76
		parent::__construct($appName, $request);
77
78
		$this->rootFolder = $rootFolder;
79
		$this->tokenManager = $tokenManager;
80
		$this->directMapper = $directMapper;
81
		$this->config = $config;
82
		$this->appConfig = $appConfig;
83
		$this->templateManager = $templateManager;
84
		$this->federationService = $federationService;
85
	}
86
87
	/**
88
	 * @NoAdminRequired
89
	 * @NoCSRFRequired
90
	 * @PublicPage
91
	 *
92
	 * @param string $token
93
	 * @return JSONResponse|RedirectResponse|TemplateResponse
94
	 * @throws NotFoundException
95
	 */
96
	public function show($token) {
97
		try {
98
			$direct = $this->directMapper->getByToken($token);
99
		} catch (DoesNotExistException $e) {
100
			$params = [
101
				'errors' => [['error' => $e->getMessage()]]
102
			];
103
			return new TemplateResponse('core', 'error', $params, 'guest');
104
		}
105
106
		// Delete the token. They are for 1 time use only
107
		$this->directMapper->delete($direct);
0 ignored issues
show
Deprecated Code introduced by
The method OCP\AppFramework\Db\Mapper::delete() has been deprecated with message: 14.0.0 Move over to QBMapper

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
108
109
		$folder = $this->rootFolder->getUserFolder($direct->getUid());
110
		if ($this->templateManager->isTemplate($direct->getFileid())) {
111
			$item = $this->templateManager->get($direct->getFileid());
112
			if ($direct->getTemplateDestination() === 0 || $direct->getTemplateDestination() === null) {
113
				return new JSONResponse([], Http::STATUS_BAD_REQUEST);
114
			}
115
116
			try {
117
				list($urlSrc, $token) = $this->tokenManager->getTokenForTemplate($item, $direct->getUid(), $direct->getTemplateDestination(), true);
118
			} catch (\Exception $e) {
119
				return new JSONResponse([], Http::STATUS_BAD_REQUEST);
120
			}
121
122
			$relativePath = '/new.odt';
123
124
		} else {
125
			try {
126
				$item = $folder->getById($direct->getFileid())[0];
127
				if(!($item instanceof Node)) {
128
					throw new \Exception();
129
				}
130
131
				/** Open file from remote collabora */
132
				$federatedUrl = $this->federationService->getRemoteRedirectURL($item, $direct);
0 ignored issues
show
Compatibility introduced by
$item of type object<OCP\Files\Node> is not a sub-type of object<OCP\Files\File>. It seems like you assume a child interface of the interface OCP\Files\Node to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
133 View Code Duplication
				if ($federatedUrl !== null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
134
					$response = new RedirectResponse($federatedUrl);
135
					$response->addHeader('X-Frame-Options', 'ALLOW');
136
					return $response;
137
				}
138
139
				list($urlSrc, $token) = $this->tokenManager->getToken($item->getId(), null, $direct->getUid(), true);
140
			} catch (\Exception $e) {
141
				$params = [
142
					'errors' => [['error' => $e->getMessage()]]
143
				];
144
				return new TemplateResponse('core', 'error', $params, 'guest');
145
			}
146
147
			$relativePath = $folder->getRelativePath($item->getPath());
148
		}
149
150
		try {
151
			$params = [
152
				'permissions' => $item->getPermissions(),
153
				'title' => $item->getName(),
154
				'fileId' => $item->getId() . '_' . $this->config->getSystemValue('instanceid'),
155
				'token' => $token,
156
				'urlsrc' => $urlSrc,
157
				'path' => $relativePath,
158
				'instanceId' => $this->config->getSystemValue('instanceid'),
159
				'canonical_webroot' => $this->appConfig->getAppValue('canonical_webroot'),
160
				'direct' => true,
161
			];
162
163
			$response = new TemplateResponse('richdocuments', 'documents', $params, 'empty');
164
			$policy = new ContentSecurityPolicy();
165
			$policy->allowInlineScript(true);
0 ignored issues
show
Deprecated Code introduced by
The method OCP\AppFramework\Http\Em...cy::allowInlineScript() has been deprecated with message: 10.0 CSP tokens are now used

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
166
			$policy->addAllowedFrameDomain($this->appConfig->getAppValue('public_wopi_url'));
167
			$response->setContentSecurityPolicy($policy);
168
			return $response;
169
		} catch (\Exception $e) {
170
			$params = [
171
				'errors' => [['error' => $e->getMessage()]]
172
			];
173
			return new TemplateResponse('core', 'error', $params, 'guest');
174
		}
175
176
	}
177
}
178