Passed
Push — feature/909_Ampache_API_improv... ( a84036...a957d5 )
by Pauli
03:28
created

Album::getYears()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
rs 10
c 0
b 0
f 0
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 2017 - 2023
13
 */
14
15
namespace OCA\Music\Db;
16
17
use OCP\IL10N;
18
use OCP\IURLGenerator;
19
20
use OCA\Music\Utility\Util;
21
22
/**
23
 * @method ?string getName()
24
 * @method void setName(?string $name)
25
 * @method ?string getMbid()
26
 * @method void setMbid(?string $mbid)
27
 * @method ?int getDisk()
28
 * @method void setDisk(?int $discnumber)
29
 * @method ?string getMbidGroup()
30
 * @method void setMbidGroup(?string $mbidGroup)
31
 * @method ?int getCoverFileId()
32
 * @method void setCoverFileId(?int $coverFileId)
33
 * @method int getAlbumArtistId()
34
 * @method void setAlbumArtistId(int $albumArtistId)
35
 * @method ?string getAlbumArtistName()
36
 * @method void setAlbumArtistName(?string $name)
37
 * @method string getHash()
38
 * @method void setHash(string $hash)
39
 * @method ?string getStarred()
40
 * @method void setStarred(?string $timestamp)
41
 * @method ?int getRating()
42
 * @method setRating(?int $rating)
43
 */
