Passed
Push — feature/786_podcasts ( 7b8be7...af6910 )
by Pauli
02:22
created

PodcastEpisode::toSubsonicApi()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 16
nc 1
nop 0
dl 0
loc 18
rs 9.7333
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 int getChannelId()
22
 * @method void setChannelId(int $id)
23
 * @method string getStreamUrl()
24
 * @method setStreamUrl(string $url)
25
 * @method string getMimetype()
26
 * @method setMimetype(string $mime)
27
 * @method int getSize()
28
 * @method void setSize(int $size)
29
 * @method int getDuration()
30
 * @method void setDuration(int $duration)
31
 * @method string getGuid()
32
 * @method void setGuid(string $guid)
33
 * @method string getGuidHash()
34
 * @method void setGuidHash(string $guidHash)
35
 * @method string getTitle()
36
 * @method void setTitle(string $title)
37
 * @method int getEpisode()
38
 * @method void setEpisode(int $episode)
39
 * @method string getLinkUrl()
40
 * @method void setLinkUrl(string $url)
41
 * @method string getPublished()
42
 * @method void setPublished(string $timestamp)
43
 * @method string getKeywords()
44
 * @method void setKeywords(string $keywords)
45
 * @method string getCopyright()
46
 * @method void setCopyright(string $copyright)
47
 * @method string getAuthor()
48
 * @method void setAuthor(string $author)
49
 * @method string getDescription()
50
 * @method void setDescription(string $description)
51
 * @method string getCreated()
52
 * @method void setCreated(string $timestamp)
53
 * @method string getUpdated()
54
 * @method void setUpdated(string $timestamp)
55
 */
56
class PodcastEpisode extends Entity {
57
	public $userId;
58
	public $channelId;
59
	public $streamUrl;
60
	public $mimetype;
61
	public $size;
62
	public $duration;
63
	public $guid;
64
	public $guidHash;
65
	public $title;
66
	public $episode;
67
	public $linkUrl;
68
	public $published;
69
	public $keywords;
70
	public $copyright;
71
	public $author;
72
	public $description;
73
	public $created;
74
	public $updated;
75
76
	public function __construct() {
77
		$this->addType('channelId', 'int');
78
		$this->addType('size', 'int');
79
		$this->addType('duration', 'int');
80
		$this->addType('episode', 'int');
81
	}
82
83
	public function toApi() : array {
84
		return [
85
			'id' => $this->getId(),
86
			'title' => $this->getTitle(),
87
			'stream_url' => $this->getStreamUrl(),
88
			'mimetype' => $this->getMimetype()
89
		];
90
	}
91
92
	public function toAmpacheApi() : array {
93
		return [
94
			'id' => (string)$this->getId(),
95
			'name' => $this->getTitle(),
96
			'title' => $this->getTitle(),
97
			'description' => $this->getDescription(),
98
			'author' => $this->getAuthor(),
99
			'author_full' => $this->getAuthor(),
100
			'website' => $this->getLinkUrl(),
101
			'pubdate' => $this->getPublished(), // TODO: format?
102
			'state' => 'Completed',
103
			'filelength' => Util::formatTime($this->getDuration()),
104
			'filesize' => Util::formatFileSize($this->getSize(), 2),
105
			'mime' => $this->getMimetype(),
106
			'url' => $this->getStreamUrl()
107
		];
108
	}
109
110
	public function toSubsonicApi() : array {
111
		return [
112
			'id' => 'podcast_episode-' . $this->getId(),
113
			'streamId' => 'podcast_episode-' . $this->getId(),
114
			'channelId' => 'podcast_channel-' . $this->getChannelId(),
115
			'title' => $this->getTitle(),
116
			'description' => $this->getDescription(),
117
			'publishDate' => Util::formatZuluDateTime($this->getPublished()),
118
			'status' => 'completed',
119
			'parent' => $this->getChannelId(),
120
			'isDir' => false,
121
			'year' => $this->getYear(),
122
			'genre' => 'Podcast',
123
			'coverArt' => 'podcast_channel-' . $this->getChannelId(),
124
			'size' => $this->getSize(),
125
			'contentType' => $this->getMimetype(),
126
			//'suffix' => 'mp3',
127
			'duration' => $this->getDuration(),
128
			//'bitRate' => 128
129
		];
130
	}
131
132
	public function getYear() : ?int {
133
		$matches = null;
134
		if (\is_string($this->published) && \preg_match('/^(\d\d\d\d)-\d\d-\d\d.*/', $this->published, $matches) === 1) {
135
			return (int)$matches[1];
136
		} else {
137
			return null;
138
		}
139
	}
140
}
141