From 26cab8107e1ee3d38f29e0563c3e38e443f7aefd Mon Sep 17 00:00:00 2001 From: Ricardo Fiorani Date: Wed, 19 Sep 2018 23:06:09 +0200 Subject: [PATCH 1/2] added oembed on vimeo adapter --- composer.json | 3 +- src/Adapter/Vimeo/VimeoServiceAdapter.php | 44 +++++++---------------- 2 files changed, 14 insertions(+), 33 deletions(-) diff --git a/composer.json b/composer.json index d0c4485..9159236 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,8 @@ } ], "require": { - "php": ">=5.3" + "php": ">=5.3", + "ext-json": "*" }, "autoload": { "psr-4": { diff --git a/src/Adapter/Vimeo/VimeoServiceAdapter.php b/src/Adapter/Vimeo/VimeoServiceAdapter.php index d6f2453..23d276c 100644 --- a/src/Adapter/Vimeo/VimeoServiceAdapter.php +++ b/src/Adapter/Vimeo/VimeoServiceAdapter.php @@ -1,10 +1,5 @@ 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); } @@ -227,21 +223,6 @@ 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. * @@ -251,17 +232,16 @@ private function getVideoIdByPattern($url, $pattern) * * @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); } } From b5eb97d6dcb0560e5a9d38c79eb0c602a2d03224 Mon Sep 17 00:00:00 2001 From: Ricardo Fiorani Date: Wed, 19 Sep 2018 23:17:56 +0200 Subject: [PATCH 2/2] test fix and small improvements --- src/Adapter/Vimeo/VimeoServiceAdapter.php | 2 +- src/Container/ServicesContainer.php | 17 ++++++++--------- test/Container/ServicesContainerTest.php | 17 +++++++---------- 3 files changed, 16 insertions(+), 20 deletions(-) diff --git a/src/Adapter/Vimeo/VimeoServiceAdapter.php b/src/Adapter/Vimeo/VimeoServiceAdapter.php index 23d276c..fd03831 100644 --- a/src/Adapter/Vimeo/VimeoServiceAdapter.php +++ b/src/Adapter/Vimeo/VimeoServiceAdapter.php @@ -228,7 +228,7 @@ public function isEmbeddable() * * @todo make this better by using guzzle * - * @return array + * @return \stdClass * * @throws ServiceApiNotAvailable */ diff --git a/src/Container/ServicesContainer.php b/src/Container/ServicesContainer.php index 138c84e..3fa4b92 100644 --- a/src/Container/ServicesContainer.php +++ b/src/Container/ServicesContainer.php @@ -1,10 +1,5 @@ $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 @@ -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 + ) ); } diff --git a/test/Container/ServicesContainerTest.php b/test/Container/ServicesContainerTest.php index 6c1f6f4..ef832ec 100644 --- a/test/Container/ServicesContainerTest.php +++ b/test/Container/ServicesContainerTest.php @@ -1,14 +1,7 @@ assertContains('TestService', $serviceContainer->getServiceNameList()); + $this->setExpectedException('\\RicardoFiorani\\Container\\Exception\\DuplicatedServiceNameException'); + $serviceContainer->registerService('TestService', array('#testPattern#'), function () { + // @todo test the injected service maybe ? }); } @@ -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); } /**