Passed
Push — feature/909_Ampache_API_improv... ( 943762...5a3d3f )
by Pauli
12:26
created

Bookmark::getNameString()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
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 Gavin E <[email protected]>
10
 * @author Pauli Järvinen <[email protected]>
11
 * @copyright Gavin E 2020
12
 * @copyright Pauli Järvinen 2020 - 2023
13
 */
14
15
namespace OCA\Music\Db;
16
17
use OCA\Music\Utility\Util;
18
19
use OCP\IL10N;
20
21
/**
22
 * @method int getType()
23
 * @method void setType(int $type)
24
 * @method int getEntryId()
25
 * @method void setEntryId(int $entryId)
26
 * @method int getPosition()
27
 * @method void setPosition(int $position)
28
 * @method ?string getComment()
29
 * @method void setComment(?string $comment)
30
 */
31
class Bookmark extends Entity {
32
	public $type;
33
	public $entryId;
34
	public $position;
35
	public $comment;
36
37
	public const TYPE_TRACK = 1;
38
	public const TYPE_PODCAST_EPISODE = 2;
39
40
	public function __construct() {
41
		$this->addType('type', 'int');
42
		$this->addType('entryId', 'int');
43
		$this->addType('position', 'int');
44
	}
45
46
	public function getNameString(IL10N $l10n) : string {
47
		return $this->getComment() ?: (string)$l10n->t('(no comment)');
48
	}
49
50
	public function toSubsonicApi() : array {
51
		return [
52
			'position' => $this->getPosition(),
53
			'username' => $this->getUserId(),
54
			'comment' => $this->getComment() ?: '',
55
			'created' => Util::formatZuluDateTime($this->getCreated()),
56
			'changed' => Util::formatZuluDateTime($this->getUpdated())
57
		];
58
	}
59
60
	public function toAmpacheApi() : array {
61
		return [
62
			'id' => (string)$this->getId(),
63
			'owner' => $this->getUserId(),
64
			'object_type' => ($this->getType() == self::TYPE_TRACK) ? 'song' : 'podcast_episode',
65
			'object_id' => (string)$this->getEntryId(),
66
			'position' => (int)($this->getPosition() / 1000), // millisecods to seconds
67
			'client' => $this->getComment(),
68
			'creation_date' => Util::formatDateTimeUtcOffset($this->getCreated()),
69
			'update_date' => Util::formatDateTimeUtcOffset($this->getUpdated())
70
		];
71
	}
72
}
73