Completed
Push — playlist-again ( 707dae...157e8e )
by Pauli
15:52
created

PlaylistApiController::get()   A

Complexity

Conditions 3
Paths 7

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 17
rs 9.4285
cc 3
eloc 12
nc 7
nop 1
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\TrackBusinessLayer;
27
use \OCA\Music\Db\Playlist;
28
use \OCA\Music\Db\PlaylistMapper;
29
use \OCA\Music\Utility\APISerializer;
30
31
class PlaylistApiController extends Controller {
32
33
	private $playlistMapper;
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
								PlaylistMapper $playlistMapper,
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->playlistMapper = $playlistMapper;
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->playlistMapper->findAll($this->userId);
68
		foreach ($playlists as $list) {
69
			$list->setTrackIds($this->playlistMapper->getTracks($list->getId()));
70
		}
71
		$serializer = new APISerializer();
72
73
		return $serializer->serialize($playlists);
74
	}
75
76
	/**
77
	 * creates a playlist
78
	 *
79
	 * @NoAdminRequired
80
	 * @NoCSRFRequired
81
	 */
82
	public function create() {
83
		$name = $this->params('name');
84
85
		$playlist = new Playlist();
86
		$playlist->setName($name);
87
		$playlist->setUserId($this->userId);
88
89
		$playlist = $this->playlistMapper->insert($playlist);
90
91
		// if trackIds is provided just add them to the playlist
92
		$trackIds = $this->params('trackIds');
93
		if (!empty($trackIds)){
94
			$newTrackIds = array();
95
			foreach (explode(',', $trackIds) as $trackId) {
96
				$newTrackIds[] = (int) $trackId;
97
			}
98
99
			$this->playlistMapper->addTracks($newTrackIds, $playlist->getId());
100
101
			// set trackIds in model
102
			$tracks = $this->playlistMapper->getTracks($playlist->getId());
103
			$playlist->setTrackIds($tracks);
104
105
		}
106
107
		return $playlist->toAPI();
108
	}
109
110
	/**
111
	 * deletes a playlist
112
	 * @param  int $id playlist ID
113
	 *
114
	 * @NoAdminRequired
115
	 * @NoCSRFRequired
116
	 */
117
	public function delete($id) {
118
		$this->playlistMapper->deleteById([$id]);
119
120
		return array();
121
	}
122
123
	/**
124
	 * lists a single playlist
125
	 * @param  int $id playlist ID
126
	 *
127
	 * @NoAdminRequired
128
	 * @NoCSRFRequired
129
	 */
130
	public function get($id) {
131
		try {
132
			$playlist = $this->playlistMapper->find($id, $this->userId);
133
			$playlist->setTrackIds($this->playlistMapper->getTracks($id));
134
135
			$fulltree = filter_var($this->params('fulltree'), FILTER_VALIDATE_BOOLEAN);
136
			if ($fulltree) {
137
				return $this->toFullTree($playlist);
138
			} else {
139
				return $playlist->toAPI();
140
			}
141
142
		} 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...
143
			return new JSONResponse(array('message' => $ex->getMessage()),
144
				Http::STATUS_NOT_FOUND);
145
		}
146
	}
147
148
	private function toFullTree($playlist) {
149
		$songs = [];
150
151
		// Get all track information for all the tracks of the playlist
152
		foreach($playlist->getTrackIds() as $trackId) {
153
			$song = $this->trackBusinessLayer->find($trackId, $this->userId);
154
			$song->setAlbum($this->albumBusinessLayer->find($song->getAlbumId(), $this->userId));
155
			$song->setArtist($this->artistBusinessLayer->find($song->getArtistId(), $this->userId));
156
			$songs[] = $song->toCollection($this->urlGenerator, $this->userFolder);
157
		}
158
159
		return array(
160
			'name' => $playlist->getName(),
161
			'tracks' => $songs,
162
			'id' => $playlist->getId(),
163
		);
164
	}
165
166
	/**
167
	 * update a playlist
168
	 * @param  int $id playlist ID
169
	 *
170
	 * @NoAdminRequired
171
	 * @NoCSRFRequired
172
	 */
173
	public function update($id) {
174
		$name = $this->params('name');
175
176
		try {
177
			$playlist = $this->playlistMapper->find($id, $this->userId);
178
			$playlist->setName($name);
179
			$this->playlistMapper->update($playlist);
180
			$playlist->setTrackIds($this->playlistMapper->getTracks($id));
181
182
			return $playlist->toAPI();
183
		} 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...
184
			return new JSONResponse(array('message' => $ex->getMessage()),
185
				Http::STATUS_NOT_FOUND);
186
		}
187
	}
188
189
	/**
190
	 * add tracks to a playlist
191
	 * @param  int $id playlist ID
192
	 *
193
	 * @NoAdminRequired
194
	 * @NoCSRFRequired
195
	 */
196 View Code Duplication
	public function addTracks($id) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
197
		$newTrackIds = array();
198
		foreach (explode(',', $this->params('trackIds')) as $trackId) {
199
			$newTrackIds[] = (int) $trackId;
200
		}
201
202
		try {
203
			$playlist = $this->playlistMapper->find($id, $this->userId);
204
			$this->playlistMapper->addTracks($newTrackIds, $id);
205
			$playlist->setTrackIds($this->playlistMapper->getTracks($id));
206
207
			return $playlist->toAPI();
208
		} 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...
209
			return new JSONResponse(array('message' => $ex->getMessage()),
210
				Http::STATUS_NOT_FOUND);
211
		}
212
	}
213
214
	/**
215
	 * removes tracks from a playlist
216
	 * @param  int $id playlist ID
217
	 *
218
	 * @NoAdminRequired
219
	 * @NoCSRFRequired
220
	 */
221 View Code Duplication
	public function removeTracks($id) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
222
		$trackIds = array();
223
		foreach (explode(',', $this->params('trackIds')) as $trackId) {
224
			$trackIds[] = (int) $trackId;
225
		}
226
227
		try {
228
			$playlist = $this->playlistMapper->find($id, $this->userId);
229
			$this->playlistMapper->removeTracks($id, $trackIds);
230
			$playlist->setTrackIds($this->playlistMapper->getTracks($id));
231
232
			return $playlist->toAPI();
233
		} 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...
234
			return new JSONResponse(array('message' => $ex->getMessage()),
235
				Http::STATUS_NOT_FOUND);
236
		}
237
	}
238
}
239