Completed
Push — master ( 14bd36...94616a )
by Roeland
15s
created

AssetsController   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 86
Duplicated Lines 15.12 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 2
dl 13
loc 86
ccs 0
cts 46
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 13 13 1
A create() 0 17 2
A get() 0 26 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
24
namespace OCA\Richdocuments\Controller;
25
26
use OCA\Richdocuments\Db\AssetMapper;
27
use OCP\AppFramework\Controller;
28
use OCP\AppFramework\Db\DoesNotExistException;
29
use OCP\AppFramework\Http;
30
use OCP\AppFramework\Http\DataResponse;
31
use OCP\AppFramework\Http\FileDisplayResponse;
32
use OCP\AppFramework\Http\JSONResponse;
33
use OCP\Files\File;
34
use OCP\Files\IRootFolder;
35
use OCP\Files\NotFoundException;
36
use OCP\IRequest;
37
use OCP\IURLGenerator;
38
39
class AssetsController extends Controller {
40
41
	/** @var AssetMapper */
42
	private $assetMapper;
43
44
	/** @var IRootFolder */
45
	private $rootFolder;
46
47
	/** @var string */
48
	private $userId;
49
50
	/** @var IURLGenerator */
51
	private $urlGenerator;
52
53 View Code Duplication
	public function __construct($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...
54
								IRequest $request,
55
								AssetMapper $assetMapper,
56
								IRootFolder $rootFolder,
57
								$userId,
58
								IURLGenerator $urlGenerator) {
59
		parent::__construct($appName, $request);
60
61
		$this->assetMapper = $assetMapper;
62
		$this->rootFolder = $rootFolder;
63
		$this->userId = $userId;
64
		$this->urlGenerator = $urlGenerator;
65
	}
66
67
	/**
68
	 * @NoAdminRequired
69
	 *
70
	 * @param string $path
71
	 * @return JSONResponse
72
	 */
73
	public function create($path) {
74
		$userFolder = $this->rootFolder->getUserFolder($this->userId);
75
76
		try {
77
			$node = $userFolder->get($path);
78
		} 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...
79
			return new JSONResponse([], Http::STATUS_NOT_FOUND);
80
		}
81
82
		$asset = $this->assetMapper->newAsset($this->userId, $node->getId());
83
		
84
		return new JSONResponse([
85
			'url' => $this->urlGenerator->linkToRouteAbsolute('richdocuments.assets.get', [
86
				'token' => $asset->getToken(),
87
			])
88
		]);
89
	}
90
91
	/**
92
	 * @PublicPage
93
	 * @NoCSRFRequired
94
	 *
95
	 * @param string $token
96
	 * @return Http\Response
97
	 */
98
	public function get($token) {
99
		try {
100
			$asset = $this->assetMapper->getAssetByToken($token);
101
		} 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...
102
			return new DataResponse([], Http::STATUS_NOT_FOUND);
103
		}
104
105
		// Clear the assets so we can only fetch it once
106
		$this->assetMapper->delete($asset);
107
108
		$userFolder = $this->rootFolder->getUserFolder($asset->getUid());
109
		$nodes = $userFolder->getById($asset->getFileid());
110
111
		if ($nodes === []) {
112
			return new DataResponse([], Http::STATUS_NOT_FOUND);
113
		}
114
115
		$node = array_pop($nodes);
116
		if (!($node instanceof File)) {
0 ignored issues
show
Bug introduced by
The class OCP\Files\File 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...
117
			return new DataResponse([], Http::STATUS_NOT_FOUND);
118
		}
119
120
		return new FileDisplayResponse($node, Http::STATUS_OK, [
121
			'Content-Type' => $node->getMimeType()
122
		]);
123
	}
124
}
125