Passed
Pull Request — master (#875)
by Pauli
02:43
created

PodcastChannel::toSubsonicApi()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

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