Passed
Push — master ( f9c9de...599a1b )
by Pauli
01:59
created

Playlist::setTrackIdsFromArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
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
 * @author Pauli Järvinen <[email protected]>
11
 * @copyright Morris Jobke 2014
12
 * @copyright Pauli Järvinen 2017 - 2021
13
 */
14
15
namespace OCA\Music\Db;
16
17
use \OCA\Music\Utility\Util;
18
use \OCP\AppFramework\Db\Entity;
19
20
/**
21
 * @method string getName()
22
 * @method setName(string $name)
23
 * @method string getTrackIds()
24
 * @method setTrackIds(string $trackIds)
25
 * @method string getUserId()
26
 * @method setUserId(string $userId)
27
 * @method string getCreated()
28
 * @method setCreated(string $timestamp)
29
 * @method string getUpdated()
30
 * @method setUpdated(string $timestamp)
31
 * @method string getComment()
32
 * @method setComment(string $comment)
33
 * @method ?int getDuration()
34
 * @method setDuration(?int $duration)
35
 */
36
class Playlist extends Entity {
37
	public $name;
38
	public $userId;
39
	public $trackIds;
40
	public $created;
41
	public $updated;
42
	public $comment;
43
44
	// injected separately when needed
45
	public $duration;
46
47
	/**
48
	 * @return integer
49
	 */
50
	public function getTrackCount() {
51
		return \count($this->getTrackIdsAsArray());
52
	}
53
54
	/**
55
	 * @return int[]
56
	 */
57
	public function getTrackIdsAsArray() {
58
		if (!$this->trackIds || \strlen($this->trackIds) < 3) {
59
			// the list is empty if there is nothing between the leading and trailing '|'
60
			return [];
61
		} else {
62
			$encoded = \substr($this->trackIds, 1, -1); // omit leading and trailing '|'
63
			return \array_map('intval', \explode('|', $encoded));
64
		}
65
	}
66
67
	/**
68
	 * @param int[] $trackIds
69
	 */
70
	public function setTrackIdsFromArray($trackIds) {
71
		// encode to format like "|123|45|667|"
72
		$this->setTrackIds('|' . \implode('|', $trackIds) . '|');
73
	}
74
75
	public function toAPI() {
76
		return [
77
			'name' => $this->getName(),
78
			'trackIds' => $this->getTrackIdsAsArray(),
79
			'id' => $this->getId(),
80
			'created' => $this->getCreated(),
81
			'updated' => $this->getUpdated(),
82
			'comment' => $this->getComment()
83
		];
84
	}
85
86
	public function toAmpacheApi(callable $createImageUrl) {
87
		return [
88
			'id' => (string)$this->getId(),
89
			'name' => $this->getName(),
90
			'owner' => $this->getUserId(),
91
			'items' => $this->getTrackCount(),
92
			'art' => $createImageUrl($this),
93
			'type' => 'Private'
94
		];
95
	}
96
97
	public function toSubsonicApi() {
98
		return [
99
			'id' => $this->getId(),
100
			'name' => $this->getName(),
101
			'owner' => $this->userId,
102
			'public' => false,
103
			'songCount' => $this->getTrackCount(),
104
			'duration' => $this->getDuration(),
105
			'comment' => $this->getComment() ?: '',
106
			'created' => Util::formatZuluDateTime($this->getCreated()),
107
			'changed' => Util::formatZuluDateTime($this->getUpdated()),
108
			'coverArt' => 'pl-' . $this->getId() // work around: DSub always fetches the art using ID like "pl-NNN" even if we  use some other format here
109
		];
110
	}
111
}
112