Issues (40)

lib/Db/PodcastChannel.php (1 issue)

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

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

145
			$createImageUrl = fn(/** @scrutinizer ignore-unused */ $e) => $this->getImageUrl();

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
146
			$result['podcast_episode'] = \array_map(fn($e) => $e->toAmpacheApi($createImageUrl, null), $this->episodes);
147
		}
148
149
		return $result;
150
	}
151
152
	public function toSubsonicApi() : array {
153
		$result = [
154
			'id' => 'podcast_channel-' . $this->getId(),
155
			'url' => $this->getRssUrl(),
156
			'title' => $this->getTitle(),
157
			'description' => $this->getDescription(),
158
			'coverArt' => 'podcast_channel-' . $this->getId(),
159
			'originalImageUrl' => $this->getImageUrl(),
160
			'status' => 'completed',
161
			'starred' => Util::formatZuluDateTime($this->getStarred())
162
		];
163
164
		if ($this->episodes !== null) {
165
			$result['episode'] = \array_map(fn($e) => $e->toSubsonicApi(), $this->episodes);
166
		}
167
168
		return $result;
169
	}
170
171
	/**
172
	 * Create URL which loads the channel image via the cloud server. The URL handles down-scaling and caching automatically.
173
	 */
174
	private function createImageUrl(IURLGenerator $urlGenerator) : string {
175
		return $urlGenerator->linkToRoute('music.coverApi.podcastCover', ['channelId' => $this->getId()]);
176
	}
177
}
178