Completed
Push — playlist-again ( b7d5ab...454ce7 )
by Pauli
13:03
created

PlaylistBusinessLayer   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 75
Duplicated Lines 18.67 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 2
dl 14
loc 75
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A find() 0 3 1
A findAll() 0 3 1
A addTracks() 7 7 1
A removeTracks() 7 7 1
A create() 0 7 1
A rename() 0 6 1
A removeTracksFromAllLists() 0 11 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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) {
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...
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) {
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...
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