From 76d99118e718eee930bfb8577d52f21f0ecb7d1a Mon Sep 17 00:00:00 2001 From: Joseph Date: Mon, 9 Oct 2023 13:15:15 +0100 Subject: [PATCH] Rework methods in Index class --- include/Index.php | 84 +++++++++++++++++++++-------------------------- 1 file changed, 38 insertions(+), 46 deletions(-) diff --git a/include/Index.php b/include/Index.php index 76994707..196d147d 100644 --- a/include/Index.php +++ b/include/Index.php @@ -19,7 +19,7 @@ class Index private string $feedId = ''; /** @var string $feedType Feed type (channel or playlist) */ - private string $feedType = 'channel'; + private string $feedType = ''; /** @var array $supportedTypes Supported feed types */ private array $supportedTypes = ['channel', 'playlist']; @@ -113,10 +113,12 @@ private function checkInputs(): void throw new Exception('Type parameter not given.'); } - if (in_array($_POST['type'], $this->supportedTypes)) { - $this->feedType = $_POST['type']; + if (in_array($_POST['type'], $this->supportedTypes) === false) { + throw new Exception('Unknown type parameter given.'); } + $this->feedType = $_POST['type']; + if ($_POST['type'] === 'url') { $this->fromUrl = true; @@ -157,68 +159,59 @@ private function generate(): void } if ($this->feedType === 'channel') { - $this->findChannel(); - } - - if ($this->feedType === 'playlist') { - $this->findPlaylist(); + $this->feedId = $this->findChannel($this->query); + } else { + $this->feedId = $this->findPlaylist($this->query); } } } /** * Find channel + * + * @param string $query + * @return string Channel Id + * + * @throws Exception if channel is not found */ - private function findChannel(): void + private function findChannel(string $query): string { - if (Validate::channelId($this->query) === true) { - $this->feedId = $this->query; - } else { - $this->searchApi($this->query); + if (Validate::channelId($query) === true) { + return $query; } - } - /** - * Find playlist - */ - private function findPlaylist(): void - { - if (Validate::playlistId($this->query) === true) { - $this->feedId = $this->query; - } else { - $this->searchApi($this->query); + $api = new Api(); + $response = $api->searchChannels($query); + + if (empty($response->items)) { + throw new Exception('Channel not found'); } + + return $response->items[0]->id->channelId; } /** - * Search YouTube data API for channel or playlist + * Find playlist * - * @param string $query Query string - * @throws Exception if the channel or playlist was not found + * @param string $query + * @return string Playlist Id + * + * @throws Exception if playlist is not found */ - private function searchApi(string $query): void + private function findPlaylist(string $query): string { - $api = new Api(); - - if ($this->feedType === 'channel') { - $response = $api->searchChannels($query); + if (Validate::playlistId($query) === true) { + return $query; } - if ($this->feedType === 'playlist') { - $response = $api->searchPlaylists($query); - } + $api = new Api(); + $response = $api->searchPlaylists($query); if (empty($response->items)) { - throw new Exception(ucfirst($this->feedType) . ' not found'); - } - - if ($this->feedType === 'channel') { - $this->feedId = $response->items['0']->id->channelId; + throw new Exception('Playlist not found'); } - if ($this->feedType === 'playlist') { - $this->feedId = $response->items['0']->id->playlistId; - } + return $response->items[0]->id->playlistId; } /** @@ -228,12 +221,11 @@ private function searchApi(string $query): void */ private function createFormatSelect(): string { - $html = ''; + return sprintf('', $options); } }