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 |
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; |
19
|
|
|
use \OCP\AppFramework\Db\MultipleObjectsReturnedException; |
20
|
|
|
|
21
|
|
|
|
22
|
|
|
use \OCA\Music\Db\PlaylistMapper; |
23
|
|
|
use \OCA\Music\Db\Playlist; |
24
|
|
|
|
25
|
|
|
class PlaylistBusinessLayer extends BusinessLayer { |
26
|
|
|
|
27
|
|
|
private $logger; |
28
|
|
|
|
29
|
|
|
public function __construct(PlaylistMapper $playlistMapper, Logger $logger) { |
30
|
|
|
parent::__construct($playlistMapper); |
31
|
|
|
$this->logger = $logger; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Return a playlist |
36
|
|
|
* @param int $playlistId the id of the playlist |
37
|
|
|
* @param string $userId the name of the user |
38
|
|
|
* @return Playlist playlist |
39
|
|
|
*/ |
40
|
|
|
public function find($playlistId, $userId) { |
41
|
|
|
return $this->mapper->find($playlistId, $userId); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Returns all playlists |
46
|
|
|
* @param string $userId the name of the user |
47
|
|
|
* @return Playlist[] playlists |
48
|
|
|
*/ |
49
|
|
|
public function findAll($userId) { |
50
|
|
|
return $this->mapper->findAll($userId); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
View Code Duplication |
public function addTracks($trackIds, $playlistId, $userId) { |
|
|
|
|
54
|
|
|
$playlist = $this->find($playlistId, $userId); |
55
|
|
|
$prevTrackIds = $playlist->getTrackIdsAsArray(); |
56
|
|
|
$playlist->setTrackIdsFromArray(array_merge($prevTrackIds, $trackIds)); |
57
|
|
|
$this->mapper->update($playlist); |
58
|
|
|
return $playlist; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
View Code Duplication |
public function removeTracks($trackIds, $playlistId, $userId) { |
|
|
|
|
62
|
|
|
$playlist = $this->find($playlistId, $userId); |
63
|
|
|
$prevTrackIds = $playlist->getTrackIdsAsArray(); |
64
|
|
|
$playlist->setTrackIdsFromArray(array_diff($prevTrackIds, $trackIds)); |
65
|
|
|
$this->mapper->update($playlist); |
66
|
|
|
return $playlist; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function create($name, $userId) { |
70
|
|
|
$playlist = new Playlist(); |
71
|
|
|
$playlist->setName($name); |
72
|
|
|
$playlist->setUserId($userId); |
73
|
|
|
|
74
|
|
|
return $this->mapper->insert($playlist); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function rename($name, $playlistId, $userId) { |
78
|
|
|
$playlist = $this->find($playlistId, $userId); |
79
|
|
|
$playlist->setName($name); |
80
|
|
|
$this->mapper->update($playlist); |
81
|
|
|
return $playlist; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* removes tracks from all available playlists |
86
|
|
|
* @param int[] $trackIds array of all track IDs to remove |
87
|
|
|
*/ |
88
|
|
|
public function removeTracksFromAllLists($trackIds) { |
89
|
|
|
foreach ($trackIds as $trackId) { |
90
|
|
|
$affectedLists = $this->mapper->findListsContainingTrack($trackId); |
91
|
|
|
|
92
|
|
|
foreach ($affectedLists as $playlist) { |
93
|
|
|
$prevTrackIds = $playlist->getTrackIdsAsArray(); |
94
|
|
|
$playlist->setTrackIdsFromArray(array_diff($prevTrackIds, [$trackId])); |
95
|
|
|
$this->mapper->update($playlist); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
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.