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

Playlist   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 0
dl 0
loc 36
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getTrackIdsAsArray() 0 9 3
A setTrackIdsFromArray() 0 4 1
A toAPI() 0 7 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 2014
11
 */
12
13
namespace OCA\Music\Db;
14
15
use \OCP\AppFramework\Db\Entity;
16
17
/**
18
 * @method string getName()
19
 * @method setName(string $name)
20
 * @method string getTrackIds()
21
 * @method setTrackIds(string $trackIds)
22
 * @method string getUserId()
23
 * @method setUserId(string $userId)
24
 */
25
class Playlist extends Entity {
26
27
	public $name;
28
	public $userId;
29
	public $trackIds;
30
31
	/**
32
	 * @return int[]
33
	 */
34
	public function getTrackIdsAsArray() {
35
		if (!$this->trackIds || strlen($this->trackIds) < 3) {
36
			// the list is empty if there is nothing between the leading and trailing '|'
37
			return [];
38
		} else {
39
			$encoded = substr($this->trackIds, 1, -1); // omit leading and trailing '|'
40
			return array_map('intval', explode('|', $encoded));
41
		}
42
	}
43
44
	/**
45
	 * @param int[] $trackIds
46
	 */
47
	public function setTrackIdsFromArray($trackIds) {
48
		// encode to format like "|123|45|667|"
49
		$this->setTrackIds('|' . implode('|', $trackIds) . '|');
50
	}
51
52
	public function toAPI() {
53
		return array(
54
			'name' => $this->getName(),
55
			'trackIds' => $this->getTrackIdsAsArray(),
56
			'id' => $this->getId(),
57
		);
58
	}
59
60
}
61