Skip to content

Commit

Permalink
Merge pull request #18 from ricardofiorani/update-vimeo-adapter-to-us…
Browse files Browse the repository at this point in the history
…e-oembed

Update Vimeo Adapter to use oEmbed
  • Loading branch information
ricardofiorani authored Sep 19, 2018
2 parents 7904e78 + b5eb97d commit 87fa11d
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 53 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
}
],
"require": {
"php": ">=5.3"
"php": ">=5.3",
"ext-json": "*"
},
"autoload": {
"psr-4": {
Expand Down
46 changes: 13 additions & 33 deletions src/Adapter/Vimeo/VimeoServiceAdapter.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
<?php
/**
* Created by PhpStorm.
* User: Ricardo Fiorani
* Date: 29/08/2015
* Time: 14:56.
*/

namespace RicardoFiorani\Adapter\Vimeo;

use RicardoFiorani\Adapter\AbstractServiceAdapter;
Expand Down Expand Up @@ -42,18 +37,19 @@ class VimeoServiceAdapter extends AbstractServiceAdapter
*/
public function __construct($url, $pattern, EmbedRendererInterface $renderer)
{
$videoId = $this->getVideoIdByPattern($url, $pattern);
$videoData = $this->getVideoDataFromServiceApi($url);
$videoId = $videoData->video_id;
$this->setVideoId($videoId);
$videoData = $this->getVideoDataFromServiceApi();

//oEmbed only provides one size now :(
$this->setThumbnails(array(
self::THUMBNAIL_SMALL => $videoData[self::THUMBNAIL_SMALL],
self::THUMBNAIL_MEDIUM => $videoData[self::THUMBNAIL_MEDIUM],
self::THUMBNAIL_LARGE => $videoData[self::THUMBNAIL_LARGE],
self::THUMBNAIL_SMALL => $videoData->thumbnail_url,
self::THUMBNAIL_MEDIUM => $videoData->thumbnail_url,
self::THUMBNAIL_LARGE => $videoData->thumbnail_url,
));

$this->setTitle($videoData['title']);
$this->setDescription($videoData['description']);
$this->setTitle($videoData->title);
$this->setDescription($videoData->description);

return parent::__construct($url, $pattern, $renderer);
}
Expand Down Expand Up @@ -227,41 +223,25 @@ public function isEmbeddable()
return true;
}

/**
* @param string $url
* @param string $pattern
*
* @return int
*/
private function getVideoIdByPattern($url, $pattern)
{
$match = array();
preg_match($pattern, $url, $match);
$videoId = $match[2];

return $videoId;
}

/**
* Uses the Vimeo video API to get video info.
*
* @todo make this better by using guzzle
*
* @return array
* @return \stdClass
*
* @throws ServiceApiNotAvailable
*/
private function getVideoDataFromServiceApi()
private function getVideoDataFromServiceApi($url)
{
$contents = file_get_contents('http://vimeo.com/api/v2/video/' . $this->getVideoId() . '.php');
$contents = file_get_contents('https://vimeo.com/api/oembed.json?url=' . urlencode($url));
if (false === $contents) {
throw new ServiceApiNotAvailable(
'Service "%s" could not reach it\'s API. Check if file_get_contents() function is available.',
$this->getServiceName()
);
}
$hash = unserialize($contents);

return reset($hash);
return json_decode($contents);
}
}
17 changes: 8 additions & 9 deletions src/Container/ServicesContainer.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
<?php
/**
* Created by PhpStorm.
* User: Ricardo Fiorani
* Date: 31/08/2015
* Time: 21:06.
*/

namespace RicardoFiorani\Container;

use RicardoFiorani\Adapter\CallableServiceAdapterFactoryInterface;
Expand Down Expand Up @@ -68,14 +63,15 @@ private function registerFromConfig(array $config)
foreach ($config['services'] as $serviceName => $serviceConfig) {
$this->registerService($serviceName, $serviceConfig['patterns'], $serviceConfig['factory']);
}

$this->setRenderer($config['renderer']['name'], $config['renderer']['factory']);
}

/**
* Register a Service.
*
* @param string $serviceName
* @param array $regex
* @param string $serviceName
* @param array $regex
* @param string|callable $factory
*
* @throws DuplicatedServiceNameException
Expand All @@ -84,7 +80,10 @@ public function registerService($serviceName, array $regex, $factory)
{
if ($this->hasService($serviceName)) {
throw new DuplicatedServiceNameException(
'The service "%s" is already registered in the container.', $serviceName
sprintf(
'The service "%s" is already registered in the container.',
$serviceName
)
);
}

Expand Down
17 changes: 7 additions & 10 deletions test/Container/ServicesContainerTest.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
<?php
/**
* Created by PhpStorm.
* User: Ricardo Fiorani
* Date: 10/02/2016
* Time: 19:58
*/

namespace RicardoFiorani\Test\Container;


use PHPUnit_Framework_TestCase;
use RicardoFiorani\Container\ServicesContainer;
use stdClass;
Expand All @@ -31,8 +24,11 @@ public function testServiceContainerServiceRegistrationByInjection()
});

$this->assertContains('TestService', $serviceContainer->getServiceNameList());

$this->setExpectedException('\\RicardoFiorani\\Container\\Exception\\DuplicatedServiceNameException');

$serviceContainer->registerService('TestService', array('#testPattern#'), function () {
// @todo test the injected service maybe ?
});
}

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

public function testIfReturnsAlreadyInstantiatedFactory(){
public function testIfReturnsAlreadyInstantiatedFactory()
{
$config = $this->getMockConfig();
$serviceContainer = $this->createServiceContainer($config);
$factory = $serviceContainer->getFactory('Youtube');
$this->assertInstanceOf('\\RicardoFiorani\\Adapter\\Youtube\\Factory\\YoutubeServiceAdapterFactory',$factory);
$this->assertInstanceOf('\\RicardoFiorani\\Adapter\\Youtube\\Factory\\YoutubeServiceAdapterFactory', $factory);

$alreadyInstantiatedFactory = $serviceContainer->getFactory('Youtube');
$this->assertEquals($factory,$alreadyInstantiatedFactory);
$this->assertEquals($factory, $alreadyInstantiatedFactory);
}

/**
Expand Down

0 comments on commit 87fa11d

Please sign in to comment.