Passed
Push — feature/329_Subsonic_API ( b603ae...7f0054 )
by Pauli
12:00
created

Album::compareYearAndName()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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