Passed
Push — master ( 51b60d...a4260e )
by Pauli
04:35 queued 15s
created

FavoritesController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
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 20
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 2024
11
 */
12
13
namespace OCA\Music\Controller;
14
15
use OCP\AppFramework\Controller;
16
use OCP\AppFramework\Http\JSONResponse;
17
use OCP\IRequest;
18
19
use OCA\Music\AppFramework\BusinessLayer\BusinessLayer;
20
use OCA\Music\BusinessLayer\AlbumBusinessLayer;
21
use OCA\Music\BusinessLayer\ArtistBusinessLayer;
22
use OCA\Music\BusinessLayer\PlaylistBusinessLayer;
23
use OCA\Music\BusinessLayer\PodcastChannelBusinessLayer;
24
use OCA\Music\BusinessLayer\PodcastEpisodeBusinessLayer;
25
use OCA\Music\BusinessLayer\TrackBusinessLayer;
26
27
class FavoritesController extends Controller {
28
	private $albumBusinessLayer;
29
	private $artistBusinessLayer;
30
	private $playlistBusinessLayer;
31
	private $podcastChannelBusinessLayer;
32
	private $podcastEpisodeBusinessLayer;
33
	private $trackBusinessLayer;
34
	private $userId;
35
36
	public function __construct(
37
			string $appname,
38
			IRequest $request,
39
			AlbumBusinessLayer $albumBusinessLayer,
40
			ArtistBusinessLayer $artistBusinessLayer,
41
			PlaylistBusinessLayer $playlistBusinessLayer,
42
			PodcastChannelBusinessLayer $podcastChannelBusinessLayer,
43
			PodcastEpisodeBusinessLayer $podcastEpisodeBusinessLayer,
44
			TrackBusinessLayer $trackBusinessLayer,
45
			string $userId) {
46
47
		parent::__construct($appname, $request);
48
49
		$this->albumBusinessLayer = $albumBusinessLayer;
50
		$this->artistBusinessLayer = $artistBusinessLayer;
51
		$this->playlistBusinessLayer = $playlistBusinessLayer;
52
		$this->podcastChannelBusinessLayer = $podcastChannelBusinessLayer;
53
		$this->podcastEpisodeBusinessLayer = $podcastEpisodeBusinessLayer;
54
		$this->trackBusinessLayer = $trackBusinessLayer;
55
		$this->userId = $userId;
56
	}
57
58
	/**
59
	 * @NoAdminRequired
60
	 * @NoCSRFRequired
61
	 */
62
	public function favorites() {
63
		return new JSONResponse([
64
			'tracks' => $this->trackBusinessLayer->findAllStarredIds($this->userId),
65
			'albums' => $this->albumBusinessLayer->findAllStarredIds($this->userId),
66
			'artists' => $this->artistBusinessLayer->findAllStarredIds($this->userId),
67
			'playlists' => $this->playlistBusinessLayer->findAllStarredIds($this->userId),
68
			'podcast_channels' => $this->podcastChannelBusinessLayer->findAllStarredIds($this->userId),
69
			'podcast_episodes' => $this->podcastEpisodeBusinessLayer->findAllStarredIds($this->userId),
70
		]);
71
	}
72
73
	/**
74
	 * @NoAdminRequired
75
	 * @NoCSRFRequired
76
	 */
77
	public function setFavoriteTrack(int $id, $status) {
78
		return $this->setFavorite($this->trackBusinessLayer, $id, $status);
79
	}
80
81
	/**
82
	 * @NoAdminRequired
83
	 * @NoCSRFRequired
84
	 */
85
	public function setFavoriteAlbum(int $id, $status) {
86
		return $this->setFavorite($this->albumBusinessLayer, $id, $status);
87
	}
88
89
	/**
90
	 * @NoAdminRequired
91
	 * @NoCSRFRequired
92
	 */
93
	public function setFavoriteArtist(int $id, $status) {
94
		return $this->setFavorite($this->artistBusinessLayer, $id, $status);
95
	}
96
97
	/**
98
	 * @NoAdminRequired
99
	 * @NoCSRFRequired
100
	 */
101
	public function setFavoritePlaylist(int $id, $status) {
102
		return $this->setFavorite($this->playlistBusinessLayer, $id, $status);
103
	}
104
105
	/**
106
	 * @NoAdminRequired
107
	 * @NoCSRFRequired
108
	 */
109
	public function setFavoriteChannel(int $id, $status) {
110
		return $this->setFavorite($this->podcastChannelBusinessLayer, $id, $status);
111
	}
112
113
	/**
114
	 * @NoAdminRequired
115
	 * @NoCSRFRequired
116
	 */
117
	public function setFavoriteEpisode(int $id, $status) {
118
		return $this->setFavorite($this->podcastEpisodeBusinessLayer, $id, $status);
119
	}
120
121
	private function setFavorite(BusinessLayer $businessLayer, int $id, $status) {
122
		$status = \filter_var($status, FILTER_VALIDATE_BOOLEAN);
123
		if ($status) {
124
			$businessLayer->setStarred([$id], $this->userId);
125
		} else {
126
			$businessLayer->unsetStarred([$id], $this->userId);
127
		}
128
		return new JSONResponse(['favorite' => ($status != 0)]);
129
	}
130
}