Skip to content

Commit 87fa11d

Browse files
Merge pull request #18 from ricardofiorani/update-vimeo-adapter-to-use-oembed
Update Vimeo Adapter to use oEmbed
2 parents 7904e78 + b5eb97d commit 87fa11d

File tree

4 files changed

+30
-53
lines changed

4 files changed

+30
-53
lines changed

composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
}
1414
],
1515
"require": {
16-
"php": ">=5.3"
16+
"php": ">=5.3",
17+
"ext-json": "*"
1718
},
1819
"autoload": {
1920
"psr-4": {

src/Adapter/Vimeo/VimeoServiceAdapter.php

+13-33
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
<?php
2-
/**
3-
* Created by PhpStorm.
4-
* User: Ricardo Fiorani
5-
* Date: 29/08/2015
6-
* Time: 14:56.
7-
*/
2+
83
namespace RicardoFiorani\Adapter\Vimeo;
94

105
use RicardoFiorani\Adapter\AbstractServiceAdapter;
@@ -42,18 +37,19 @@ class VimeoServiceAdapter extends AbstractServiceAdapter
4237
*/
4338
public function __construct($url, $pattern, EmbedRendererInterface $renderer)
4439
{
45-
$videoId = $this->getVideoIdByPattern($url, $pattern);
40+
$videoData = $this->getVideoDataFromServiceApi($url);
41+
$videoId = $videoData->video_id;
4642
$this->setVideoId($videoId);
47-
$videoData = $this->getVideoDataFromServiceApi();
4843

44+
//oEmbed only provides one size now :(
4945
$this->setThumbnails(array(
50-
self::THUMBNAIL_SMALL => $videoData[self::THUMBNAIL_SMALL],
51-
self::THUMBNAIL_MEDIUM => $videoData[self::THUMBNAIL_MEDIUM],
52-
self::THUMBNAIL_LARGE => $videoData[self::THUMBNAIL_LARGE],
46+
self::THUMBNAIL_SMALL => $videoData->thumbnail_url,
47+
self::THUMBNAIL_MEDIUM => $videoData->thumbnail_url,
48+
self::THUMBNAIL_LARGE => $videoData->thumbnail_url,
5349
));
5450

55-
$this->setTitle($videoData['title']);
56-
$this->setDescription($videoData['description']);
51+
$this->setTitle($videoData->title);
52+
$this->setDescription($videoData->description);
5753

5854
return parent::__construct($url, $pattern, $renderer);
5955
}
@@ -227,41 +223,25 @@ public function isEmbeddable()
227223
return true;
228224
}
229225

230-
/**
231-
* @param string $url
232-
* @param string $pattern
233-
*
234-
* @return int
235-
*/
236-
private function getVideoIdByPattern($url, $pattern)
237-
{
238-
$match = array();
239-
preg_match($pattern, $url, $match);
240-
$videoId = $match[2];
241-
242-
return $videoId;
243-
}
244-
245226
/**
246227
* Uses the Vimeo video API to get video info.
247228
*
248229
* @todo make this better by using guzzle
249230
*
250-
* @return array
231+
* @return \stdClass
251232
*
252233
* @throws ServiceApiNotAvailable
253234
*/
254-
private function getVideoDataFromServiceApi()
235+
private function getVideoDataFromServiceApi($url)
255236
{
256-
$contents = file_get_contents('http://vimeo.com/api/v2/video/' . $this->getVideoId() . '.php');
237+
$contents = file_get_contents('https://vimeo.com/api/oembed.json?url=' . urlencode($url));
257238
if (false === $contents) {
258239
throw new ServiceApiNotAvailable(
259240
'Service "%s" could not reach it\'s API. Check if file_get_contents() function is available.',
260241
$this->getServiceName()
261242
);
262243
}
263-
$hash = unserialize($contents);
264244

265-
return reset($hash);
245+
return json_decode($contents);
266246
}
267247
}

src/Container/ServicesContainer.php

+8-9
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
<?php
2-
/**
3-
* Created by PhpStorm.
4-
* User: Ricardo Fiorani
5-
* Date: 31/08/2015
6-
* Time: 21:06.
7-
*/
2+
83
namespace RicardoFiorani\Container;
94

105
use RicardoFiorani\Adapter\CallableServiceAdapterFactoryInterface;
@@ -68,14 +63,15 @@ private function registerFromConfig(array $config)
6863
foreach ($config['services'] as $serviceName => $serviceConfig) {
6964
$this->registerService($serviceName, $serviceConfig['patterns'], $serviceConfig['factory']);
7065
}
66+
7167
$this->setRenderer($config['renderer']['name'], $config['renderer']['factory']);
7268
}
7369

7470
/**
7571
* Register a Service.
7672
*
77-
* @param string $serviceName
78-
* @param array $regex
73+
* @param string $serviceName
74+
* @param array $regex
7975
* @param string|callable $factory
8076
*
8177
* @throws DuplicatedServiceNameException
@@ -84,7 +80,10 @@ public function registerService($serviceName, array $regex, $factory)
8480
{
8581
if ($this->hasService($serviceName)) {
8682
throw new DuplicatedServiceNameException(
87-
'The service "%s" is already registered in the container.', $serviceName
83+
sprintf(
84+
'The service "%s" is already registered in the container.',
85+
$serviceName
86+
)
8887
);
8988
}
9089

test/Container/ServicesContainerTest.php

+7-10
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
11
<?php
2-
/**
3-
* Created by PhpStorm.
4-
* User: Ricardo Fiorani
5-
* Date: 10/02/2016
6-
* Time: 19:58
7-
*/
82

93
namespace RicardoFiorani\Test\Container;
104

11-
125
use PHPUnit_Framework_TestCase;
136
use RicardoFiorani\Container\ServicesContainer;
147
use stdClass;
@@ -31,8 +24,11 @@ public function testServiceContainerServiceRegistrationByInjection()
3124
});
3225

3326
$this->assertContains('TestService', $serviceContainer->getServiceNameList());
27+
3428
$this->setExpectedException('\\RicardoFiorani\\Container\\Exception\\DuplicatedServiceNameException');
29+
3530
$serviceContainer->registerService('TestService', array('#testPattern#'), function () {
31+
// @todo test the injected service maybe ?
3632
});
3733
}
3834

@@ -44,14 +40,15 @@ public function testServicesList()
4440
$this->assertContains('Youtube', $serviceContainer->getServices());
4541
}
4642

47-
public function testIfReturnsAlreadyInstantiatedFactory(){
43+
public function testIfReturnsAlreadyInstantiatedFactory()
44+
{
4845
$config = $this->getMockConfig();
4946
$serviceContainer = $this->createServiceContainer($config);
5047
$factory = $serviceContainer->getFactory('Youtube');
51-
$this->assertInstanceOf('\\RicardoFiorani\\Adapter\\Youtube\\Factory\\YoutubeServiceAdapterFactory',$factory);
48+
$this->assertInstanceOf('\\RicardoFiorani\\Adapter\\Youtube\\Factory\\YoutubeServiceAdapterFactory', $factory);
5249

5350
$alreadyInstantiatedFactory = $serviceContainer->getFactory('Youtube');
54-
$this->assertEquals($factory,$alreadyInstantiatedFactory);
51+
$this->assertEquals($factory, $alreadyInstantiatedFactory);
5552
}
5653

5754
/**

0 commit comments

Comments
 (0)