Artist::getUri()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 4
rs 10
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 - 2025
13
 */
14
15
namespace OCA\Music\Db;
16
17
use OCP\IL10N;
18
use OCP\IURLGenerator;
19
20
/**
21
 * @method ?string getName()
22
 * @method void setName(?string $name)
23
 * @method ?int getCoverFileId()
24
 * @method void setCoverFileId(?int $coverFileId)
25
 * @method ?string getMbid()
26
 * @method void setMbid(?string $mbid)
27
 * @method string getHash()
28
 * @method void setHash(string $hash)
29
 * @method ?string getStarred()
30
 * @method void setStarred(?string $timestamp)
31
 * @method int getRating()
32
 * @method void setRating(int $rating)
33
 */
34
class Artist extends Entity {
35
	public ?string $name = null;
36
	public ?int $coverFileId = null;
37
	public ?string $mbid = null;
38
	public string $hash = '';
39
	public ?string $starred = null;
40
	public int $rating = 0;
41
42
	// not part of the standard content, injected separately when needed
43
	private ?string $lastfmUrl = null;
44
	/** @var ?Album[] $albums */
45
	private ?array $albums = null;
46
	/** @var ?Track[] $tracks */
47
	private ?array $tracks = null;
48
49
	public function __construct() {
50
		$this->addType('coverFileId', 'int');
51
		$this->addType('rating', 'int');
52
	}
53
54
	public function getLastfmUrl() : ?string {
55
		return $this->lastfmUrl;
56
	}
57
58
	public function setLastfmUrl(?string $lastfmUrl) : void {
59
		$this->lastfmUrl = $lastfmUrl;
60
	}
61
62
	/**
63
	 * @return ?Album[]
64
	 */
65
	public function getAlbums() : ?array {
66
		return $this->albums;
67
	}
68
69
	/**
70
	 * @param Album[] $albums
71
	 */
72
	public function setAlbums(array $albums) : void {
73
		$this->albums = $albums;
74
	}
75
76
	/**
77
	 * @return ?Track[]
78
	 */
79
	public function getTracks() : ?array {
80
		return $this->tracks;
81
	}
82
83
	/**
84
	 * @param Track[] $tracks
85
	 */
86
	public function setTracks(array $tracks) : void {
87
		$this->tracks = $tracks;
88
	}
89
90
	public function getUri(IURLGenerator $urlGenerator) : string {
91
		return $urlGenerator->linkToRoute(
92
			'music.shivaApi.artist',
93
			['id' => $this->id]
94
		);
95
	}
96
97
	public function getNameString(IL10N $l10n) : string {
98
		return $this->getName() ?: self::unknownNameString($l10n);
99
	}
100
101
	/**
102
	 * Return the cover URL to be used in the API
103
	 */
104
	public function coverToAPI(IURLGenerator $urlGenerator) : ?string {
105
		$coverUrl = null;
106
		if ($this->getCoverFileId() > 0) {
107
			$coverUrl = $urlGenerator->linkToRoute(
108
				'music.coverApi.artistCover',
109
				['artistId' => $this->getId()]
110
			);
111
		}
112
		return $coverUrl;
113
	}
114
115
	/**
116
	 * @param array $albums in the "toCollection" format
117
	 */
118
	public function toCollection(IURLGenerator $urlGenerator, IL10N $l10n, array $albums) : array {
119
		return [
120
			'id' => $this->getId(),
121
			'name' => $this->getNameString($l10n),
122
			'albums' => $albums,
123
			'cover' => $this->coverToAPI($urlGenerator)
124
		];
125
	}
126
127
	public function toShivaApi(IURLGenerator $urlGenerator, IL10N $l10n) : array {
128
		return [
129
			'id' => $this->getId(),
130
			'name' => $this->getNameString($l10n),
131
			'image' => $this->coverToAPI($urlGenerator),
132
			'slug' => $this->slugify('name'),
0 ignored issues
show
Deprecated Code introduced by
The function OCP\AppFramework\Db\Entity::slugify() has been deprecated: 24.0.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

132
			'slug' => /** @scrutinizer ignore-deprecated */ $this->slugify('name'),

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
133
			'uri' => $this->getUri($urlGenerator)
134
		];
135
	}
136
137
	public static function unknownNameString(IL10N $l10n) : string {
138
		return (string) $l10n->t('Unknown artist');
139
	}
140
}
141