Passed
Push — master ( 4e74cd...0361f1 )
by Pauli
02:51
created

Track::toAmpacheApi()   B

Complexity

Conditions 8
Paths 4

Size

Total Lines 42
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 32
c 1
b 0
f 0
dl 0
loc 42
rs 8.1635
cc 8
nc 4
nop 3
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 2013, 2014
12
 * @copyright Pauli Järvinen 2016 - 2021
13
 */
14
15
namespace OCA\Music\Db;
16
17
use \OCP\IL10N;
18
use \OCP\IURLGenerator;
19
use \OCP\AppFramework\Db\Entity;
20
21
/**
22
 * @method string getTitle()
23
 * @method void setTitle(string $title)
24
 * @method ?int getNumber()
25
 * @method void setNumber(?int $number)
26
 * @method ?int getDisk()
27
 * @method void setDisk(?int $disk)
28
 * @method ?int getYear()
29
 * @method void setYear(?int $year)
30
 * @method int getArtistId()
31
 * @method void setArtistId(int $artistId)
32
 * @method ?string getArtistName()
33
 * @method void setArtistName(?string $artistName)
34
 * @method int getAlbumId()
35
 * @method void setAlbumId(int $albumId)
36
 * @method ?string getAlbumName()
37
 * @method void setAlbumName(?string $albumName)
38
 * @method ?Album getAlbum()
39
 * @method void setAlbum(?Album $album)
40
 * @method ?int getLength()
41
 * @method void setLength(?int $length)
42
 * @method int getFileId()
43
 * @method void setFileId(int $fileId)
44
 * @method ?int getBitrate()
45
 * @method void setBitrate(?int $bitrate)
46
 * @method string getMimetype()
47
 * @method void setMimetype(string $mimetype)
48
 * @method string getUserId()
49
 * @method void setUserId(string $userId)
50
 * @method ?string getMbid()
51
 * @method void setMbid(?string $mbid)
52
 * @method ?string getStarred()
53
 * @method void setStarred(?string $timestamp)
54
 * @method ?int getGenreId()
55
 * @method void setGenreId(?int $genreId)
56
 * @method string getCreated()
57
 * @method setCreated(string $timestamp)
58
 * @method string getUpdated()
59
 * @method setUpdated(string $timestamp)
60
 * @method ?string getGenreName()
61
 * @method void setGenreName(?string $genreName)
62
 * @method string getFilename()
63
 * @method void setFilename(string $filename)
64
 * @method int getSize()
65
 * @method void setSize(int $size)
66
 * @method ?int getNumberOnPlaylist()
67
 * @method void setNumberOnPlaylist(?int $number)
68
 */
