Passed
Pull Request — master (#875)
by Pauli
04:37 queued 02:12
created

PodcastEpisode   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 68
dl 0
loc 90
rs 10
c 1
b 0
f 0
wmc 9

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A toAmpacheApi() 0 16 2
A getYear() 0 6 3
A toSubsonicApi() 0 26 2
A toApi() 0 6 1
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 getStarred()
52
 * @method void setStarred(string $timestamp)
53
 * @method string getCreated()
54
 * @method void setCreated(string $timestamp)
55
 * @method string getUpdated()
56
 * @method void setUpdated(string $timestamp)
57
 */
58
class PodcastEpisode extends Entity {
59
	public $userId;
60
	public $channelId;
61
	public $streamUrl;
62
	public $mimetype;
63
	public $size;
64
	public $duration;
65
	public $guid;
66
	public $guidHash;
67
	public $title;
68
	public $episode;
69
	public $linkUrl;
70
	public $published;
71
	public $keywords;
72
	public $copyright;
73
	public $author;
74
	public $description;
75
	public $starred;
76
	public $created;
77
	public $updated;
78
79
	public function __construct() {
80
		$this->addType('channelId', 'int');
81
		$this->addType('size', 'int');
82
		$this->addType('duration', 'int');
83
		$this->addType('episode', 'int');
84
	}
85
86
	public function toApi() : array {
87
		return [
88
			'id' => $this->getId(),
89
			'title' => $this->getTitle(),
90
			'stream_url' => $this->getStreamUrl(),
91
			'mimetype' => $this->getMimetype()
92
		];
93
	}
94
95
	public function toAmpacheApi() : array {
96
		return [
97
			'id' => (string)$this->getId(),
98
			'name' => $this->getTitle(),
99
			'title' => $this->getTitle(),
100
			'description' => $this->getDescription(),
101
			'author' => $this->getAuthor(),
102
			'author_full' => $this->getAuthor(),
103
			'website' => $this->getLinkUrl(),
104
			'pubdate' => $this->getPublished(), // TODO: format?
105
			'state' => 'Completed',
106
			'filelength' => Util::formatTime($this->getDuration()),
107
			'filesize' => Util::formatFileSize($this->getSize(), 2) . 'B',
108
			'mime' => $this->getMimetype(),
109
			'url' => $this->getStreamUrl(),
110
			'flag' => empty($this->getStarred()) ? 0 : 1,
111
		];
112
	}
113
114
	public function toSubsonicApi() : array {
115
		$result = [
116
			'id' => 'podcast_episode-' . $this->getId(),
117
			'streamId' => 'podcast_episode-' . $this->getId(),
118
			'channelId' => 'podcast_channel-' . $this->getChannelId(),
119
			'title' => $this->getTitle(),
120
			'description' => $this->getDescription(),
121
			'publishDate' => Util::formatZuluDateTime($this->getPublished()),
122
			'status' => 'completed',
123
			'parent' => $this->getChannelId(),
124
			'isDir' => false,
125
			'year' => $this->getYear(),
126
			'genre' => 'Podcast',
127
			'coverArt' => 'podcast_channel-' . $this->getChannelId(),
128
			'size' => $this->getSize(),
129
			'contentType' => $this->getMimetype(),
130
			//'suffix' => 'mp3',
131
			'duration' => $this->getDuration(),
132
			//'bitRate' => 128
133
		];
134
135
		if (!empty($this->starred)) {
136
			$result['starred'] = Util::formatZuluDateTime($this->starred);
137
		}
138
139
		return $result;
140
	}
141
142
	public function getYear() : ?int {
143
		$matches = null;
144
		if (\is_string($this->published) && \preg_match('/^(\d\d\d\d)-\d\d-\d\d.*/', $this->published, $matches) === 1) {
145
			return (int)$matches[1];
146
		} else {
147
			return null;
148
		}
149
	}
150
}
151