Passed
Push — feature/playlist_improvements ( 2a690a...faf9ee )
by Pauli
14:31
created

Track::getArtistNameString()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 2
ccs 2
cts 2
cp 1
crap 2
rs 10
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\IL10N;
0 ignored issues
show
Bug introduced by
The type OCP\IL10N 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
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...
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 string getArtistName()
35
 * @method void setArtistName(string $artistName)
36
 * @method int getAlbumId()
37
 * @method void setAlbumId(int $albumId)
38
 * @method string getAlbumName()
39
 * @method void setAlbumName(string $albumName)
40
 * @method Album getAlbum()
41
 * @method void setAlbum(Album $album)
42
 * @method int getLength()
43
 * @method void setLength(int $length)
44
 * @method int getFileId()
45
 * @method void setFileId(int $fileId)
46
 * @method int getBitrate()
47
 * @method void setBitrate(int $bitrate)
48
 * @method string getMimetype()
49
 * @method void setMimetype(string $mimetype)
50
 * @method string getUserId()
51
 * @method void setUserId(string $userId)
52
 * @method string getMbid()
53
 * @method void setMbid(string $mbid)
54
 * @method string getStarred()
55
 * @method void setStarred(string $timestamp)
56
 * @method int getGenreId()
57
 * @method void setGenreId(int $genreId)
58
 * @method string getGenreName()
59
 * @method void setGenreName(string $genreName)
60
 * @method string getFilename()
61
 * @method void setFilename(string $filename)
62
 * @method int getSize()
63
 * @method void setSize(int $size)
64
 * @method int getNumberOnPlaylist()
65
 * @method void setNumberOnPlaylist(int $number)
66
 */
67
class Track extends Entity {
68
	public $title;
69
	public $number;
70
	public $disk;
71
	public $year;
72
	public $artistId;
73
	public $albumId;
74
	public $length;
75
	public $fileId;
76
	public $bitrate;
77
	public $uri;
78
	public $mimetype;
79
	public $userId;
80
	public $mbid;
81
	public $starred;
82
	public $genreId;
83
	public $filename;
84
	public $size;
85
	// not from the music_tracks table but still part of the standard content of this entity:
86
	public $albumName;
87
	public $artistName;
88
	public $genreName;
89
90 14
	// the rest of the variables are injected separately when needed
91 14
	public $album;
92 14
	public $numberOnPlaylist;
93 14
94 14
	public function __construct() {
95 14
		$this->addType('number', 'int');
96 14
		$this->addType('disk', 'int');
97 14
		$this->addType('year', 'int');
98 14
		$this->addType('artistId', 'int');
99 14
		$this->addType('albumId', 'int');
100 14
		$this->addType('length', 'int');
101 14
		$this->addType('bitrate', 'int');
102
		$this->addType('fileId', 'int');
103 10
		$this->addType('genreId', 'int');
104 10
		$this->addType('size', 'int');
105 10
	}
106 10
107
	public function getUri(IURLGenerator $urlGenerator) {
108
		return $urlGenerator->linkToRoute(
109
			'music.api.track',
110 10
			['trackIdOrSlug' => $this->id]
111
		);
112 10
	}
113 10
114 10
	public function getArtistWithUri(IURLGenerator $urlGenerator) {
115 10
		return [
116
			'id' => $this->artistId,
117
			'uri' => $urlGenerator->linkToRoute(
118
				'music.api.artist',
119
				['artistIdOrSlug' => $this->artistId]
120 10
			)
121
		];
122 10
	}
123 10
124 10
	public function getAlbumWithUri(IURLGenerator $urlGenerator) {
125 10
		return [
126
			'id' => $this->albumId,
127
			'uri' => $urlGenerator->linkToRoute(
128
				'music.api.album',
129
				['albumIdOrSlug' => $this->albumId]
130 1
			)
131
		];
132 1
	}
133 1
134 1
	public function getArtistNameString(IL10N $l10n) {
135 1
		return $this->getArtistName() ?: Artist::unknownNameString($l10n);
136 1
	}
137 1
138 1
	public function getAlbumNameString(IL10N $l10n) {
139 1
		return $this->getAlbumName() ?: Album::unknownNameString($l10n);
140
	}
141
142
	public function getGenreNameString(IL10N $l10n) {
143 10
		return $this->getGenreName() ?: Genre::unknownNameString($l10n);
144
	}
145 10
146 10
	public function toCollection(IL10N $l10n) {
147 10
		return [
148 10
			'title' => $this->getTitle(),
149 10
			'number' => $this->getNumber(),
150 10
			'disk' => $this->getDisk(),
151 10
			'artistName' => $this->getArtistNameString($l10n),
152 10
			'artistId' => $this->getArtistId(),
153
			'length' => $this->getLength(),
154 10
			'files' => [$this->getMimetype() => $this->getFileId()],
155 10
			'id' => $this->getId(),
156 10
		];
157 10
	}
158
159
	public function toAPI(IURLGenerator $urlGenerator) {
160
		return [
161 10
			'title' => $this->getTitle(),
162
			'ordinal' => $this->getAdjustedTrackNumber(),
163 10
			'artist' => $this->getArtistWithUri($urlGenerator),
164
			'album' => $this->getAlbumWithUri($urlGenerator),
165
			'length' => $this->getLength(),
166
			'files' => [$this->getMimetype() => $urlGenerator->linkToRoute(
167
				'music.api.download',
168
				['fileId' => $this->getFileId()]
169
			)],
170
			'bitrate' => $this->getBitrate(),
171
			'id' => $this->getId(),
172 10
			'slug' => $this->getId() . '-' . $this->slugify('title'),
173 10
			'uri' => $this->getUri($urlGenerator)
174
		];
175 10
	}
176
177
	public function getAdjustedTrackNumber() {
178
		// Number on playlist overrides the track number if it is set.
179
		if ($this->numberOnPlaylist !== null) {
180
			$trackNumber = $this->numberOnPlaylist;
181 10
		}
182
		else {
183
			// On single-disk albums, the track number is given as-is.
184
			// On multi-disk albums, the disk-number is applied to the track number.
185
			// In case we have no Album reference, the best we can do is to apply the
186
			// disk number if it is greater than 1. For disk 1, we don't know if this
187
			// is a multi-disk album or not.
188
			$numberOfDisks = ($this->album) ? $this->album->getNumberOfDisks() : null;
189
			$trackNumber = $this->getNumber();
190
191
			if ($this->disk > 1 || $numberOfDisks > 1) {
192
				$trackNumber = $trackNumber ?: 0;
193
				$trackNumber += (100 * $this->disk);
194
			}
195
		}
196
197
		return $trackNumber;
198
	}
199
200
	public function getFileExtension() {
201
		$parts = \explode('.', $this->getFilename());
202
		return \end($parts);
203
	}
204
205
	public static function compareArtistAndTitle(Track $a, Track $b) {
206
		$artistResult = Util::stringCaseCompare($a->getArtistName(), $b->getArtistName());
207
208
		return $artistResult ?: Util::stringCaseCompare($a->getTitle(), $b->getTitle());
209
	}
210
}
211