Skip to content

Commit

Permalink
Rework methods in Index class
Browse files Browse the repository at this point in the history
  • Loading branch information
VerifiedJoseph committed Oct 9, 2023
1 parent 47416b9 commit 76d9911
Showing 1 changed file with 38 additions and 46 deletions.
84 changes: 38 additions & 46 deletions include/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<int, string> $supportedTypes Supported feed types */
private array $supportedTypes = ['channel', 'playlist'];
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}

/**
Expand All @@ -228,12 +221,11 @@ private function searchApi(string $query): void
*/
private function createFormatSelect(): string
{
$html = '<select name="format">';

$options = '';
foreach (Config::getFeedFormats() as $format) {
$html .= sprintf('<option value="%s">%s</option>', $format, strtoupper($format));
$options .= sprintf('<option value="%s">%s</option>', $format, strtoupper($format));
}

return $html .= '</select>';
return sprintf('<select name="format">%s</select>', $options);
}
}

0 comments on commit 76d9911

Please sign in to comment.