Passed
Push — master ( d5fa9e...eded98 )
by Pauli
01:56
created

Track::toAPI()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 13
nc 1
nop 1
dl 0
loc 15
ccs 13
cts 13
cp 1
crap 1
rs 9.8333
c 0
b 0
f 0
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
 * @author Pauli Järvinen <[email protected]>
11
 * @copyright Morris Jobke 2013, 2014
12
 * @copyright Pauli Järvinen 2016 - 2020
13
 */
14
15
namespace OCA\Music\Db;
16
17
use \OCP\IURLGenerator;
0 ignored issues
show
Bug introduced by
The type OCP\IURLGenerator was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
19
use \OCP\AppFramework\Db\Entity;
0 ignored issues
show
Bug introduced by
The type OCP\AppFramework\Db\Entity was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
20
21
use \OCA\Music\Utility\Util;
22
23
/**
24
 * @method string getTitle()
25
 * @method void setTitle(string $title)
26
 * @method int getNumber()
27
 * @method void setNumber(int $number)
28
 * @method int getDisk()
29
 * @method void setDisk(int $disk)
30
 * @method int getYear()
31
 * @method void setYear(int $year)
32
 * @method int getArtistId()
33
 * @method void setArtistId(int $artistId)
34
 * @method Artist getArtist()
35
 * @method void setArtist(Artist $artist)
36
 * @method int getAlbumId()
37
 * @method void setAlbumId(int $albumId)
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 Genre getGenre()
57
 * @method void setGenre(Genre $genre)
58
 * @method string getFilename()
59
 * @method void setFilename(string $filename)
60
 * @method int getSize()
61
 * @method void setSize(int $size)
62
 */
63
class Track extends Entity {
64
	public $title;
65
	public $number;
66
	public $disk;
67
	public $year;
68
	public $artistId;
69
	public $albumId;
70
	public $length;
71
	public $fileId;
72
	public $bitrate;
73
	public $uri;
74
	public $mimetype;
75
	public $userId;
76
	public $mbid;
77
	public $starred;
78
	public $genreId;
79
	public $filename;
80
	public $size;
81
82
	// these don't come from the music_tracks table:
83
	public $artist;
84
	public $album;
85
	public $genre;
86
87 14
	public function __construct() {
88 14
		$this->addType('number', 'int');
89 14
		$this->addType('disk', 'int');
90 14
		$this->addType('year', 'int');
91 14
		$this->addType('artistId', 'int');
92 14
		$this->addType('albumId', 'int');
93 14
		$this->addType('length', 'int');
94 14
		$this->addType('bitrate', 'int');
95 14
		$this->addType('fileId', 'int');
96 14
		$this->addType('genreId', 'int');
97 14
		$this->addType('size', 'int');
98 14
	}
99
100 10
	public function getUri(IURLGenerator $urlGenerator) {
101 10
		return $urlGenerator->linkToRoute(
102 10
			'music.api.track',
103 10
			['trackIdOrSlug' => $this->id]
104
		);
105
	}
106
107 10
	public function getArtistWithUri(IURLGenerator $urlGenerator) {
108
		return [
109 10
			'id' => $this->artistId,
110 10
			'uri' => $urlGenerator->linkToRoute(
111 10
				'music.api.artist',
112 10
				['artistIdOrSlug' => $this->artistId]
113
			)
114
		];
115
	}
116
117 10
	public function getAlbumWithUri(IURLGenerator $urlGenerator) {
118
		return [
119 10
			'id' => $this->albumId,
120 10
			'uri' => $urlGenerator->linkToRoute(
121 10
				'music.api.album',
122 10
				['albumIdOrSlug' => $this->albumId]
123
			)
124
		];
125
	}
126
127 1
	public function toCollection($l10n) {
128
		return [
129 1
			'title' => $this->getTitle(),
130 1
			'number' => $this->getNumber(),
131 1
			'disk' => $this->getDisk(),
132 1
			'artistName' => $this->getArtist()->getNameString($l10n),
133 1
			'artistId' => $this->getArtistId(),
134 1
			'files' => [$this->getMimetype() => $this->getFileId()],
135 1
			'id' => $this->getId(),
136
		];
137
	}
138
139 10
	public function toAPI(IURLGenerator $urlGenerator) {
140
		return [
141 10
			'title' => $this->getTitle(),
142 10
			'ordinal' => $this->getDiskAdjustedTrackNumber(),
143 10
			'artist' => $this->getArtistWithUri($urlGenerator),
144 10
			'album' => $this->getAlbumWithUri($urlGenerator),
145 10
			'length' => $this->getLength(),
146 10
			'files' => [$this->getMimetype() => $urlGenerator->linkToRoute(
147 10
				'music.api.download',
148 10
				['fileId' => $this->getFileId()]
149
			)],
150 10
			'bitrate' => $this->getBitrate(),
151 10
			'id' => $this->getId(),
152 10
			'slug' => $this->getId() . '-' . $this->slugify('title'),
153 10
			'uri' => $this->getUri($urlGenerator)
154
		];
155
	}
156
157 10
	public function getDiskAdjustedTrackNumber() {
158
		// On single-disk albums, the track number is given as-is.
159
		// On multi-disk albums, the disk-number is applied to the track number.
160
		// In case we have no Album reference, the best we can do is to apply the
161
		// disk number if it is greater than 1. For disk 1, we don't know if this
162
		// is a multi-disk album or not.
163 10
		$numberOfDisks = ($this->album) ? $this->album->getNumberOfDisks() : null;
164 10
		$trackNumber = $this->getNumber();
165
166 10
		if ($this->disk > 1 || $numberOfDisks > 1) {
167
			$trackNumber = $trackNumber ?: 0;
168
			$trackNumber += (100 * $this->disk);
169
		}
170
171 10
		return $trackNumber;
172
	}
173
174
	public function getFileExtension() {
175
		$parts = \explode('.', $this->getFilename());
176
		return \end($parts);
177
	}
178
179
	public static function compareArtistAndTitle(Track $a, Track $b) {
180
		$artistResult = Util::stringCaseCompare(
181
				$a->getArtist()->getName(), $b->getArtist()->getName());
182
183
		return $artistResult ?: Util::stringCaseCompare($a->getTitle(), $b->getTitle());
184
	}
185
}
186