Passed
Push — feature/329_Subsonic_API ( d9d298 )
by Pauli
10:35
created

PlaylistBusinessLayer   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 50
dl 0
loc 97
rs 10
c 1
b 0
f 0
wmc 13

8 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 6 1
A addTracks() 0 6 1
A rename() 0 5 1
A getPlaylistTracks() 0 20 4
A __construct() 0 7 1
A removeTracks() 0 7 1
A moveTrack() 0 8 1
A removeTracksFromAllLists() 0 8 3
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 Pauli Järvinen <[email protected]>
10
 * @copyright Pauli Järvinen 2016 - 2019
11
 */
12
13
namespace OCA\Music\BusinessLayer;
14
15
use \OCA\Music\AppFramework\BusinessLayer\BusinessLayer;
16
use \OCA\Music\AppFramework\BusinessLayer\BusinessLayerException;
17
use \OCA\Music\AppFramework\Core\Logger;
18
use \OCP\AppFramework\Db\DoesNotExistException;
0 ignored issues
show
Bug introduced by
The type OCP\AppFramework\Db\DoesNotExistException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
19
use \OCP\AppFramework\Db\MultipleObjectsReturnedException;
0 ignored issues
show
Bug introduced by
The type OCP\AppFramework\Db\Mult...bjectsReturnedException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
20
21
use \OCA\Music\Db\PlaylistMapper;
22
use \OCA\Music\Db\Playlist;
23
24
class PlaylistBusinessLayer extends BusinessLayer {
25
	private $logger;
26
	private $trackBusinessLayer;
27
28
	public function __construct(
29
			PlaylistMapper $playlistMapper,
30
			TrackBusinessLayer $trackBusinessLayer,
31
			Logger $logger) {
32
		parent::__construct($playlistMapper);
33
		$this->logger = $logger;
34
		$this->trackBusinessLayer = $trackBusinessLayer;
35
	}
36
37
	public function addTracks($trackIds, $playlistId, $userId) {
38
		$playlist = $this->find($playlistId, $userId);
39
		$prevTrackIds = $playlist->getTrackIdsAsArray();
40
		$playlist->setTrackIdsFromArray(\array_merge($prevTrackIds, $trackIds));
41
		$this->mapper->update($playlist);
42
		return $playlist;
43
	}
44
45
	public function removeTracks($trackIndices, $playlistId, $userId) {
46
		$playlist = $this->find($playlistId, $userId);
47
		$trackIds = $playlist->getTrackIdsAsArray();
48
		$trackIds = \array_diff_key($trackIds, \array_flip($trackIndices));
49
		$playlist->setTrackIdsFromArray($trackIds);
50
		$this->mapper->update($playlist);
51
		return $playlist;
52
	}
53
54
	public function moveTrack($fromIndex, $toIndex, $playlistId, $userId) {
55
		$playlist = $this->find($playlistId, $userId);
56
		$trackIds = $playlist->getTrackIdsAsArray();
57
		$movedTrack = \array_splice($trackIds, $fromIndex, 1);
58
		\array_splice($trackIds, $toIndex, 0, $movedTrack);
59
		$playlist->setTrackIdsFromArray($trackIds);
60
		$this->mapper->update($playlist);
61
		return $playlist;
62
	}
63
64
	public function create($name, $userId) {
65
		$playlist = new Playlist();
66
		$playlist->setName($name);
67
		$playlist->setUserId($userId);
68
69
		return $this->mapper->insert($playlist);
70
	}
71
72
	public function rename($name, $playlistId, $userId) {
73
		$playlist = $this->find($playlistId, $userId);
74
		$playlist->setName($name);
75
		$this->mapper->update($playlist);
76
		return $playlist;
77
	}
78
79
	/**
80
	 * removes tracks from all available playlists
81
	 * @param int[] $trackIds array of all track IDs to remove
82
	 */
83
	public function removeTracksFromAllLists($trackIds) {
84
		foreach ($trackIds as $trackId) {
85
			$affectedLists = $this->mapper->findListsContainingTrack($trackId);
86
87
			foreach ($affectedLists as $playlist) {
88
				$prevTrackIds = $playlist->getTrackIdsAsArray();
89
				$playlist->setTrackIdsFromArray(\array_diff($prevTrackIds, [$trackId]));
90
				$this->mapper->update($playlist);
91
			}
92
		}
93
	}
94
95
	/**
96
	 * get list of Track objects belonging to a given playlist
97
	 * @param int $playlistId
98
	 * @param string $userId
99
	 * @return Track[]
100
	 */
101
	public function getPlaylistTracks($playlistId, $userId) {
102
		$playlist = $this->find($playlistId, $userId);
103
		$trackIds = $playlist->getTrackIdsAsArray();
104
		$tracks = empty($trackIds) ? [] : $this->trackBusinessLayer->findById($trackIds, $this->userId);
0 ignored issues
show
Bug Best Practice introduced by
The property userId does not exist on OCA\Music\BusinessLayer\PlaylistBusinessLayer. Did you maybe forget to declare it?
Loading history...
105
106
		// The $tracks contains the songs in unspecified order and with no duplicates.
107
		// Build a new array where the tracks are in the same order as in $trackIds.
108
		// First create an index as a middle-step.
109
		$tracksById = [];
110
		foreach ($tracks as $track) {
111
			$tracksById[$track->getId()] = $track;
112
		}
113
		$playlistTracks = [];
114
		foreach ($trackIds as $trackId) {
115
			$track = $tracksById[$trackId];
116
			$track->setNumber(\count($playlistTracks) + 1); // override track # with the ordinal on the list
117
			$playlistTracks[] = $track;
118
		}
119
120
		return $playlistTracks;
121
	}
122
}
123