1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: Ricardo Fiorani |
5
|
|
|
* Date: 29/08/2015 |
6
|
|
|
* Time: 14:56. |
7
|
|
|
*/ |
8
|
|
|
namespace RicardoFiorani\Adapter\Vimeo; |
9
|
|
|
|
10
|
|
|
use RicardoFiorani\Adapter\AbstractServiceAdapter; |
11
|
|
|
use RicardoFiorani\Exception\InvalidThumbnailSizeException; |
12
|
|
|
use RicardoFiorani\Exception\ServiceApiNotAvailable; |
13
|
|
|
use RicardoFiorani\Renderer\EmbedRendererInterface; |
14
|
|
|
|
15
|
|
|
class VimeoServiceAdapter extends AbstractServiceAdapter |
16
|
|
|
{ |
17
|
|
|
const THUMBNAIL_SMALL = 'thumbnail_small'; |
|
|
|
|
18
|
|
|
const THUMBNAIL_MEDIUM = 'thumbnail_medium'; |
19
|
|
|
const THUMBNAIL_LARGE = 'thumbnail_large'; |
|
|
|
|
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
public $title; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
public $description; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var array |
33
|
|
|
*/ |
34
|
|
|
public $thumbnails; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param string $url |
38
|
|
|
* @param string $pattern |
39
|
|
|
* @param EmbedRendererInterface $renderer |
40
|
|
|
*/ |
41
|
12 |
|
public function __construct($url, $pattern, EmbedRendererInterface $renderer) |
42
|
|
|
{ |
43
|
12 |
|
$videoId = $this->getVideoIdByPattern($url, $pattern); |
44
|
12 |
|
$this->setVideoId($videoId); |
45
|
12 |
|
$videoData = $this->getVideoDataFromServiceApi(); |
46
|
|
|
|
47
|
12 |
|
$this->setThumbnails(array( |
48
|
12 |
|
self::THUMBNAIL_SMALL => $videoData[self::THUMBNAIL_SMALL], |
49
|
12 |
|
self::THUMBNAIL_MEDIUM => $videoData[self::THUMBNAIL_MEDIUM], |
50
|
12 |
|
self::THUMBNAIL_LARGE => $videoData[self::THUMBNAIL_LARGE], |
51
|
12 |
|
)); |
52
|
|
|
|
53
|
12 |
|
$this->setTitle($videoData['title']); |
54
|
12 |
|
$this->setDescription($videoData['description']); |
55
|
|
|
|
56
|
12 |
|
return parent::__construct($url, $pattern, $renderer); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Returns the service name (ie: "Youtube" or "Vimeo"). |
61
|
|
|
* |
62
|
|
|
* @return string |
63
|
|
|
*/ |
64
|
1 |
|
public function getServiceName() |
65
|
|
|
{ |
66
|
1 |
|
return 'Vimeo'; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Returns if the service has a thumbnail image. |
71
|
|
|
* |
72
|
|
|
* @return bool |
73
|
|
|
*/ |
74
|
1 |
|
public function hasThumbnail() |
75
|
|
|
{ |
76
|
1 |
|
return false == empty($this->thumbnails); |
|
|
|
|
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return string |
81
|
|
|
*/ |
82
|
1 |
|
public function getTitle() |
83
|
|
|
{ |
84
|
1 |
|
return $this->title; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param string $title |
89
|
|
|
*/ |
90
|
12 |
|
public function setTitle($title) |
91
|
|
|
{ |
92
|
12 |
|
$this->title = $title; |
93
|
12 |
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return string |
97
|
|
|
*/ |
98
|
1 |
|
public function getDescription() |
99
|
|
|
{ |
100
|
1 |
|
return $this->description; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param string $description |
105
|
|
|
*/ |
106
|
12 |
|
public function setDescription($description) |
107
|
|
|
{ |
108
|
12 |
|
$this->description = $description; |
109
|
12 |
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @return array |
113
|
|
|
*/ |
114
|
1 |
|
public function getThumbnails() |
115
|
|
|
{ |
116
|
1 |
|
return $this->thumbnails; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param array $thumbnails |
121
|
|
|
*/ |
122
|
12 |
|
public function setThumbnails($thumbnails) |
123
|
|
|
{ |
124
|
12 |
|
$this->thumbnails = $thumbnails; |
125
|
12 |
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param string $size |
129
|
|
|
* |
130
|
|
|
* @return string |
131
|
|
|
* |
132
|
|
|
* @throws InvalidThumbnailSizeException |
133
|
|
|
*/ |
134
|
2 |
|
public function getThumbnail($size) |
135
|
|
|
{ |
136
|
2 |
|
if (false == in_array($size, $this->getThumbNailSizes())) { |
|
|
|
|
137
|
1 |
|
throw new InvalidThumbnailSizeException(); |
138
|
|
|
} |
139
|
|
|
|
140
|
1 |
|
return $this->thumbnails[$size]; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @param bool $autoplay |
145
|
|
|
* |
146
|
|
|
* @return string |
147
|
|
|
*/ |
148
|
1 |
|
public function getEmbedUrl($autoplay = false) |
149
|
|
|
{ |
150
|
1 |
|
return 'http://player.vimeo.com/video/'.$this->getVideoId().($autoplay ? '?autoplay=1' : ''); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Returns all thumbnails available sizes. |
155
|
|
|
* |
156
|
|
|
* @return array |
|
|
|
|
157
|
|
|
*/ |
158
|
3 |
|
public function getThumbNailSizes() |
159
|
|
|
{ |
160
|
|
|
return array( |
161
|
3 |
|
self::THUMBNAIL_SMALL, |
162
|
3 |
|
self::THUMBNAIL_MEDIUM, |
163
|
3 |
|
self::THUMBNAIL_LARGE, |
164
|
3 |
|
); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Returns the small thumbnail's url. |
169
|
|
|
* |
170
|
|
|
* @return string |
171
|
|
|
*/ |
172
|
1 |
|
public function getSmallThumbnail() |
173
|
|
|
{ |
174
|
1 |
|
return $this->getThumbnail(self::THUMBNAIL_SMALL); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Returns the medium thumbnail's url. |
179
|
|
|
* |
180
|
|
|
* @return string |
181
|
|
|
*/ |
182
|
1 |
|
public function getMediumThumbnail() |
183
|
|
|
{ |
184
|
1 |
|
return $this->getThumbnail(self::THUMBNAIL_MEDIUM); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Returns the large thumbnail's url. |
189
|
|
|
* |
190
|
|
|
* @return string |
191
|
|
|
*/ |
192
|
1 |
|
public function getLargeThumbnail() |
193
|
|
|
{ |
194
|
1 |
|
return $this->getThumbnail(self::THUMBNAIL_LARGE); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Returns the largest thumnbnaail's url. |
199
|
|
|
* |
200
|
|
|
* @return string |
201
|
|
|
*/ |
202
|
1 |
|
public function getLargestThumbnail() |
203
|
|
|
{ |
204
|
1 |
|
return $this->getThumbnail(self::THUMBNAIL_LARGE); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* @return bool |
209
|
|
|
*/ |
210
|
1 |
|
public function isEmbeddable() |
211
|
|
|
{ |
212
|
1 |
|
return true; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* @param string $url |
217
|
|
|
* @param string $pattern |
218
|
|
|
* |
219
|
|
|
* @return int |
|
|
|
|
220
|
|
|
*/ |
221
|
12 |
|
private function getVideoIdByPattern($url, $pattern) |
222
|
|
|
{ |
223
|
12 |
|
$match = array(); |
224
|
12 |
|
preg_match($pattern, $url, $match); |
225
|
12 |
|
$videoId = $match[2]; |
226
|
|
|
|
227
|
12 |
|
return $videoId; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Uses the Vimeo video API to get video info. |
232
|
|
|
* |
233
|
|
|
* @todo make this better by using guzzle |
|
|
|
|
234
|
|
|
* |
235
|
|
|
* @return array |
236
|
|
|
* |
237
|
|
|
* @throws ServiceApiNotAvailable |
238
|
|
|
*/ |
239
|
12 |
|
private function getVideoDataFromServiceApi() |
240
|
|
|
{ |
241
|
12 |
|
$contents = file_get_contents('http://vimeo.com/api/v2/video/'.$this->getVideoId().'.php'); |
242
|
12 |
|
if (false === $contents) { |
243
|
|
|
throw new ServiceApiNotAvailable('Vimeo Service Adapter could not reach Vimeo API Service. Check if your server has file_get_contents() function available.'); |
|
|
|
|
244
|
|
|
} |
245
|
12 |
|
$hash = unserialize($contents); |
246
|
|
|
|
247
|
12 |
|
return reset($hash); |
248
|
|
|
} |
249
|
|
|
} |
|
|
|
|
250
|
|
|
|
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.