Passed
Push — master ( 73fe5d...0c6689 )
by Pauli
02:23
created

ShivaApiController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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

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 Morris Jobke <[email protected]>
10
 * @author Pauli Järvinen <[email protected]>
11
 * @copyright Morris Jobke 2013, 2014
12
 * @copyright Pauli Järvinen 2017 - 2021
13
 */
14
15
namespace OCA\Music\Controller;
16
17
use OCP\AppFramework\Controller;
18
use OCP\AppFramework\Http;
19
use OCP\AppFramework\Http\JSONResponse;
20
use OCP\IL10N;
21
use OCP\IRequest;
22
use OCP\IURLGenerator;
23
24
use OCA\Music\AppFramework\BusinessLayer\BusinessLayerException;
25
use OCA\Music\AppFramework\Core\Logger;
26
use OCA\Music\BusinessLayer\AlbumBusinessLayer;
27
use OCA\Music\BusinessLayer\ArtistBusinessLayer;
28
use OCA\Music\BusinessLayer\TrackBusinessLayer;
29
use OCA\Music\Db\Album;
30
use OCA\Music\Db\Artist;
31
use OCA\Music\Db\SortBy;
32
use OCA\Music\Db\Track;
33
use OCA\Music\Http\ErrorResponse;
34
35
class ShivaApiController extends Controller {
36
37
	/** @var IL10N */
38
	private $l10n;
39
	/** @var TrackBusinessLayer */
40
	private $trackBusinessLayer;
41
	/** @var ArtistBusinessLayer */
42
	private $artistBusinessLayer;
43
	/** @var AlbumBusinessLayer */
44
	private $albumBusinessLayer;
45
	/** @var string */
46
	private $userId;
47
	/** @var IURLGenerator */
48
	private $urlGenerator;
49
	/** @var Logger */
50
	private $logger;
51
52
	public function __construct(string $appname,
53
								IRequest $request,
54
								IURLGenerator $urlGenerator,
55
								TrackBusinessLayer $trackbusinesslayer,
56
								ArtistBusinessLayer $artistbusinesslayer,
57
								AlbumBusinessLayer $albumbusinesslayer,
58
								?string $userId, // null if this gets called after the user has logged out
59
								IL10N $l10n,
60
								Logger $logger) {
61
		parent::__construct($appname, $request);
62
		$this->l10n = $l10n;
63
		$this->trackBusinessLayer = $trackbusinesslayer;
64
		$this->artistBusinessLayer = $artistbusinesslayer;
65
		$this->albumBusinessLayer = $albumbusinesslayer;
66
		$this->userId = $userId;
67
		$this->urlGenerator = $urlGenerator;
68
		$this->logger = $logger;
69
	}
70
71
	private static function shivaPageToLimits(?int $pageSize, ?int $page) : array {
72
		if (\is_int($page) && \is_int($pageSize) && $page > 0 && $pageSize > 0) {
73
			$limit = $pageSize;
74
			$offset = ($page - 1) * $pageSize;
75
		} else {
76
			$limit = $offset = null;
77
		}
78
		return [$limit, $offset];
79
	}
80
81
	/**
82
	 * @NoAdminRequired
83
	 * @NoCSRFRequired
84
	 */
85
	public function artists($fulltree, $albums, ?int $page_size=null, ?int $page=null) {
86
		$fulltree = \filter_var($fulltree, FILTER_VALIDATE_BOOLEAN);
87
		$includeAlbums = \filter_var($albums, FILTER_VALIDATE_BOOLEAN);
88
		list($limit, $offset) = self::shivaPageToLimits($page_size, $page);
89
90
		/** @var Artist[] $artists */
91
		$artists = $this->artistBusinessLayer->findAll($this->userId, SortBy::Name, $limit, $offset);
92
93
		$artists = \array_map(function ($a) use ($fulltree, $includeAlbums) {
94
			return $this->artistToApi($a, $includeAlbums || $fulltree, $fulltree);
95
		}, $artists);
96
97
		return new JSONResponse($artists);
98
	}
99
100
	/**
101
	 * @NoAdminRequired
102
	 * @NoCSRFRequired
103
	 */
104
	public function artist(int $artistId, $fulltree) {
105
		$fulltree = \filter_var($fulltree, FILTER_VALIDATE_BOOLEAN);
106
		try {
107
			/** @var Artist $artist */
108
			$artist = $this->artistBusinessLayer->find($artistId, $this->userId);
109
			$artist = $this->artistToApi($artist, $fulltree, $fulltree);
110
			return new JSONResponse($artist);
111
		} catch (BusinessLayerException $e) {
112
			return new ErrorResponse(Http::STATUS_NOT_FOUND);
113
		}
114
	}
115
116
	/**
117
	 * Return given artist in Shia API format
118
	 * @param Artist $artist
119
	 * @param boolean $includeAlbums
120
	 * @param boolean $includeTracks (ignored if $includeAlbums==false)
121
	 * @return array
122
	 */
123
	private function artistToApi(Artist $artist, bool $includeAlbums, bool $includeTracks) : array {
124
		$artistInApi = $artist->toAPI($this->urlGenerator, $this->l10n);
125
		if ($includeAlbums) {
126
			$artistId = $artist->getId();
127
			$albums = $this->albumBusinessLayer->findAllByArtist($artistId, $this->userId);
128
129
			$artistInApi['albums'] = \array_map(function ($a) use ($includeTracks) {
130
				return $this->albumToApi($a, $includeTracks, false);
131
			}, $albums);
132
		}
133
		return $artistInApi;
134
	}
135
136
	/**
137
	 * @NoAdminRequired
138
	 * @NoCSRFRequired
139
	 */
140
	public function albums(?int $artist=null, $fulltree=null, ?int $page_size=null, ?int $page=null) {
141
		$fulltree = \filter_var($fulltree, FILTER_VALIDATE_BOOLEAN);
142
		list($limit, $offset) = self::shivaPageToLimits($page_size, $page);
143
144
		if ($artist !== null) {
145
			$albums = $this->albumBusinessLayer->findAllByArtist($artist, $this->userId, $limit, $offset);
146
		} else {
147
			$albums = $this->albumBusinessLayer->findAll($this->userId, SortBy::Name, $limit, $offset);
148
		}
149
150
		$albums = \array_map(function ($a) use ($fulltree) {
151
			return $this->albumToApi($a, $fulltree, $fulltree);
152
		}, $albums);
153
154
		return new JSONResponse($albums);
155
	}
156
157
	/**
158
	 * @NoAdminRequired
159
	 * @NoCSRFRequired
160
	 */
161
	public function album(int $albumId, $fulltree) {
162
		$fulltree = \filter_var($fulltree, FILTER_VALIDATE_BOOLEAN);
163
		try {
164
			$album = $this->albumBusinessLayer->find($albumId, $this->userId);
165
			$album = $this->albumToApi($album, $fulltree, $fulltree);
166
			return new JSONResponse($album);
167
		} catch (BusinessLayerException $e) {
168
			return new ErrorResponse(Http::STATUS_NOT_FOUND);
169
		}
170
	}
171
172
	/**
173
	 * Return given album in the Shiva API format
174
	 */
175
	private function albumToApi(Album $album, bool $includeTracks, bool $includeArtists) : array {
176
		$albumInApi = $album->toAPI($this->urlGenerator, $this->l10n);
177
178
		if ($includeTracks) {
179
			$albumId = $album->getId();
180
			$tracks = $this->trackBusinessLayer->findAllByAlbum($albumId, $this->userId);
181
			$albumInApi['tracks'] = \array_map(function ($t) {
182
				return $t->toAPI($this->urlGenerator);
183
			}, $tracks);
184
		}
185
186
		if ($includeArtists) {
187
			$artistIds = $album->getArtistIds();
188
			$artists = $this->artistBusinessLayer->findById($artistIds, $this->userId);
189
			$albumInApi['artists'] = \array_map(function ($a) {
190
				return $a->toAPI($this->urlGenerator, $this->l10n);
191
			}, $artists);
192
		}
193
194
		return $albumInApi;
195
	}
196
197
	/**
198
	 * @NoAdminRequired
199
	 * @NoCSRFRequired
200
	 */
201
	public function tracks(?int $artist=null, ?int $album=null, $fulltree=null, ?int $page_size=null, ?int $page=null) {
202
		$fulltree = \filter_var($fulltree, FILTER_VALIDATE_BOOLEAN);
203
		list($limit, $offset) = self::shivaPageToLimits($page_size, $page);
204
205
		if ($album !== null) {
206
			$tracks = $this->trackBusinessLayer->findAllByAlbum($album, $this->userId, $artist, $limit, $offset);
207
		} elseif ($artist !== null) {
208
			$tracks = $this->trackBusinessLayer->findAllByArtist($artist, $this->userId, $limit, $offset);
209
		} else {
210
			$tracks = $this->trackBusinessLayer->findAll($this->userId, SortBy::Name, $limit, $offset);
211
		}
212
		foreach ($tracks as &$track) {
213
			$artistId = $track->getArtistId();
214
			$albumId = $track->getAlbumId();
215
			$track = $track->toAPI($this->urlGenerator);
216
			if ($fulltree) {
217
				$artist = $this->artistBusinessLayer->find($artistId, $this->userId);
218
				$track['artist'] = $artist->toAPI($this->urlGenerator, $this->l10n);
219
				$album = $this->albumBusinessLayer->find($albumId, $this->userId);
220
				$track['album'] = $album->toAPI($this->urlGenerator, $this->l10n);
221
			}
222
		}
223
		return new JSONResponse($tracks);
224
	}
225
226
	/**
227
	 * @NoAdminRequired
228
	 * @NoCSRFRequired
229
	 */
230
	public function track(int $trackId) {
231
		try {
232
			/** @var Track $track */
233
			$track = $this->trackBusinessLayer->find($trackId, $this->userId);
234
			return new JSONResponse($track->toAPI($this->urlGenerator));
235
		} catch (BusinessLayerException $e) {
236
			return new ErrorResponse(Http::STATUS_NOT_FOUND);
237
		}
238
	}
239
240
}
241