@@ -12,49 +12,49 @@ class Youtube
12
12
{
13
13
/**
14
14
* Application Container
15
- *
15
+ *
16
16
* @var Application
17
17
*/
18
18
private $ app ;
19
19
20
20
/**
21
21
* Google Client
22
- *
22
+ *
23
23
* @var \Google_Client
24
24
*/
25
25
protected $ client ;
26
26
27
27
/**
28
28
* Google YouTube Service
29
- *
29
+ *
30
30
* @var \Google_Service_YouTube
31
31
*/
32
32
protected $ youtube ;
33
33
34
34
/**
35
35
* Video ID
36
- *
36
+ *
37
37
* @var string
38
38
*/
39
39
private $ videoId ;
40
40
41
41
/**
42
42
* Video Snippet
43
- *
43
+ *
44
44
* @var array
45
45
*/
46
46
private $ snippet ;
47
47
48
48
/**
49
49
* Thumbnail URL
50
- *
50
+ *
51
51
* @var string
52
52
*/
53
53
private $ thumbnailUrl ;
54
54
55
55
/**
56
56
* Constructor
57
- *
57
+ *
58
58
* @param \Google_Client $client
59
59
*/
60
60
public function __construct ($ app , Google_Client $ client )
@@ -69,14 +69,15 @@ public function __construct($app, Google_Client $client)
69
69
$ this ->client ->setAccessToken ($ accessToken );
70
70
}
71
71
}
72
-
72
+
73
73
/**
74
74
* Upload the video to YouTube
75
- *
75
+ *
76
76
* @param string $path
77
- * @param array $data
77
+ * @param array $data
78
78
* @param string $privacyStatus
79
79
* @return string
80
+ * @throws Exception
80
81
*/
81
82
public function upload ($ path , array $ data = [], $ privacyStatus = 'public ' )
82
83
{
@@ -87,22 +88,7 @@ public function upload($path, array $data = [], $privacyStatus = 'public')
87
88
$ this ->handleAccessToken ();
88
89
89
90
try {
90
- // Setup the Snippet
91
- $ snippet = new \Google_Service_YouTube_VideoSnippet ();
92
-
93
- if (array_key_exists ('title ' , $ data )) $ snippet ->setTitle ($ data ['title ' ]);
94
- if (array_key_exists ('description ' , $ data )) $ snippet ->setDescription ($ data ['description ' ]);
95
- if (array_key_exists ('tags ' , $ data )) $ snippet ->setTags ($ data ['tags ' ]);
96
- if (array_key_exists ('category_id ' , $ data )) $ snippet ->setCategoryId ($ data ['category_id ' ]);
97
-
98
- // Set the Privacy Status
99
- $ status = new \Google_Service_YouTube_VideoStatus ();
100
- $ status ->privacyStatus = $ privacyStatus ;
101
-
102
- // Set the Snippet & Status
103
- $ video = new \Google_Service_YouTube_Video ();
104
- $ video ->setSnippet ($ snippet );
105
- $ video ->setStatus ($ status );
91
+ $ video = $ this ->getVideo ($ data , $ privacyStatus );
106
92
107
93
// Set the Chunk Size
108
94
$ chunkSize = 1 * 1024 * 1024 ;
@@ -155,11 +141,47 @@ public function upload($path, array $data = [], $privacyStatus = 'public')
155
141
}
156
142
157
143
/**
158
- * Set a Custom Thumbnail for the Upload
144
+ * Update the video on YouTube
159
145
*
160
- * @param string $imagePath
146
+ * @param string $id
147
+ * @param array $data
148
+ * @param string $privacyStatus
149
+ * @return string
150
+ * @throws Exception
151
+ */
152
+ public function update ($ id , array $ data = [], $ privacyStatus = 'public ' )
153
+ {
154
+ $ this ->handleAccessToken ();
155
+
156
+ if (!$ this ->exists ($ id )) {
157
+ throw new Exception ('A video matching id " ' . $ id .'" could not be found. ' );
158
+ }
159
+
160
+ try {
161
+ $ video = $ this ->getVideo ($ data , $ privacyStatus , $ id );
162
+
163
+ $ status = $ this ->youtube ->videos ->update ('status,snippet ' , $ video );
164
+
165
+ // Set ID of the Updated Video
166
+ $ this ->videoId = $ status ['id ' ];
167
+
168
+ // Set the Snippet from Updated Video
169
+ $ this ->snippet = $ status ['snippet ' ];
170
+ } catch (\Google_Service_Exception $ e ) {
171
+ throw new Exception ($ e ->getMessage ());
172
+ } catch (\Google_Exception $ e ) {
173
+ throw new Exception ($ e ->getMessage ());
174
+ }
175
+
176
+ return $ this ;
177
+ }
178
+
179
+ /**
180
+ * Set a Custom Thumbnail for the Upload
161
181
*
182
+ * @param string $imagePath
162
183
* @return self
184
+ * @throws Exception
163
185
*/
164
186
public function withThumbnail ($ imagePath )
165
187
{
@@ -207,9 +229,9 @@ public function withThumbnail($imagePath)
207
229
/**
208
230
* Delete a YouTube video by it's ID.
209
231
*
210
- * @param int $id
211
- *
232
+ * @param int $id
212
233
* @return bool
234
+ * @throws Exception
213
235
*/
214
236
public function delete ($ id )
215
237
{
@@ -222,6 +244,39 @@ public function delete($id)
222
244
return $ this ->youtube ->videos ->delete ($ id );
223
245
}
224
246
247
+ /**
248
+ * @param $data
249
+ * @param $privacyStatus
250
+ * @param null $id
251
+ * @return \Google_Service_YouTube_Video
252
+ */
253
+ private function getVideo ($ data , $ privacyStatus , $ id = null )
254
+ {
255
+ // Setup the Snippet
256
+ $ snippet = new \Google_Service_YouTube_VideoSnippet ();
257
+
258
+ if (array_key_exists ('title ' , $ data )) $ snippet ->setTitle ($ data ['title ' ]);
259
+ if (array_key_exists ('description ' , $ data )) $ snippet ->setDescription ($ data ['description ' ]);
260
+ if (array_key_exists ('tags ' , $ data )) $ snippet ->setTags ($ data ['tags ' ]);
261
+ if (array_key_exists ('category_id ' , $ data )) $ snippet ->setCategoryId ($ data ['category_id ' ]);
262
+
263
+ // Set the Privacy Status
264
+ $ status = new \Google_Service_YouTube_VideoStatus ();
265
+ $ status ->privacyStatus = $ privacyStatus ;
266
+
267
+ // Set the Snippet & Status
268
+ $ video = new \Google_Service_YouTube_Video ();
269
+ if ($ id )
270
+ {
271
+ $ video ->setId ($ id );
272
+ }
273
+
274
+ $ video ->setSnippet ($ snippet );
275
+ $ video ->setStatus ($ status );
276
+
277
+ return $ video ;
278
+ }
279
+
225
280
/**
226
281
* Check if a YouTube video exists by it's ID.
227
282
*
@@ -273,8 +328,9 @@ public function getThumbnailUrl()
273
328
/**
274
329
* Setup the Google Client
275
330
*
276
- * @param \Google_Client $client
277
- * @return \Google_Client $client
331
+ * @param Google_Client $client
332
+ * @return Google_Client $client
333
+ * @throws Exception
278
334
*/
279
335
private function setup (Google_Client $ client )
280
336
{
@@ -291,7 +347,7 @@ private function setup(Google_Client $client)
291
347
$ client ->setAccessType ('offline ' );
292
348
$ client ->setApprovalPrompt ('force ' );
293
349
$ client ->setRedirectUri (url (
294
- $ this ->app ->config ->get ('youtube.routes.prefix ' )
350
+ $ this ->app ->config ->get ('youtube.routes.prefix ' )
295
351
. '/ ' .
296
352
$ this ->app ->config ->get ('youtube.routes.redirect_uri ' )
297
353
));
@@ -314,7 +370,7 @@ public function saveAccessTokenToDB($accessToken)
314
370
315
371
/**
316
372
* Get the latest access token from the database.
317
- *
373
+ *
318
374
* @return string
319
375
*/
320
376
public function getLatestAccessTokenFromDB ()
@@ -328,7 +384,7 @@ public function getLatestAccessTokenFromDB()
328
384
329
385
/**
330
386
* Handle the Access Token
331
- *
387
+ *
332
388
* @return void
333
389
*/
334
390
public function handleAccessToken ()
0 commit comments