69
class Track extends Entity {
70
	public $title;
71
	public $number;
72
	public $disk;
73
	public $year;
74
	public $artistId;
75
	public $albumId;
76
	public $length;
77
	public $fileId;
78
	public $bitrate;
79
	public $uri;
80
	public $mimetype;
81
	public $userId;
82
	public $mbid;
83
	public $starred;
84
	public $genreId;
85
	public $created;
86
	public $updated;
87
	// not from the music_tracks table but still part of the standard content of this entity:
88
	public $filename;
89
	public $size;
90
	public $albumName;
91
	public $artistName;
92
	public $genreName;
93
94
	// the rest of the variables are injected separately when needed
95
	public $album;
96
	public $numberOnPlaylist;
97
98
	public function __construct() {
99
		$this->addType('number', 'int');
100
		$this->addType('disk', 'int');
101
		$this->addType('year', 'int');
102
		$this->addType('artistId', 'int');
103
		$this->addType('albumId', 'int');
104
		$this->addType('length', 'int');
105
		$this->addType('bitrate', 'int');
106
		$this->addType('fileId', 'int');
107
		$this->addType('genreId', 'int');
108
		$this->addType('size', 'int');
109
	}
110
111
	public function getUri(IURLGenerator $urlGenerator) {
112
		return $urlGenerator->linkToRoute(
113
			'music.api.track',
114
			['trackIdOrSlug' => $this->id]
115
		);
116
	}
117
118
	public function getArtistWithUri(IURLGenerator $urlGenerator) {
119
		return [
120
			'id' => $this->artistId,
121
			'uri' => $urlGenerator->linkToRoute(
122
				'music.api.artist',
123
				['artistIdOrSlug' => $this->artistId]
124
			)
125
		];
126
	}
127
128
	public function getAlbumWithUri(IURLGenerator $urlGenerator) {
129
		return [
130
			'id' => $this->albumId,
131
			'uri' => $urlGenerator->linkToRoute(
132
				'music.api.album',
133
				['albumIdOrSlug' => $this->albumId]
134
			)
135
		];
136
	}
137
138
	public function getArtistNameString(IL10N $l10n) {
139
		return $this->getArtistName() ?: Artist::unknownNameString($l10n);
140
	}
141
142
	public function getAlbumNameString(IL10N $l10n) {
143
		return $this->getAlbumName() ?: Album::unknownNameString($l10n);
144
	}
145
146
	public function getGenreNameString(IL10N $l10n) {
147
		return $this->getGenreName() ?: Genre::unknownNameString($l10n);
148
	}
149
150
	public function toCollection(IL10N $l10n) : array {
151
		return [
152
			'title' => $this->getTitle(),
153
			'number' => $this->getNumber(),
154
			'disk' => $this->getDisk(),
155
			'artistName' => $this->getArtistNameString($l10n),
156
			'artistId' => $this->getArtistId(),
157
			'length' => $this->getLength(),
158
			'files' => [$this->getMimetype() => $this->getFileId()],
159
			'id' => $this->getId(),
160
		];
161
	}
162
163
	public function toAPI(IURLGenerator $urlGenerator) : array {
164
		return [
165
			'title' => $this->getTitle(),
166
			'ordinal' => $this->getAdjustedTrackNumber(),
167
			'artist' => $this->getArtistWithUri($urlGenerator),
168
			'album' => $this->getAlbumWithUri($urlGenerator),
169
			'length' => $this->getLength(),
170
			'files' => [$this->getMimetype() => $urlGenerator->linkToRoute(
171
				'music.api.download',
172
				['fileId' => $this->getFileId()]
173
			)],
174
			'bitrate' => $this->getBitrate(),
175
			'id' => $this->getId(),
176
			'slug' => $this->getId() . '-' . $this->slugify('title'),
177
			'uri' => $this->getUri($urlGenerator)
178
		];
179
	}
180
181
	public function toAmpacheApi(IL10N $l10n, callable $createPlayUrl, callable $createImageUrl) : array {
182
		$album = $this->getAlbum();
183
184
		$result = [
185
			'id' => (string)$this->getId(),
186
			'title' => $this->getTitle() ?: '',
187
			'name' => $this->getTitle() ?: '',
188
			'artist' => [
189
				'id' => (string)$this->getArtistId() ?: '0',
190
				'value' => $this->getArtistNameString($l10n)
191
			],
192
			'albumartist' => [
193
				'id' => (string)$album->getAlbumArtistId() ?: '0',
194
				'value' => $album->getAlbumArtistNameString($l10n)
195
			],
196
			'album' => [
197
				'id' => (string)$album->getId() ?: '0',
198
				'value' => $album->getNameString($l10n)
199
			],
200
			'url' => $createPlayUrl($this),
201
			'time' => $this->getLength(),
202
			'year' => $this->getYear(),
203
			'track' => $this->getAdjustedTrackNumber(),
204
			'bitrate' => $this->getBitrate(),
205
			'mime' => $this->getMimetype(),
206
			'size' => $this->getSize(),
207
			'art' => $createImageUrl($this),
208
			'rating' => 0,
209
			'preciserating' => 0,
210
			'flag' => empty($this->getStarred()) ? 0 : 1,
211
		];
212
213
		$genreId = $this->getGenreId();
214
		if ($genreId !== null) {
215
			$result['tag'] = [[
216
				'id' => (string)$genreId,
217
				'value' => $this->getGenreNameString($l10n),
218
				'count' => 1
219
			]];
220
		}
221
222
		return $result;
223
	}
224
225
	public function getAdjustedTrackNumber() {
226
		// Number on playlist overrides the track number if it is set.
227
		if ($this->numberOnPlaylist !== null) {
228
			$trackNumber = $this->numberOnPlaylist;
229
		} else {
230
			// On single-disk albums, the track number is given as-is.
231
			// On multi-disk albums, the disk-number is applied to the track number.
232
			// In case we have no Album reference, the best we can do is to apply the
233
			// disk number if it is greater than 1. For disk 1, we don't know if this
234
			// is a multi-disk album or not.
235
			$numberOfDisks = ($this->album) ? $this->album->getNumberOfDisks() : null;
236
			$trackNumber = $this->getNumber();
237
238
			if ($this->disk > 1 || $numberOfDisks > 1) {
239
				$trackNumber = $trackNumber ?: 0;
240
				$trackNumber += (100 * $this->disk);
241
			}
242
		}
243
244
		return $trackNumber;
245
	}
246
247
	public function getFileExtension() {
248
		$parts = \explode('.', $this->getFilename() ?? '');
249
		return \end($parts);
250
	}
251
252
}
253