Passed
Pull Request — master (#1078)
by Pauli
20:11 queued 15:09
created

AmpacheImageController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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

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 declare(strict_types=1);
2
3
/**
4
 * ownCloud - Music app
5
 *
6
 * This file is licensed under the Affero General Public License version 3 or
7
 * later. See the COPYING file.
8
 *
9
 * @author Pauli Järvinen <[email protected]>
10
 * @copyright Pauli Järvinen 2023
11
 */
12
13
namespace OCA\Music\Controller;
14
15
use OCA\Music\BusinessLayer\AlbumBusinessLayer;
16
use OCA\Music\BusinessLayer\ArtistBusinessLayer;
17
use OCA\Music\BusinessLayer\PlaylistBusinessLayer;
18
use OCA\Music\AppFramework\BusinessLayer\BusinessLayer;
19
use OCA\Music\AppFramework\BusinessLayer\BusinessLayerException;
20
use OCA\Music\AppFramework\Core\Logger;
21
use OCA\Music\Http\ErrorResponse;
22
use OCA\Music\Http\FileResponse;
23
use OCA\Music\Utility\AmpacheImageService;
24
use OCA\Music\Utility\CoverHelper;
25
use OCA\Music\Utility\LibrarySettings;
26
use OCA\Music\Utility\PlaceholderImage;
27
use OCP\AppFramework\Controller;
28
use OCP\AppFramework\Http;
29
use OCP\AppFramework\Http\Response;
30
use OCP\IRequest;
31
32
class AmpacheImageController extends Controller {
33
	private $service;
34
	private $coverHelper;
35
	private $librarySettings;
36
	private $albumBusinessLayer;
37
	private $artistBusinessLayer;
38
	private $playlistBusinessLayer;
39
	private $logger;
40
41
	public function __construct(
42
			string $appname,
43
			IRequest $request,
44
			AmpacheImageService $service,
45
			CoverHelper $coverHelper,
46
			LibrarySettings $librarySettings,
47
			AlbumBusinessLayer $albumBusinessLayer,
48
			ArtistBusinessLayer $artistBusinessLayer,
49
			PlaylistBusinessLayer $playlistBusinessLayer,
50
			Logger $logger) {
51
		parent::__construct($appname, $request);
52
		$this->service = $service;
53
		$this->coverHelper = $coverHelper;
54
		$this->librarySettings = $librarySettings;
55
		$this->albumBusinessLayer = $albumBusinessLayer;
56
		$this->artistBusinessLayer = $artistBusinessLayer;
57
		$this->playlistBusinessLayer = $playlistBusinessLayer;
58
		$this->logger = $logger;
59
	}
60
61
	/**
62
	 * @NoAdminRequired
63
	 * @PublicPage
64
	 * @NoCSRFRequired
65
	 * @NoSameSiteCookieRequired
66
	 */
67
	public function image(?string $object_type, ?string $object_id, ?string $token) : Response {
68
		if ($token === null) {
69
			// Workaround for Ample client which uses this kind of call to get the placeholder graphips
70
			return new FileResponse(PlaceholderImage::generateForResponse('?', $object_type, 200));
0 ignored issues
show
Bug introduced by
It seems like $object_type can also be of type null; however, parameter $seed of OCA\Music\Utility\Placeh...::generateForResponse() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

70
			return new FileResponse(PlaceholderImage::generateForResponse('?', /** @scrutinizer ignore-type */ $object_type, 200));
Loading history...
71
		}
72
73
		$userId = $this->service->getUserForToken($token, $object_type, (int)$object_id);
0 ignored issues
show
Bug introduced by
It seems like $object_type can also be of type null; however, parameter $entityType of OCA\Music\Utility\Ampach...vice::getUserForToken() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

73
		$userId = $this->service->getUserForToken($token, /** @scrutinizer ignore-type */ $object_type, (int)$object_id);
Loading history...
74
		if ($userId === null) {
75
			return new ErrorResponse(Http::STATUS_FORBIDDEN, 'invalid token');
76
		}
77
78
		$businessLayer = $this->getBusinessLayer($object_type);
0 ignored issues
show
Bug introduced by
It seems like $object_type can also be of type null; however, parameter $object_type of OCA\Music\Controller\Amp...ler::getBusinessLayer() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

78
		$businessLayer = $this->getBusinessLayer(/** @scrutinizer ignore-type */ $object_type);
Loading history...
79
		if ($businessLayer === null) {
80
			return new ErrorResponse(Http::STATUS_NOT_FOUND, "invalid object_type $object_type");	
81
		}
82
83
		try {
84
			$entity = $businessLayer->find((int)$object_id, $userId);
85
		} catch (BusinessLayerException $e) {
86
			return new ErrorResponse(Http::STATUS_NOT_FOUND, "$object_type $object_id not found");	
87
		}
88
89
		$coverImage = $this->coverHelper->getCover($entity, $userId, $this->librarySettings->getFolder($userId));
90
		if ($coverImage === null) {
91
			return new ErrorResponse(Http::STATUS_NOT_FOUND, "$object_type $object_id has no cover image");		
92
		}
93
94
		return new FileResponse($coverImage);
95
	}
96
97
	private function getBusinessLayer(string $object_type) : ?BusinessLayer {
98
		switch ($object_type) {
99
			case 'album':		return $this->albumBusinessLayer;
100
			case 'artist':		return $this->artistBusinessLayer;
101
			case 'playlist':	return $this->playlistBusinessLayer;
102
			default:			return null;
103
		}
104
	}
105
}
106