44
class Album extends Entity {
45
	public $name;
46
	public $mbid;
47
	public $disk; // deprecated
48
	public $mbidGroup;
49
	public $coverFileId;
50
	public $albumArtistId;
51
	public $hash;
52
	public $starred;
53
	public $rating;
54
	public $albumArtistName; // not from music_albums table but still part of the standard content
55
56
	// extra fields injected separately by AlbumBusinessLayer
57
	private $years;
58
	private $genres; // *partial* Genre objects, not all properties are set
59
	private $artistIds;
60
	private $numberOfDisks;
61
62
	public function __construct() {
63
		$this->addType('disk', 'int');
64
		$this->addType('coverFileId', 'int');
65
		$this->addType('albumArtistId', 'int');
66
		$this->addType('rating', 'int');
67
	}
68
69
	public function getYears() : ?array {
70
		return $this->years;
71
	}
72
73
	public function setYears(?array $years) : void {
74
		$this->years = $years;
75
	}
76
77
	public function getGenres() : ?array {
78
		return $this->genres;
79
	}
80
81
	public function setGenres(?array $genres) : void {
82
		$this->genres = $genres;
83
	}
84
85
	public function getArtistIds() : ?array {
86
		return $this->artistIds;
87
	}
88
89
	public function setArtistIds(?array $artistIds) : void {
90
		$this->artistIds = $artistIds;
91
	}
92
93
	public function getNumberOfDisks() : ?int {
94
		return $this->numberOfDisks;
95
	}
96
97
	public function setNumberOfDisks(?int $count) : void {
98
		$this->numberOfDisks = $count;
99
	}
100
101
	/**
102
	 * Generates URL to album
103
	 * @param \OCP\IURLGenerator $urlGenerator
104
	 * @return string the url
105
	 */
106
	public function getUri(IURLGenerator $urlGenerator) : string {
107
		return $urlGenerator->linkToRoute(
108
			'music.shivaApi.album',
109
			['albumId' => $this->id]
110
		);
111
	}
112
113
	/**
114
	 * Returns an array of all artists - each with ID and URL for that artist
115
	 * @param \OCP\IURLGenerator $urlGenerator URLGenerator
116
	 * @return array
117
	 */
118
	public function getArtists(IURLGenerator $urlGenerator) : array {
119
		$artists = [];
120
		foreach ($this->artistIds as $artistId) {
121
			$artists[] = [
122
				'id' => $artistId,
123
				'uri' => $urlGenerator->linkToRoute(
124
					'music.shivaApi.artist',
125
					['artistId' => $artistId]
126
				)
127
			];
128
		}
129
		return $artists;
130
	}
131
132
	/**
133
	 * Returns the years(s) of the album.
134
	 * The album may have zero, one, or multiple years as people may tag tracks of
135
	 * colletion albums with their original release dates. The respective formatted
136
	 * year ranges could be e.g. null, '2016', and '1995 - 2000'.
137
	 * @return string|null
138
	 */
139
	public function getYearRange() : ?string {
140
		$count = empty($this->years) ? 0 : \count($this->years);
141
142
		if ($count == 0) {
143
			return null;
144
		} elseif ($count == 1) {
145
			return (string)$this->years[0];
146
		} else {
147
			return \min($this->years) . ' - ' . \max($this->years);
148
		}
149
	}
150
151
	/**
152
	 * The Shiva and Ampache API definitions require the year to be a single numeric value.
153
	 * In case the album has multiple years, output the largest of these in the API.
154
	 * @return int|null
155
	 */
156
	public function yearToAPI() : ?int {
157
		return empty($this->years) ? null : (int)\max($this->years);
158
	}
159
160
	/**
161
	 * Returns the name of the album - if empty it returns the translated
162
	 * version of "Unknown album"
163
	 * @param IL10N $l10n
164
	 * @return string
165
	 */
166
	public function getNameString(IL10N $l10n) : string {
167
		return $this->getName() ?: self::unknownNameString($l10n);
168
	}
169
170
	/**
171
	 * Returns the name of the album artist - if empty it returns the translated
172
	 * version of "Unknown artist"
173
	 * @param IL10N $l10n
174
	 * @return string
175
	 */
176
	public function getAlbumArtistNameString(IL10N $l10n) : string {
177
		return $this->getAlbumArtistName() ?: Artist::unknownNameString($l10n);
178
	}
179
180
	/**
181
	 * Return the cover URL to be used in the Shiva API
182
	 * @param IURLGenerator $urlGenerator
183
	 * @return string|null
184
	 */
185
	public function coverToAPI(IURLGenerator $urlGenerator) : ?string {
186
		$coverUrl = null;
187
		if ($this->getCoverFileId() > 0) {
188
			$coverUrl = $urlGenerator->linkToRoute('music.api.albumCover',
189
					['albumId' => $this->getId()]);
190
		}
191
		return $coverUrl;
192
	}
193
194
	/**
195
	 * If the cover image is already cached, the cover is presented with a link containing the image hash.
196
	 * Otherwise, the collection contains an URL which triggers the caching and then redirects to the
197
	 * URL with image hash.
198
	 * @param  IURLGenerator $urlGenerator URL Generator
199
	 * @param  string|null $cachedCoverHash Cached cover image hash if available
200
	 * @return string|null
201
	 */
202
	public function coverToCollection(IURLGenerator $urlGenerator, ?string $cachedCoverHash) : ?string {
203
		if (!empty($cachedCoverHash)) {
204
			return $urlGenerator->linkToRoute('music.api.cachedCover', ['hash' => $cachedCoverHash]);
205
		} elseif ($this->getCoverFileId() > 0) {
206
			return $this->coverToAPI($urlGenerator);
207
		} else {
208
			return null;
209
		}
210
	}
211
212
	/**
213
	 * Creates object used for collection API (array with name, year, cover URL and ID)
214
	 * @param  IURLGenerator $urlGenerator URL Generator
215
	 * @param  IL10N $l10n Localization handler
216
	 * @param  string|null $cachedCoverHash Cached cover image hash if available
217
	 * @param  Track[] $tracks Tracks of the album in the "toCollection" format
218
	 * @return array collection API object
219
	 */
220
	public function toCollection(IURLGenerator $urlGenerator, IL10N $l10n, ?string $cachedCoverHash, array $tracks) : array {
221
		return [
222
			'name'      => $this->getNameString($l10n),
223
			'year'      => $this->getYearRange(),
224
			'cover'     => $this->coverToCollection($urlGenerator, $cachedCoverHash),
225
			'id'        => $this->getId(),
226
			'diskCount' => $this->getNumberOfDisks(),
227
			'tracks'    => $tracks
228
		];
229
	}
230
231
	/**
232
	 * Creates object used by the Shiva API (array with name, year, cover URL, ID, slug, URI and artists Array)
233
	 * @param  IURLGenerator $urlGenerator URL Generator
234
	 * @param  IL10N $l10n Localization handler
235
	 * @return array shiva API object
236
	 */
237
	public function toAPI(IURLGenerator $urlGenerator, IL10N $l10n) : array {
238
		return [
239
			'name'          => $this->getNameString($l10n),
240
			'year'          => $this->yearToAPI(),
241
			'cover'         => $this->coverToAPI($urlGenerator),
242
			'id'            => $this->getId(),
243
			'uri'           => $this->getUri($urlGenerator),
244
			'slug'          => $this->slugify('name'),
245
			'albumArtistId' => $this->getAlbumArtistId(),
246
			'artists'       => $this->getArtists($urlGenerator)
247
		];
248
	}
249
250
	public static function compareYearAndName(Album $a, Album $b) : int {
251
		$yearResult = \strcmp($a->getYearRange() ?? '', $b->getYearRange() ?? '');
252
253
		return $yearResult ?: Util::stringCaseCompare($a->getName(), $b->getName());
254
	}
255
256
	public static function unknownNameString(IL10N $l10n) : string {
257
		return (string) $l10n->t('Unknown album');
258
	}
259
}
260