Completed
Pull Request — master (#777)
by Pauli
14:05 queued 11:49
created

Track::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 10
nc 1
nop 0
dl 0
loc 11
ccs 11
cts 11
cp 1
crap 1
rs 9.9332
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
 * @method int getNumberOnPlaylist()
63
 * @method void setNumberOnPlaylist(int $number)
64
 */
65
class Track extends Entity {
66
	public $title;
67
	public $number;
68
	public $disk;
69
	public $year;
70
	public $artistId;
71
	public $albumId;
72
	public $length;
73
	public $fileId;
74
	public $bitrate;
75
	public $uri;
76
	public $mimetype;
77
	public $userId;
78
	public $mbid;
79
	public $starred;
80
	public $genreId;
81
	public $filename;
82
	public $size;
83
84
	// these don't come from the music_tracks table:
85
	public $artist;
86
	public $album;
87
	public $genre;
88
	public $numberOnPlaylist;
89
90 14
	public function __construct() {
91 14
		$this->addType('number', 'int');
92 14
		$this->addType('disk', 'int');
93 14
		$this->addType('year', 'int');
94 14
		$this->addType('artistId', 'int');
95 14
		$this->addType('albumId', 'int');
96 14
		$this->addType('length', 'int');
97 14
		$this->addType('bitrate', 'int');
98 14
		$this->addType('fileId', 'int');
99 14
		$this->addType('genreId', 'int');
100 14
		$this->addType('size', 'int');
101 14
	}
102
103 10
	public function getUri(IURLGenerator $urlGenerator) {
104 10
		return $urlGenerator->linkToRoute(
105 10
			'music.api.track',
106 10
			['trackIdOrSlug' => $this->id]
107
		);
108
	}
109
110 10
	public function getArtistWithUri(IURLGenerator $urlGenerator) {
111
		return [
112 10
			'id' => $this->artistId,
113 10
			'uri' => $urlGenerator->linkToRoute(
114 10
				'music.api.artist',
115 10
				['artistIdOrSlug' => $this->artistId]
116
			)
117
		];
118
	}
119
120 10
	public function getAlbumWithUri(IURLGenerator $urlGenerator) {
121
		return [
122 10
			'id' => $this->albumId,
123 10
			'uri' => $urlGenerator->linkToRoute(
124 10
				'music.api.album',
125 10
				['albumIdOrSlug' => $this->albumId]
126
			)
127
		];
128
	}
129
130 1
	public function toCollection($l10n) {
131
		return [
132 1
			'title' => $this->getTitle(),
133 1
			'number' => $this->getNumber(),
134 1
			'disk' => $this->getDisk(),
135 1
			'artistName' => $this->getArtist()->getNameString($l10n),
136 1
			'artistId' => $this->getArtistId(),
137 1
			'length' => $this->getLength(),
138 1
			'files' => [$this->getMimetype() => $this->getFileId()],
139 1
			'id' => $this->getId(),
140
		];
141
	}
142
143 10
	public function toAPI(IURLGenerator $urlGenerator) {
144
		return [
145 10
			'title' => $this->getTitle(),
146 10
			'ordinal' => $this->getAdjustedTrackNumber(),
147 10
			'artist' => $this->getArtistWithUri($urlGenerator),
148 10
			'album' => $this->getAlbumWithUri($urlGenerator),
149 10
			'length' => $this->getLength(),
150 10
			'files' => [$this->getMimetype() => $urlGenerator->linkToRoute(
151 10
				'music.api.download',
152 10
				['fileId' => $this->getFileId()]
153
			)],
154 10
			'bitrate' => $this->getBitrate(),
155 10
			'id' => $this->getId(),
156 10
			'slug' => $this->getId() . '-' . $this->slugify('title'),
157 10
			'uri' => $this->getUri($urlGenerator)
158
		];
159
	}
160
161 10
	public function getAdjustedTrackNumber() {
162
		// Number on playlist overrides the track number if it is set.
163 10
		if ($this->numberOnPlaylist !== null) {
164
			$trackNumber = $this->numberOnPlaylist;
165
		}
166
		else {
167
			// On single-disk albums, the track number is given as-is.
168
			// On multi-disk albums, the disk-number is applied to the track number.
169
			// In case we have no Album reference, the best we can do is to apply the
170
			// disk number if it is greater than 1. For disk 1, we don't know if this
171
			// is a multi-disk album or not.
172 10
			$numberOfDisks = ($this->album) ? $this->album->getNumberOfDisks() : null;
173 10
			$trackNumber = $this->getNumber();
174
175 10
			if ($this->disk > 1 || $numberOfDisks > 1) {
176
				$trackNumber = $trackNumber ?: 0;
177
				$trackNumber += (100 * $this->disk);
178
			}
179
		}
180
181 10
		return $trackNumber;
182
	}
183
184
	public function getFileExtension() {
185
		$parts = \explode('.', $this->getFilename());
186
		return \end($parts);
187
	}
188
189
	public static function compareArtistAndTitle(Track $a, Track $b) {
190
		$artistResult = Util::stringCaseCompare(
191
				$a->getArtist()->getName(), $b->getArtist()->getName());
192
193
		return $artistResult ?: Util::stringCaseCompare($a->getTitle(), $b->getTitle());
194
	}
195
}
196