Completed
Push — playlist-again ( 930e57...9c67bf )
by Pauli
12:16
created

PlaylistApiController::modifyPlaylist()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 2
eloc 7
nc 3
nop 2
1
<?php
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
 * @copyright Morris Jobke 2013, 2014
11
 */
12
13
namespace OCA\Music\Controller;
14
15
use \OCP\AppFramework\Controller;
16
use \OCP\AppFramework\Http;
17
use \OCP\AppFramework\Http\JSONResponse;
18
use \OCP\AppFramework\Db\DoesNotExistException;
19
20
use \OCP\IRequest;
21
use \OCP\IURLGenerator;
22
use \OCP\Files\Folder;
23
24
use \OCA\Music\BusinessLayer\AlbumBusinessLayer;
25
use \OCA\Music\BusinessLayer\ArtistBusinessLayer;
26
use \OCA\Music\BusinessLayer\PlaylistBusinessLayer;
27
use \OCA\Music\BusinessLayer\TrackBusinessLayer;
28
use \OCA\Music\Db\Playlist;
29
use \OCA\Music\Utility\APISerializer;
30
31
class PlaylistApiController extends Controller {
32
33
	private $playlistBusinessLayer;
34
	private $userId;
35
	private $userFolder;
36
	private $artistBusinessLayer;
37
	private $albumBusinessLayer;
38
	private $trackBusinessLayer;
39
	private $urlGenerator;
40
41
	public function __construct($appname,
42
								IRequest $request,
43
								IURLGenerator $urlGenerator,
44
								PlaylistBusinessLayer $playlistBusinessLayer,
45
								ArtistBusinessLayer $artistBusinessLayer,
46
								AlbumBusinessLayer $albumBusinessLayer,
47
								TrackBusinessLayer $trackBusinessLayer,
48
								Folder $userFolder,
49
								$userId){
50
		parent::__construct($appname, $request);
51
		$this->userId = $userId;
52
		$this->userFolder = $userFolder;
53
		$this->urlGenerator = $urlGenerator;
54
		$this->playlistBusinessLayer = $playlistBusinessLayer;
55
		$this->artistBusinessLayer = $artistBusinessLayer;
56
		$this->albumBusinessLayer = $albumBusinessLayer;
57
		$this->trackBusinessLayer = $trackBusinessLayer;
58
	}
59
60
	/**
61
	 * lists all playlists
62
	 *
63
	 * @NoAdminRequired
64
	 * @NoCSRFRequired
65
	 */
66
	public function getAll() {
67
		$playlists = $this->playlistBusinessLayer->findAll($this->userId);
68
		$serializer = new APISerializer();
69
70
		return $serializer->serialize($playlists);
71
	}
72
73
	/**
74
	 * creates a playlist
75
	 *
76
	 * @NoAdminRequired
77
	 * @NoCSRFRequired
78
	 */
79
	public function create() {
80
		$playlist = $this->playlistBusinessLayer->create($this->params('name'), $this->userId);
81
82
		// add trackIds to the newly created playlist if provided
83
		if (!empty($this->params('trackIds'))){
84
			$playlist = $this->playlistBusinessLayer->addTracks(
85
					$this->paramArray('trackIds'), $playlist->getId(), $this->userId);
86
		}
87
		
88
		return $playlist->toAPI();
89
	}
90
91
	/**
92
	 * deletes a playlist
93
	 * @param  int $id playlist ID
94
	 *
95
	 * @NoAdminRequired
96
	 * @NoCSRFRequired
97
	 */
98
	public function delete($id) {
99
		$this->playlistBusinessLayer->delete($id, $this->userId);
100
		return array();
101
	}
102
103
	/**
104
	 * lists a single playlist
105
	 * @param  int $id playlist ID
106
	 *
107
	 * @NoAdminRequired
108
	 * @NoCSRFRequired
109
	 */
110
	public function get($id) {
111
		try {
112
			$playlist = $this->playlistBusinessLayer->find($id, $this->userId);
113
114
			$fulltree = filter_var($this->params('fulltree'), FILTER_VALIDATE_BOOLEAN);
115
			if ($fulltree) {
116
				return $this->toFullTree($playlist);
117
			} else {
118
				return $playlist->toAPI();
119
			}
120
121
		} catch(DoesNotExistException $ex) {
0 ignored issues
show
Bug introduced by
The class OCP\AppFramework\Db\DoesNotExistException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
122
			return new JSONResponse(array('message' => $ex->getMessage()),
123
				Http::STATUS_NOT_FOUND);
124
		}
125
	}
126
127
	private function toFullTree($playlist) {
128
		$songs = [];
129
130
		// Get all track information for all the tracks of the playlist
131
		foreach($playlist->getTrackIdsAsArray() as $trackId) {
132
			$song = $this->trackBusinessLayer->find($trackId, $this->userId);
133
			$song->setAlbum($this->albumBusinessLayer->find($song->getAlbumId(), $this->userId));
134
			$song->setArtist($this->artistBusinessLayer->find($song->getArtistId(), $this->userId));
135
			$songs[] = $song->toCollection($this->urlGenerator, $this->userFolder);
136
		}
137
138
		return array(
139
			'name' => $playlist->getName(),
140
			'tracks' => $songs,
141
			'id' => $playlist->getId(),
142
		);
143
	}
144
145
	/**
146
	 * update a playlist
147
	 * @param  int $id playlist ID
148
	 *
149
	 * @NoAdminRequired
150
	 * @NoCSRFRequired
151
	 */
152
	public function update($id) {
153
		return $this->modifyPlaylist('rename', [$this->params('name'), $id, $this->userId]);
154
	}
155
156
	/**
157
	 * add tracks to a playlist
158
	 * @param  int $id playlist ID
159
	 *
160
	 * @NoAdminRequired
161
	 * @NoCSRFRequired
162
	 */
163
	public function addTracks($id) {
164
		return $this->modifyPlaylist('addTracks', [$this->paramArray('trackIds'), $id, $this->userId]);
165
	}
166
167
	/**
168
	 * removes tracks from a playlist
169
	 * @param  int $id playlist ID
170
	 *
171
	 * @NoAdminRequired
172
	 * @NoCSRFRequired
173
	 */
174
	public function removeTracks($id) {
175
		return $this->modifyPlaylist('removeTracks', [$this->paramArray('indices'), $id, $this->userId]);
176
	}
177
178
	/**
179
	 * moves single track on playlist to a new position
180
	 * @param  int $id playlist ID
181
	 *
182
	 * @NoAdminRequired
183
	 * @NoCSRFRequired
184
	 */
185
	public function reorder($id) {
186
		return $this->modifyPlaylist('moveTrack',
187
				[$this->params('fromIndex'), $this->params('toIndex'), $id, $this->userId]);
188
	}
189
190
	/**
191
	 * Modify playlist by calling a supplied method from PlaylistBusinessLayer
192
	 * @param string funcName   Name of a function to call from PlaylistBusinessLayer
193
	 * @param array $funcParams Parameters to pass to the function 'funcName'
194
	 * @return \OCP\AppFramework\Http\JSONResponse JSON representation of the modified playlist
195
	 */
196
	private function modifyPlaylist($funcName, $funcParams) {
197
		try {
198
			$playlist = call_user_func_array([$this->playlistBusinessLayer, $funcName], $funcParams);
199
			return $playlist->toAPI();
200
		} catch(DoesNotExistException $ex) {
0 ignored issues
show
Bug introduced by
The class OCP\AppFramework\Db\DoesNotExistException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
201
			return new JSONResponse(array('message' => $ex->getMessage()),
202
					Http::STATUS_NOT_FOUND);
203
		}
204
	}
205
206
	/**
207
	 * Get integer array passed as parameter to the Playlist API
208
	 * @param string $name Name of the parameter
209
	 * @return int[]
210
	 */
211
	private function paramArray($name) {
212
		$array = array();
213
		foreach (explode(',', $this->params($name)) as $item) {
214
			$array[] = (int) $item;
215
		}
216
		return $array;
217
	}
218
}
219