Completed
Push — playlist-again ( 454ce7...0728a6 )
by Pauli
14:41
created

PlaylistApiController::paramArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 2
eloc 5
nc 2
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\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 View Code Duplication
	public function __construct($appname,
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...
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->getTrackIds() 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 View Code Duplication
	public function update($id) {
153
		try {
154
			$playlist = $this->playlistBusinessLayer->rename($this->params('name'), $id, $this->userId);
155
			return $playlist->toAPI();
156
		} 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...
157
			return new JSONResponse(array('message' => $ex->getMessage()),
158
				Http::STATUS_NOT_FOUND);
159
		}
160
	}
161
162
	/**
163
	 * add tracks to a playlist
164
	 * @param  int $id playlist ID
165
	 *
166
	 * @NoAdminRequired
167
	 * @NoCSRFRequired
168
	 */
169 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...
170
		try {
171
			$playlist = $this->playlistBusinessLayer->addTracks(
172
					$this->paramArray('trackIds'), $id, $this->userId);
173
			return $playlist->toAPI();
174
		} 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...
175
			return new JSONResponse(array('message' => $ex->getMessage()),
176
				Http::STATUS_NOT_FOUND);
177
		}
178
	}
179
180
	/**
181
	 * removes tracks from a playlist
182
	 * @param  int $id playlist ID
183
	 *
184
	 * @NoAdminRequired
185
	 * @NoCSRFRequired
186
	 */
187 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...
188
		try {
189
			$playlist = $this->playlistBusinessLayer->removeTracks(
190
					$this->paramArray('indices'), $id, $this->userId);
191
			return $playlist->toAPI();
192
		} 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...
193
			return new JSONResponse(array('message' => $ex->getMessage()),
194
				Http::STATUS_NOT_FOUND);
195
		}
196
	}
197
198
	private function paramArray($name) {
199
		$array = array();
200
		foreach (explode(',', $this->params($name)) as $item) {
201
			$array[] = (int) $item;
202
		}
203
		return $array;
204
	}
205
}
206