Passed
Push — feature/909_Ampache_API_improv... ( a84036...a957d5 )
by Pauli
03:28
created

PodcastChannel::__construct()   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 0
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 2021 - 2023
11
 */
12
13
namespace OCA\Music\Db;
14
15
use OCA\Music\Utility\Util;
16
17
/**
18
 * @method string getRssUrl()
19
 * @method void setRssUrl(string $url)
20
 * @method string getRssHash()
21
 * @method void setRssHash(string $hash)
22
 * @method string getContentHash()
23
 * @method void setContentHash(string $hash)
24
 * @method string getUpdateChecked()
25
 * @method void setUpdateChecked(string $timestamp)
26
 * @method string getPublished()
27
 * @method void setPublished(string $timestamp)
28
 * @method string getLastBuildDate()
29
 * @method void setLastBuildDate(string $timestamp)
30
 * @method string getTitle()
31
 * @method void setTitle(string $title)
32
 * @method string getLinkUrl()
33
 * @method void setLinkUrl(string $url)
34
 * @method string getLanguage()
35
 * @method void setLanguage(string $language)
36
 * @method string getCopyright()
37
 * @method void setCopyright(string $copyright)
38
 * @method string getAuthor()
39
 * @method void setAuthor(string $author)
40
 * @method string getDescription()
41
 * @method void setDescription(string $description)
42
 * @method string getImageUrl()
43
 * @method void setImageUrl(string $url)
44
 * @method string getCategory()
45
 * @method void setCategory(string $category)
46
 * @method string getStarred()
47
 * @method void setStarred(string $timestamp)
48
 * @method ?int getRating()
49
 * @method setRating(?int $rating)
50
 */
51
class PodcastChannel extends Entity {
52
	public $rssUrl;
53
	public $rssHash;
54
	public $contentHash;
55
	public $updateChecked;
56
	public $published;
57
	public $lastBuildDate;
58
	public $title;
59
	public $linkUrl;
60
	public $language;
61
	public $copyright;
62
	public $author;
63
	public $description;
64
	public $imageUrl;
65
	public $category;
66
	public $starred;
67
	public $rating;
68
69
	// not part of the default content, may be injected separately
70
	private $episodes;
71
72
	public function __construct() {
73
		$this->addType('rating', 'int');
74
	}
75
76
	/**
77
	 * @return ?PodcastEpisode[]
78
	 */
79
	public function getEpisodes() : array {
80
		return $this->episodes;
81
	}
82
83
	/**
84
	 * @param PodcastEpisode[] $episodes
85
	 */
86
	public function setEpisodes(array $episodes) : void {
87
		$this->episodes = $episodes;
88
	}
89
90
	public function toApi() : array {
91
		$result = [
92
			'id' => $this->getId(),
93
			'title' => $this->getTitle(),
94
			'image' => $this->getImageUrl(),
95
			'hash' => $this->getContentHash()
96
		];
97
98
		if ($this->episodes !== null) {
99
			$result['episodes'] = Util::arrayMapMethod($this->episodes, 'toApi');
100
		}
101
102
		return $result;
103
	}
104
105
	public function detailsToApi() : array {
106
		return [
107
			'id' => $this->getId(),
108
			'title' => $this->getTitle(),
109
			'description' => $this->getDescription(),
110
			'image' => $this->getImageUrl(),
111
			'link_url' =>  $this->getLinkUrl(),
112
			'rss_url' => $this->getRssUrl(),
113
			'language' => $this->getLanguage(),
114
			'copyright' => $this->getCopyright(),
115
			'author' => $this->getAuthor(),
116
			'category' => $this->getCategory(),
117
			'published' => $this->getPublished(),
118
			'last_build_date' => $this->getLastBuildDate(),
119
			'update_checked' => $this->getUpdateChecked(),
120
		];
121
	}
122
123
	public function toAmpacheApi() : array {
124
		$result = [
125
			'id' => (string)$this->getId(),
126
			'name' => $this->getTitle(),
127
			'description' => $this->getDescription(),
128
			'language' => $this->getLanguage(),
129
			'copyright' => $this->getCopyright(),
130
			'feed_url' => $this->getRssUrl(),
131
			'build_date' => Util::formatDateTimeUtcOffset($this->getLastBuildDate()),
132
			'sync_date' => Util::formatDateTimeUtcOffset($this->getUpdateChecked()),
133
			'public_url' => $this->getLinkUrl(),
134
			'website' => $this->getLinkUrl(),
135
			'art' => $this->getImageUrl(),
136
			'flag' => !empty($this->getStarred()),
137
			'rating' => $this->getRating() ?? 0,
138
			'preciserating' => $this->getRating() ?? 0,
139
		];
140
141
		if ($this->episodes !== null) {
142
			$result['podcast_episode'] = Util::arrayMapMethod($this->episodes, 'toAmpacheApi');
143
		}
144
145
		return $result;
146
	}
147
148
	public function toSubsonicApi() : array {
149
		$result = [
150
			'id' => 'podcast_channel-' . $this->getId(),
151
			'url' => $this->getRssUrl(),
152
			'title' => $this->getTitle(),
153
			'description' => $this->getDescription(),
154
			'coverArt' => 'podcast_channel-' . $this->getId(),
155
			'originalImageUrl' => $this->getImageUrl(),
156
			'status' => 'completed',
157
			'starred' => Util::formatZuluDateTime($this->getStarred())
158
		];
159
160
		if ($this->episodes !== null) {
161
			$result['episode'] = Util::arrayMapMethod($this->episodes, 'toSubsonicApi');
162
		}
163
164
		return $result;
165
	}
166
}
167