11
11
12
12
use RicardoFiorani \Adapter \AbstractServiceAdapter ;
13
13
use RicardoFiorani \Exception \InvalidThumbnailSizeException ;
14
+ use RicardoFiorani \Exception \ServiceApiNotAvailable ;
14
15
use RicardoFiorani \Renderer \EmbedRendererInterface ;
15
16
16
17
class VimeoServiceAdapter extends AbstractServiceAdapter
17
18
{
18
-
19
19
const THUMBNAIL_SMALL = 'thumbnail_small ' ;
20
20
const THUMBNAIL_MEDIUM = 'thumbnail_medium ' ;
21
21
const THUMBNAIL_LARGE = 'thumbnail_large ' ;
22
22
23
+ /**
24
+ * @var string
25
+ */
26
+ public $ title ;
27
+
28
+ /**
29
+ * @var string
30
+ */
31
+ public $ description ;
32
+
33
+ /**
34
+ * @var array
35
+ */
36
+ public $ thumbnails ;
23
37
24
38
/**
25
39
* @param string $url
@@ -28,49 +42,22 @@ class VimeoServiceAdapter extends AbstractServiceAdapter
28
42
*/
29
43
public function __construct ($ url , $ pattern , EmbedRendererInterface $ renderer )
30
44
{
31
- $ match = array ();
32
- preg_match ($ pattern , $ url , $ match );
33
- /*Gets the Video ID*/
34
- $ videoId = $ match [2 ];
35
- if (empty ($ videoId )) {
36
- $ videoId = $ match [1 ];
37
- }
38
-
45
+ $ videoId = $ this ->getVideoIdByPattern ($ url , $ pattern );
39
46
$ this ->setVideoId ($ videoId );
40
-
41
- /*Sends the video ID to the API to get the thumbnails and other infos*/
42
- $ hash = unserialize (@file_get_contents ("http://vimeo.com/api/v2/video/ " . $ this ->getVideoId () . ".php " ));
43
- $ data = $ hash [0 ];
44
-
47
+ $ videoData = $ this ->getVideoDataFromServiceApi ();
45
48
46
49
$ this ->setThumbnails (array (
47
- self ::THUMBNAIL_SMALL => $ data [self ::THUMBNAIL_SMALL ],
48
- self ::THUMBNAIL_MEDIUM => $ data [self ::THUMBNAIL_MEDIUM ],
49
- self ::THUMBNAIL_LARGE => $ data [self ::THUMBNAIL_LARGE ],
50
+ self ::THUMBNAIL_SMALL => $ videoData [self ::THUMBNAIL_SMALL ],
51
+ self ::THUMBNAIL_MEDIUM => $ videoData [self ::THUMBNAIL_MEDIUM ],
52
+ self ::THUMBNAIL_LARGE => $ videoData [self ::THUMBNAIL_LARGE ],
50
53
));
51
54
52
- $ this ->setTitle ($ data ['title ' ]);
53
- $ this ->setDescription ($ data ['description ' ]);
55
+ $ this ->setTitle ($ videoData ['title ' ]);
56
+ $ this ->setDescription ($ videoData ['description ' ]);
54
57
55
58
return parent ::__construct ($ url , $ pattern , $ renderer );
56
59
}
57
60
58
- /**
59
- * @var string
60
- */
61
- public $ title ;
62
-
63
- /**
64
- * @var string
65
- */
66
- public $ description ;
67
-
68
- /**
69
- * @var array
70
- */
71
- public $ thumbnails ;
72
-
73
-
74
61
/**
75
62
* Returns the service name (ie: "Youtube" or "Vimeo")
76
63
* @return string
@@ -80,7 +67,6 @@ public function getServiceName()
80
67
return 'Vimeo ' ;
81
68
}
82
69
83
-
84
70
/**
85
71
* Returns if the service has a thumbnail image
86
72
* @return bool
@@ -90,7 +76,6 @@ public function hasThumbnail()
90
76
return false == empty ($ this ->thumbnails );
91
77
}
92
78
93
-
94
79
/**
95
80
* @return string
96
81
*/
@@ -139,7 +124,6 @@ public function setThumbnails($thumbnails)
139
124
$ this ->thumbnails = $ thumbnails ;
140
125
}
141
126
142
-
143
127
/**
144
128
* @param string $size
145
129
* @return string
@@ -176,7 +160,6 @@ public function getThumbNailSizes()
176
160
);
177
161
}
178
162
179
-
180
163
/**
181
164
* Returns the small thumbnail's url
182
165
* @return string
@@ -220,4 +203,35 @@ public function isEmbeddable()
220
203
{
221
204
return true ;
222
205
}
206
+
207
+ /**
208
+ * @param string $url
209
+ * @param string $pattern
210
+ * @return int
211
+ */
212
+ private function getVideoIdByPattern ($ url , $ pattern )
213
+ {
214
+ $ match = array ();
215
+ preg_match ($ pattern , $ url , $ match );
216
+ $ videoId = $ match [2 ];
217
+
218
+ return $ videoId ;
219
+ }
220
+
221
+ /**
222
+ * Uses the Vimeo video API to get video info
223
+ * @todo make this better by using guzzle
224
+ * @return array
225
+ * @throws ServiceApiNotAvailable
226
+ */
227
+ private function getVideoDataFromServiceApi ()
228
+ {
229
+ $ contents = file_get_contents ("http://vimeo.com/api/v2/video/ " . $ this ->getVideoId () . ".php " );
230
+ if (false === $ contents ) {
231
+ throw new ServiceApiNotAvailable ('Vimeo Service Adapter could not reach Vimeo API Service. Check if your server has file_get_contents() function available. ' );
232
+ }
233
+ $ hash = unserialize ($ contents );
234
+
235
+ return reset ($ hash );
236
+ }
223
237
}
0 commit comments