From 3d06b8faee539cdc9cd38043fbd2b6816a5ef078 Mon Sep 17 00:00:00 2001 From: Bahaa Alhagar Date: Sun, 26 Aug 2018 19:06:38 +0200 Subject: [PATCH] fixed addVideoToPlaylist method --- src/YoutubeUploader.php | 75 +++++++++++++++++++++++------------------ 1 file changed, 43 insertions(+), 32 deletions(-) diff --git a/src/YoutubeUploader.php b/src/YoutubeUploader.php index 3210294..1b6011e 100644 --- a/src/YoutubeUploader.php +++ b/src/YoutubeUploader.php @@ -133,7 +133,6 @@ public function upload($path, array $data = [], $privacyStatus = 'public') // Set the Snippet from Uploaded Video $this->snippet = $status['snippet']; - } catch (\Google_Service_Exception $e) { throw new Exception($e->getMessage()); } catch (\Google_Exception $e) { @@ -219,7 +218,6 @@ public function withThumbnail($imagePath) $this->client->setDefer(false); $this->thumbnailUrl = $status['items'][0]['default']['url']; - } catch (\Google_Service_Exception $e) { throw new Exception($e->getMessage()); } catch (\Google_Exception $e) { @@ -278,10 +276,12 @@ public function createPlaylist($title, $description, $privacyStatus = 'public') // 4. Call the playlists.insert method to create the playlist. The API // response will contain information about the new playlist. - $playlistResponse = $this->youtube->playlists->insert('snippet,status', - $youTubePlaylist, array()); + $playlistResponse = $this->youtube->playlists->insert( + 'snippet,status', + $youTubePlaylist, + array() + ); $this->playlistId = $playlistResponse['id']; - } catch (\Google_Service_Exception $e) { throw new Exception($e->getMessage()); } catch (\Google_Exception $e) { @@ -293,26 +293,23 @@ public function createPlaylist($title, $description, $privacyStatus = 'public') /** * [addVideoToPlaylist add Video to youtube playlist] - * @param [string] $playlistId - * @param [string] $videoId - * @param [string] $title + * @param [string] $playlistId + * @param [string] $videoId + * @param [string] $title */ - public function addVideoToPlaylist(array $video[], $playlistId = null) + public function addVideoToPlaylist(array $video = [], $playlistId = null) { $playlistId = $playlistId ? $playlistId : $this->$playlistId; - if (!$playlistId) - { + if (!$playlistId) { throw new NotDefinedPlaylistException("please provide playlist ID"); } - if (!isset($video['id'])) - { + if (!isset($video['id'])) { throw new Exception('Please provide a Video youtube id.'); } - if (!$this->exists($video['id'])) - { + if (!$this->exists($video['id'])) { throw new Exception('A video matching id "'. $video['id'] .'" could not be found.'); } @@ -329,7 +326,12 @@ public function addVideoToPlaylist(array $video[], $playlistId = null) // title if you want to display a different value than the title of the // video being added. Add the resource ID and the playlist ID retrieved $playlistItemSnippet = new \Google_Service_YouTube_PlaylistItemSnippet(); - isset($video['title']) ?: $playlistItemSnippet->setTitle($video['title']); + + + if (isset($video['title'])) { + $playlistItemSnippet->setTitle($video['title']); + } + $playlistItemSnippet->setPlaylistId($playlistId); $playlistItemSnippet->setResourceId($resourceId); @@ -339,13 +341,15 @@ public function addVideoToPlaylist(array $video[], $playlistId = null) $playlistItem = new \Google_Service_YouTube_PlaylistItem(); $playlistItem->setSnippet($playlistItemSnippet); $playlistItemResponse = $this->youtube->playlistItems->insert( - 'snippet,contentDetails', $playlistItem, array()); - + 'snippet,contentDetails', + $playlistItem, + array() + ); } catch (\Google_Service_Exception $e) { throw new Exception($e->getMessage()); } catch (\Google_Exception $e) { throw new Exception($e->getMessage()); - } + } return $playlistItemResponse; } @@ -362,10 +366,18 @@ private function getVideo($data, $privacyStatus, $id = null) // Setup the Snippet $snippet = new \Google_Service_YouTube_VideoSnippet(); - if (array_key_exists('title', $data)) $snippet->setTitle($data['title']); - if (array_key_exists('description', $data)) $snippet->setDescription($data['description']); - if (array_key_exists('tags', $data)) $snippet->setTags($data['tags']); - if (array_key_exists('category_id', $data)) $snippet->setCategoryId($data['category_id']); + if (array_key_exists('title', $data)) { + $snippet->setTitle($data['title']); + } + if (array_key_exists('description', $data)) { + $snippet->setDescription($data['description']); + } + if (array_key_exists('tags', $data)) { + $snippet->setTags($data['tags']); + } + if (array_key_exists('category_id', $data)) { + $snippet->setCategoryId($data['category_id']); + } // Set the Privacy Status $status = new \Google_Service_YouTube_VideoStatus(); @@ -373,8 +385,7 @@ private function getVideo($data, $privacyStatus, $id = null) // Set the Snippet & Status $video = new \Google_Service_YouTube_Video(); - if ($id) - { + if ($id) { $video->setId($id); } @@ -397,7 +408,9 @@ public function exists($id) $response = $this->youtube->videos->listVideos('status', ['id' => $id]); - if (empty($response->items)) return false; + if (empty($response->items)) { + return false; + } return true; } @@ -452,7 +465,7 @@ public function getPlaylistId() */ private function setup(Google_Client $client) { - if( + if ( !$this->app->config->get('youtubeUploader.client_id') || !$this->app->config->get('youtubeUploader.client_secret') ) { @@ -497,7 +510,7 @@ public function getLatestAccessTokenFromDB() ->latest('created_at') ->first(); - return $latest ? (is_array($latest) ? $latest['access_token'] : $latest->access_token ) : null; + return $latest ? (is_array($latest) ? $latest['access_token'] : $latest->access_token) : null; } /** @@ -511,11 +524,9 @@ public function handleAccessToken() throw new \Exception('An access token is required.'); } - if($this->client->isAccessTokenExpired()) - { + if ($this->client->isAccessTokenExpired()) { // If we have a "refresh_token" - if (array_key_exists('refresh_token', $accessToken)) - { + if (array_key_exists('refresh_token', $accessToken)) { // Refresh the access token $this->client->refreshToken($accessToken['refresh_token']);