Passed
Push — feature/909_Ampache_API_improv... ( a957d5...a4bfe6 )
by Pauli
15:10
created

Genre::unknownNameString()   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 1
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 Pauli Järvinen <[email protected]>
10
 * @copyright Pauli Järvinen 2020 - 2023
11
 */
12
13
namespace OCA\Music\Db;
14
15
use OCP\IL10N;
16
17
/**
18
 * @method string getName()
19
 * @method void setName(string $name)
20
 * @method string getLowerName()
21
 * @method void setLowerName(string $lowerName)
22
 *
23
 * @method int getTrackCount()
24
 * @method int getAlbumCount()
25
 * @method int getArtistCount()
26
 */
27
class Genre extends Entity {
28
	public $name;
29
	public $lowerName;
30
	// not from the music_genres table but still part of the standard content of this entity
31
	public $trackCount;
32
	public $albumCount;
33
	public $artistCount;
34
35
	// not part of the standard content, injected separately when needed
36
	public $trackIds;
37
38
	public function __construct() {
39
		$this->addType('trackCount', 'int');
40
		$this->addType('albumCount', 'int');
41
		$this->addType('artistCount', 'int');
42
	}
43
44
	/**
45
	 * @return ?int[]
46
	 */
47
	public function getTrackIds() : array {
48
		return $this->trackIds;
49
	}
50
51
	/**
52
	 * @param int[] $trackIds
53
	 */
54
	public function setTrackIds(array $trackIds) : void {
55
		$this->trackIds = $trackIds;
56
	}
57
58
	public function getNameString(IL10N $l10n) {
59
		return $this->getName() ?: self::unknownNameString($l10n);
60
	}
61
62
	public function toApi() {
63
		return  [
64
			'id' => $this->getId(),
65
			'name' => $this->getName(),
66
			'trackIds' => $this->trackIds ? \array_map('\intval', $this->trackIds) : []
67
		];
68
	}
69
70
	public function toAmpacheApi(IL10N $l10n) {
71
		return [
72
			'id' => (string)$this->getId(),
73
			'name' => $this->getNameString($l10n),
74
			'albums' => $this->getAlbumCount(),
75
			'artists' => $this->getArtistCount(),
76
			'songs' => $this->getTrackCount(),
77
			'videos' => 0,
78
			'playlists' => 0,
79
			'live_streams' => 0
80
		];
81
	}
82
83
	public static function unknownNameString(IL10N $l10n) {
84
		return (string) $l10n->t('(Unknown genre)');
85
	}
86
}
87