Passed
Push — feature/909_Ampache_API_improv... ( a84036...a957d5 )
by Pauli
03:28
created

Playlist::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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