FilesController::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 8
dl 0
loc 18
rs 10
c 0
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
/**
3
 * Nextcloud - Gallery
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 *
8
 * @author Olivier Paroz <[email protected]>
9
 *
10
 * @copyright Olivier Paroz 2017
11
 */
12
13
namespace OCA\Gallery\Controller;
14
15
use OCP\IRequest;
0 ignored issues
show
Bug introduced by
The type OCP\IRequest was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use OCP\IURLGenerator;
0 ignored issues
show
Bug introduced by
The type OCP\IURLGenerator was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use OCP\ILogger;
0 ignored issues
show
Bug introduced by
The type OCP\ILogger was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
19
use OCP\AppFramework\Controller;
0 ignored issues
show
Bug introduced by
The type OCP\AppFramework\Controller was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
20
use OCP\AppFramework\Http;
0 ignored issues
show
Bug introduced by
The type OCP\AppFramework\Http was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
21
use OCP\AppFramework\Http\RedirectResponse;
0 ignored issues
show
Bug introduced by
The type OCP\AppFramework\Http\RedirectResponse was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
22
23
use OCA\Gallery\Http\ImageResponse;
24
use OCA\Gallery\Service\SearchFolderService;
25
use OCA\Gallery\Service\ConfigService;
26
use OCA\Gallery\Service\SearchMediaService;
27
use OCA\Gallery\Service\DownloadService;
28
use OCA\Gallery\Service\ServiceException;
29
30
/**
31
 * Class FilesController
32
 *
33
 * @package OCA\Gallery\Controller
34
 */
35
class FilesController extends Controller {
36
37
	use Files;
38
	use HttpError;
39
40
	/** @var IURLGenerator */
41
	private $urlGenerator;
42
43
	/**
44
	 * Constructor
45
	 *
46
	 * @param string $appName
47
	 * @param IRequest $request
48
	 * @param IURLGenerator $urlGenerator
49
	 * @param SearchFolderService $searchFolderService
50
	 * @param ConfigService $configService
51
	 * @param SearchMediaService $searchMediaService
52
	 * @param DownloadService $downloadService
53
	 * @param ILogger $logger
54
	 */
55
	public function __construct(
56
		$appName,
57
		IRequest $request,
58
		IURLGenerator $urlGenerator,
59
		SearchFolderService $searchFolderService,
60
		ConfigService $configService,
61
		SearchMediaService $searchMediaService,
62
		DownloadService $downloadService,
63
		ILogger $logger
64
	) {
65
		parent::__construct($appName, $request);
66
67
		$this->urlGenerator = $urlGenerator;
68
		$this->searchFolderService = $searchFolderService;
69
		$this->configService = $configService;
70
		$this->searchMediaService = $searchMediaService;
71
		$this->downloadService = $downloadService;
72
		$this->logger = $logger;
73
	}
74
75
	/**
76
	 * @NoAdminRequired
77
	 *
78
	 * Returns a list of all media files available to the authenticated user
79
	 *
80
	 *    * Authentication can be via a login/password or a token/(password)
81
	 *    * For private galleries, it returns all media files, with the full path from the root
82
	 *     folder For public galleries, the path starts from the folder the link gives access to
83
	 *     (virtual root)
84
	 *    * An exception is only caught in case something really wrong happens. As we don't test
85
	 *     files before including them in the list, we may return some bad apples
86
	 *
87
	 * @param string $location a path representing the current album in the app
88
	 * @param string $features the list of supported features
89
	 * @param string $etag the last known etag in the client
90
	 * @param string $mediatypes the list of supported media types
91
	 *
92
	 * @return array <string,array<string,string|int>>|Http\JSONResponse
93
	 */
94
	public function getList($location, $features, $etag, $mediatypes) {
95
		$featuresArray = explode(';', $features);
96
		$mediaTypesArray = explode(';', $mediatypes);
97
		try {
98
			return $this->getFilesAndAlbums($location, $featuresArray, $etag, $mediaTypesArray);
99
		} catch (\Exception $exception) {
100
			return $this->jsonError($exception, $this->request, $this->logger);
101
		}
102
	}
103
104
	/**
105
	 * @NoAdminRequired
106
	 *
107
	 * Sends the file matching the fileId
108
	 *
109
	 * @param int $fileId the ID of the file we want to download
110
	 * @param string|null $filename
111
	 *
112
	 * @return ImageResponse
113
	 */
114
	public function download($fileId, $filename = null) {
115
		try {
116
			$download = $this->getDownload($fileId, $filename);
117
		} catch (ServiceException $exception) {
118
			$code = $this->getHttpStatusCode($exception);
119
			$url = $this->urlGenerator->linkToRoute(
120
				$this->appName . '.page.error_page', ['code' => $code]
121
			);
122
123
			$response = new RedirectResponse($url);
124
			$response->addCookie('galleryErrorMessage', $exception->getMessage());
125
126
			return $response;
127
		}
128
129
		// That's the only exception out of all the image media types we serve
130
		if ($download['mimetype'] === 'image/svg+xml') {
131
			$download['mimetype'] = 'text/plain';
132
		}
133
134
		return new ImageResponse($download);
135
	}
136
137
}
138