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

Album::getAlbumArtistNameString()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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