Passed
Push — master ( 35b7b2...cb91d5 )
by Pauli
02:01
created

Track::getDiskAdjustedTrackNumber()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 15
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 5.583

Importance

Changes 0
Metric Value
cc 5
eloc 6
nc 4
nop 0
dl 0
loc 15
ccs 5
cts 7
cp 0.7143
crap 5.583
rs 9.6111
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 setTitle(string $title)
26
 * @method int getNumber()
27
 * @method setNumber(int $number)
28
 * @method int getDisk()
29
 * @method setDisk(int $disk)
30
 * @method int getYear()
31
 * @method setYear(int $year)
32
 * @method int getArtistId()
33
 * @method setArtistId(int $artistId)
34
 * @method Artist getArtist()
35
 * @method setArtist(Artist $artist)
36
 * @method int getAlbumId()
37
 * @method setAlbumId(int $albumId)
38
 * @method Album getAlbum()
39
 * @method setAlbum(Album $album)
40
 * @method int getLength()
41
 * @method setLength(int $length)
42
 * @method int getFileId()
43
 * @method setFileId(int $fileId)
44
 * @method int getBitrate()
45
 * @method setBitrate(int $bitrate)
46
 * @method string getMimetype()
47
 * @method setMimetype(string $mimetype)
48
 * @method string getUserId()
49
 * @method setUserId(string $userId)
50
 * @method string getFilename()
51
 * @method setFilename(string $filename)
52
 * @method int getSize()
53
 * @method setSize(int $size)
54
 */
55
class Track extends Entity {
56
	public $title;
57
	public $number;
58
	public $disk;
59
	public $year;
60
	public $artistId;
61
	public $albumId;
62
	public $length;
63
	public $fileId;
64
	public $bitrate;
65
	public $uri;
66
	public $mimetype;
67
	public $userId;
68
	public $mbid;
69
	public $filename;
70
	public $size;
71
72
	// these don't come from the music_tracks table:
73
	public $artist;
74
	public $album;
75
76 14
	public function __construct() {
77 14
		$this->addType('number', 'int');
78 14
		$this->addType('disk', 'int');
79 14
		$this->addType('year', 'int');
80 14
		$this->addType('artistId', 'int');
81 14
		$this->addType('albumId', 'int');
82 14
		$this->addType('length', 'int');
83 14
		$this->addType('bitrate', 'int');
84 14
		$this->addType('fileId', 'int');
85 14
		$this->addType('size', 'int');
86 14
	}
87
88 10
	public function getUri(IURLGenerator $urlGenerator) {
89 10
		return $urlGenerator->linkToRoute(
90 10
			'music.api.track',
91 10
			['trackIdOrSlug' => $this->id]
92
		);
93
	}
94
95 10
	public function getArtistWithUri(IURLGenerator $urlGenerator) {
96
		return [
97 10
			'id' => $this->artistId,
98 10
			'uri' => $urlGenerator->linkToRoute(
99 10
				'music.api.artist',
100 10
				['artistIdOrSlug' => $this->artistId]
101
			)
102
		];
103
	}
104
105 10
	public function getAlbumWithUri(IURLGenerator $urlGenerator) {
106
		return [
107 10
			'id' => $this->albumId,
108 10
			'uri' => $urlGenerator->linkToRoute(
109 10
				'music.api.album',
110 10
				['albumIdOrSlug' => $this->albumId]
111
			)
112
		];
113
	}
114
115 1
	public function toCollection($l10n) {
116
		return [
117 1
			'title' => $this->getTitle(),
118 1
			'number' => $this->getNumber(),
119 1
			'disk' => $this->getDisk(),
120 1
			'artistName' => $this->getArtist()->getNameString($l10n),
121 1
			'artistId' => $this->getArtistId(),
122 1
			'files' => [$this->getMimetype() => $this->getFileId()],
123 1
			'id' => $this->getId(),
124
		];
125
	}
126
127 10
	public function toAPI(IURLGenerator $urlGenerator) {
128
		return [
129 10
			'title' => $this->getTitle(),
130 10
			'ordinal' => $this->getDiskAdjustedTrackNumber(),
131 10
			'artist' => $this->getArtistWithUri($urlGenerator),
132 10
			'album' => $this->getAlbumWithUri($urlGenerator),
133 10
			'length' => $this->getLength(),
134 10
			'files' => [$this->getMimetype() => $urlGenerator->linkToRoute(
135 10
				'music.api.download',
136 10
				['fileId' => $this->getFileId()]
137
			)],
138 10
			'bitrate' => $this->getBitrate(),
139 10
			'id' => $this->getId(),
140 10
			'slug' => $this->getId() . '-' . $this->slugify('title'),
141 10
			'uri' => $this->getUri($urlGenerator)
142
		];
143
	}
144
145 10
	public function getDiskAdjustedTrackNumber() {
146
		// On single-disk albums, the track number is given as-is.
147
		// On multi-disk albums, the disk-number is applied to the track number.
148
		// In case we have no Album reference, the best we can do is to apply the
149
		// disk number if it is greater than 1. For disk 1, we don't know if this
150
		// is a multi-disk album or not.
151 10
		$numberOfDisks = ($this->album) ? $this->album->getNumberOfDisks() : null;
152 10
		$trackNumber = $this->getNumber();
153
154 10
		if ($this->disk > 1 || $numberOfDisks > 1) {
155
			$trackNumber = $trackNumber ?: 0;
156
			$trackNumber += (100 * $this->disk);
157
		}
158
159 10
		return $trackNumber;
160
	}
161
162
	public function getFileExtension() {
163
		$parts = \explode('.', $this->getFilename());
164
		return \end($parts);
165
	}
166
167
	public static function compareArtistAndTitle(Track $a, Track $b) {
168
		$artistResult = Util::stringCaseCompare(
169
				$a->getArtist()->getName(), $b->getArtist()->getName());
170
171
		return $artistResult ?: Util::stringCaseCompare($a->getTitle(), $b->getTitle());
172
	}
173
}
174