Completed
Push — stable9 ( 34bd66...defdb6 )
by Olivier
11:26 queued 07:53
created

PreviewController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 18

Duplication

Lines 21
Ratio 100 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 0
Metric Value
dl 21
loc 21
ccs 10
cts 10
cp 1
rs 9.3142
c 0
b 0
f 0
cc 1
eloc 18
nc 1
nop 9
crap 1

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
 * ownCloud - galleryplus
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 2016
11
 */
12
13
namespace OCA\GalleryPlus\Controller;
14
15
use OCP\IRequest;
16
use OCP\IURLGenerator;
17
use OCP\ILogger;
18
use OCP\Files\File;
19
20
use OCP\AppFramework\Controller;
21
use OCP\AppFramework\Http;
22
use OCP\AppFramework\Http\JSONResponse;
23
24
use OCA\GalleryPlus\Http\ImageResponse;
25
use OCA\GalleryPlus\Service\ConfigService;
26
use OCA\GalleryPlus\Service\ThumbnailService;
27
use OCA\GalleryPlus\Service\PreviewService;
28
use OCA\GalleryPlus\Service\DownloadService;
29
use OCA\GalleryPlus\Utility\EventSource;
30
31
/**
32
 * Class PreviewController
33
 *
34
 * @package OCA\GalleryPlus\Controller
35
 */
36
class PreviewController extends Controller {
37
38
	use Preview;
39
40
	/** @var EventSource */
41
	private $eventSource;
42
43
	/**
44
	 * Constructor
45
	 *
46
	 * @param string $appName
47
	 * @param IRequest $request
48
	 * @param IURLGenerator $urlGenerator
49
	 * @param ConfigService $configService
50
	 * @param ThumbnailService $thumbnailService
51
	 * @param PreviewService $previewService
52
	 * @param DownloadService $downloadService
53
	 * @param EventSource $eventSource
54
	 * @param ILogger $logger
55
	 */
56 30 View Code Duplication
	public function __construct(
57
		$appName,
58
		IRequest $request,
59
		IURLGenerator $urlGenerator,
60
		ConfigService $configService,
61
		ThumbnailService $thumbnailService,
62
		PreviewService $previewService,
63
		DownloadService $downloadService,
64
		EventSource $eventSource,
65
		ILogger $logger
66
	) {
67 30
		parent::__construct($appName, $request);
68
69 30
		$this->urlGenerator = $urlGenerator;
70 30
		$this->configService = $configService;
71 30
		$this->thumbnailService = $thumbnailService;
72 30
		$this->previewService = $previewService;
73 30
		$this->downloadService = $downloadService;
74 30
		$this->eventSource = $eventSource;
75 30
		$this->logger = $logger;
76 30
	}
77
78
	/**
79
	 * @NoAdminRequired
80
	 *
81
	 * Generates thumbnails
82
	 *
83
	 * Uses EventSource to send thumbnails back as soon as they're created
84
	 *
85
	 * FIXME: @LukasReschke says: The exit is required here because
86
	 * otherwise the AppFramework is trying to add headers as well after
87
	 * dispatching the request which results in a "Cannot modify header
88
	 * information" notice.
89
	 *
90
	 * WARNING: Returning a JSON response does not get rid of the problem
91
	 *
92
	 * @param string $ids the ID of the files of which we need thumbnail previews of
93
	 * @param bool $square
94
	 * @param double $scale
95
	 *
96
	 * @return array<string,array|string|null>
97
	 */
98 3
	public function getThumbnails($ids, $square, $scale) {
99 3
		$idsArray = explode(';', $ids);
100
101 3
		foreach ($idsArray as $id) {
102
			// Casting to integer here instead of using array_map to extract IDs from the URL
103 3
			list($thumbnail, $status) = $this->getThumbnail((int)$id, $square, $scale);
104 3
			$thumbnail['fileid'] = $id;
105 3
			$thumbnail['status'] = $status;
106
107 3
			$this->eventSource->send('preview', $thumbnail);
108
		}
109 3
		$this->eventSource->close();
110
111 3
		$this->exitController();
112
		// @codeCoverageIgnoreStart
113
	} // @codeCoverageIgnoreEnd
114
115
	/**
116
	 * @NoAdminRequired
117
	 *
118
	 * Sends either a large preview of the requested file or the original file itself
119
	 *
120
	 * @param int $fileId the ID of the file of which we need a large preview of
121
	 * @param int $width
122
	 * @param int $height
123
	 *
124
	 * @return ImageResponse|Http\JSONResponse
125
	 */
126 7
	public function getPreview($fileId, $width, $height) {
127
		/** @type File $file */
128 7
		list($file, $preview, $status) = $this->getData($fileId, $width, $height);
129
130 7
		if (!$preview) {
131 4
			return new JSONResponse(
132
				[
133 4
					'message' => "I'm truly sorry, but we were unable to generate a preview for this file",
134
					'success' => false
135
				], $status
136
			);
137
		}
138 3
		$preview['name'] = $file->getName();
139
140 3
		return new ImageResponse($preview, $status);
141
	}
142
143
}
144