Passed
Push — master ( dc1961...4e74cd )
by Pauli
02:00
created

Genre::toAmpacheApi()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
c 0
b 0
f 0
dl 0
loc 10
rs 9.9666
cc 1
nc 1
nop 1
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, 2021
11
 */
12
13
namespace OCA\Music\Db;
14
15
use \OCP\IL10N;
16
use \OCP\AppFramework\Db\Entity;
17
18
/**
19
 * @method string getUserId()
20
 * @method void setUserId(string $userId)
21
 * @method string getName()
22
 * @method void setName(string $name)
23
 * @method string getLowerName()
24
 * @method void setLowerName(string $lowerName)
25
 * @method string getCreated()
26
 * @method setCreated(string $timestamp)
27
 * @method string getUpdated()
28
 * @method setUpdated(string $timestamp)
29
 * @method int getTrackCount()
30
 * @method void setTrackCount(int $count)
31
 * @method int getAlbumCount()
32
 * @method void setAlbumCount(int $count)
33
 * @method int getArtistCount()
34
 * @method void setArtistCount(int $count)
35
 * @method array getTrackIds()
36
 * @method void setTrackIds(array $trackIds)
37
 */
38
class Genre extends Entity {
39
	public $userId;
40
	public $name;
41
	public $lowerName;
42
	public $created;
43
	public $updated;
44
	// not from the music_genres table but still part of the standard content of this entity
45
	public $trackCount;
46
	public $albumCount;
47
	public $artistCount;
48
49
	// not part of the standard content, injected separately when needed
50
	public $trackIds;
51
52
	public function __construct() {
53
		$this->addType('trackCount', 'int');
54
		$this->addType('albumCount', 'int');
55
		$this->addType('artistCount', 'int');
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
			'stream' => 0
80
		];
81
	}
82
83
	public static function unknownNameString(IL10N $l10n) {
84
		return (string) $l10n->t('(Unknown genre)');
85
	}
86
}
87