From 7904e78c3c1382d5aeff31efe78b95e1cc931a4e Mon Sep 17 00:00:00 2001 From: Ricardo Fiorani Date: Tue, 17 Apr 2018 17:58:25 +0200 Subject: [PATCH 01/11] General refactoring for v1.0.0 --- src/Adapter/AbstractServiceAdapter.php | 24 +++++++++++++++---- .../Dailymotion/DailymotionServiceAdapter.php | 8 ++++++- .../InvalidThumbnailSizeException.php | 2 +- src/Adapter/Exception/InvalidUrlException.php | 12 ++++++++++ .../Exception/NotEmbeddableException.php | 2 +- .../Exception/ServiceApiNotAvailable.php | 2 +- .../Facebook/FacebookServiceAdapter.php | 2 +- src/Adapter/Vimeo/VimeoServiceAdapter.php | 21 +++++++++++----- src/Adapter/Youtube/YoutubeServiceAdapter.php | 2 +- .../DuplicatedServiceNameException.php | 2 +- src/Container/ServicesContainer.php | 7 ++++-- .../VideoServiceNotCompatibleException.php} | 4 ++-- src/Matcher/VideoServiceMatcher.php | 11 +++++---- test/Adapter/AbstractServiceAdapterTest.php | 4 ++-- .../Adapter/DailymotionServiceAdapterTest.php | 6 ++--- test/Adapter/FacebookServiceAdapterTest.php | 6 ++--- test/Adapter/VimeoServiceAdapterTest.php | 6 ++--- test/Adapter/YoutubeServiceAdapterTest.php | 6 ++--- test/Container/ServicesContainerTest.php | 2 +- test/Detector/VideoServiceMatcherTest.php | 10 ++++---- 20 files changed, 94 insertions(+), 45 deletions(-) rename src/{ => Adapter}/Exception/InvalidThumbnailSizeException.php (79%) create mode 100644 src/Adapter/Exception/InvalidUrlException.php rename src/{ => Adapter}/Exception/NotEmbeddableException.php (79%) rename src/{ => Adapter}/Exception/ServiceApiNotAvailable.php (79%) rename src/{ => Container}/Exception/DuplicatedServiceNameException.php (79%) rename src/{Exception/ServiceNotAvailableException.php => Matcher/Exception/VideoServiceNotCompatibleException.php} (53%) diff --git a/src/Adapter/AbstractServiceAdapter.php b/src/Adapter/AbstractServiceAdapter.php index 01aa57c..ae2633b 100644 --- a/src/Adapter/AbstractServiceAdapter.php +++ b/src/Adapter/AbstractServiceAdapter.php @@ -5,9 +5,11 @@ * Date: 29/08/2015 * Time: 19:37. */ + namespace RicardoFiorani\Adapter; -use RicardoFiorani\Exception\NotEmbeddableException; +use RicardoFiorani\Adapter\Exception\InvalidUrlException; +use RicardoFiorani\Adapter\Exception\NotEmbeddableException; use RicardoFiorani\Renderer\EmbedRendererInterface; abstract class AbstractServiceAdapter implements VideoAdapterInterface @@ -123,8 +125,10 @@ public function setRenderer($renderer) */ public function getEmbedCode($width, $height, $forceAutoplay = false, $forceSecure = false) { - if (false == $this->isEmbeddable()) { - throw new NotEmbeddableException(); + if (false === $this->isEmbeddable()) { + throw new NotEmbeddableException( + sprintf('The service "%s" does not provide embeddable videos', $this->getServiceName()) + ); } return $this->getRenderer()->renderVideoEmbedCode( @@ -139,6 +143,7 @@ public function getEmbedCode($width, $height, $forceAutoplay = false, $forceSecu * * @param bool|false $forceSecure * @return string + * @throws InvalidUrlException */ public function getScheme($forceSecure = false) { @@ -146,6 +151,17 @@ public function getScheme($forceSecure = false) return 'https'; } - return parse_url($this->rawUrl, PHP_URL_SCHEME); + $parsedUrlSchema = parse_url($this->rawUrl, PHP_URL_SCHEME); + + if (false !== $parsedUrlSchema) { + return $parsedUrlSchema; + } + + throw new InvalidUrlException(sprintf('The URL %s is not valid', $this->rawUrl)); + } + + public function isThumbnailSizeAvailable($intendedSize) + { + return in_array($intendedSize, $this->getThumbNailSizes()); } } diff --git a/src/Adapter/Dailymotion/DailymotionServiceAdapter.php b/src/Adapter/Dailymotion/DailymotionServiceAdapter.php index 753ba93..ff7a1a1 100644 --- a/src/Adapter/Dailymotion/DailymotionServiceAdapter.php +++ b/src/Adapter/Dailymotion/DailymotionServiceAdapter.php @@ -8,7 +8,7 @@ namespace RicardoFiorani\Adapter\Dailymotion; use RicardoFiorani\Adapter\AbstractServiceAdapter; -use RicardoFiorani\Exception\InvalidThumbnailSizeException; +use RicardoFiorani\Adapter\Exception\InvalidThumbnailSizeException; use RicardoFiorani\Renderer\EmbedRendererInterface; class DailymotionServiceAdapter extends AbstractServiceAdapter @@ -68,6 +68,7 @@ public function getThumbNailSizes() * * @return string * @throws InvalidThumbnailSizeException + * @throws \RicardoFiorani\Adapter\Exception\InvalidUrlException */ public function getThumbnail($size, $forceSecure = false) { @@ -84,6 +85,7 @@ public function getThumbnail($size, $forceSecure = false) * @param bool $forceSecure * @return string * @throws InvalidThumbnailSizeException + * @throws \RicardoFiorani\Adapter\Exception\InvalidUrlException */ public function getSmallThumbnail($forceSecure = false) { @@ -97,6 +99,7 @@ public function getSmallThumbnail($forceSecure = false) * @param bool $forceSecure * @return string * @throws InvalidThumbnailSizeException + * @throws \RicardoFiorani\Adapter\Exception\InvalidUrlException */ public function getMediumThumbnail($forceSecure = false) { @@ -110,6 +113,7 @@ public function getMediumThumbnail($forceSecure = false) * @param bool $forceSecure * @return string * @throws InvalidThumbnailSizeException + * @throws \RicardoFiorani\Adapter\Exception\InvalidUrlException */ public function getLargeThumbnail($forceSecure = false) { @@ -122,6 +126,7 @@ public function getLargeThumbnail($forceSecure = false) * @param bool $forceSecure * @return string * @throws InvalidThumbnailSizeException + * @throws \RicardoFiorani\Adapter\Exception\InvalidUrlException */ public function getLargestThumbnail($forceSecure = false) { @@ -133,6 +138,7 @@ public function getLargestThumbnail($forceSecure = false) * @param bool $forceAutoplay * @param bool $forceSecure * @return string + * @throws \RicardoFiorani\Adapter\Exception\InvalidUrlException */ public function getEmbedUrl($forceAutoplay = false, $forceSecure = false) { diff --git a/src/Exception/InvalidThumbnailSizeException.php b/src/Adapter/Exception/InvalidThumbnailSizeException.php similarity index 79% rename from src/Exception/InvalidThumbnailSizeException.php rename to src/Adapter/Exception/InvalidThumbnailSizeException.php index 4ed72f2..2289d59 100644 --- a/src/Exception/InvalidThumbnailSizeException.php +++ b/src/Adapter/Exception/InvalidThumbnailSizeException.php @@ -5,7 +5,7 @@ * Date: 29/08/2015 * Time: 20:17. */ -namespace RicardoFiorani\Exception; +namespace RicardoFiorani\Adapter\Exception; use Exception; diff --git a/src/Adapter/Exception/InvalidUrlException.php b/src/Adapter/Exception/InvalidUrlException.php new file mode 100644 index 0000000..1c29932 --- /dev/null +++ b/src/Adapter/Exception/InvalidUrlException.php @@ -0,0 +1,12 @@ +getVideoId() . '.php'); if (false === $contents) { - throw new ServiceApiNotAvailable('Vimeo Service Adapter could not reach Vimeo API Service. Check if your server has file_get_contents() function available.'); + throw new ServiceApiNotAvailable( + 'Service "%s" could not reach it\'s API. Check if file_get_contents() function is available.', + $this->getServiceName() + ); } $hash = unserialize($contents); diff --git a/src/Adapter/Youtube/YoutubeServiceAdapter.php b/src/Adapter/Youtube/YoutubeServiceAdapter.php index 76af95c..57f2380 100644 --- a/src/Adapter/Youtube/YoutubeServiceAdapter.php +++ b/src/Adapter/Youtube/YoutubeServiceAdapter.php @@ -9,7 +9,7 @@ namespace RicardoFiorani\Adapter\Youtube; use RicardoFiorani\Adapter\AbstractServiceAdapter; -use RicardoFiorani\Exception\InvalidThumbnailSizeException; +use RicardoFiorani\Adapter\Exception\InvalidThumbnailSizeException; use RicardoFiorani\Renderer\EmbedRendererInterface; class YoutubeServiceAdapter extends AbstractServiceAdapter diff --git a/src/Exception/DuplicatedServiceNameException.php b/src/Container/Exception/DuplicatedServiceNameException.php similarity index 79% rename from src/Exception/DuplicatedServiceNameException.php rename to src/Container/Exception/DuplicatedServiceNameException.php index 90d3b20..329a7ab 100644 --- a/src/Exception/DuplicatedServiceNameException.php +++ b/src/Container/Exception/DuplicatedServiceNameException.php @@ -5,7 +5,7 @@ * Date: 29/08/2015 * Time: 23:28. */ -namespace RicardoFiorani\Exception; +namespace RicardoFiorani\Container\Exception; use Exception; diff --git a/src/Container/ServicesContainer.php b/src/Container/ServicesContainer.php index 246ec33..138c84e 100644 --- a/src/Container/ServicesContainer.php +++ b/src/Container/ServicesContainer.php @@ -8,7 +8,7 @@ namespace RicardoFiorani\Container; use RicardoFiorani\Adapter\CallableServiceAdapterFactoryInterface; -use RicardoFiorani\Exception\DuplicatedServiceNameException; +use RicardoFiorani\Container\Exception\DuplicatedServiceNameException; use RicardoFiorani\Renderer\EmbedRendererInterface; class ServicesContainer @@ -47,6 +47,7 @@ class ServicesContainer * ServicesContainer constructor. * * @param array $config + * @throws DuplicatedServiceNameException */ public function __construct(array $config = array()) { @@ -82,7 +83,9 @@ private function registerFromConfig(array $config) public function registerService($serviceName, array $regex, $factory) { if ($this->hasService($serviceName)) { - throw new DuplicatedServiceNameException(); + throw new DuplicatedServiceNameException( + 'The service "%s" is already registered in the container.', $serviceName + ); } $this->services[] = $serviceName; diff --git a/src/Exception/ServiceNotAvailableException.php b/src/Matcher/Exception/VideoServiceNotCompatibleException.php similarity index 53% rename from src/Exception/ServiceNotAvailableException.php rename to src/Matcher/Exception/VideoServiceNotCompatibleException.php index 6c6e7ee..d631a33 100644 --- a/src/Exception/ServiceNotAvailableException.php +++ b/src/Matcher/Exception/VideoServiceNotCompatibleException.php @@ -5,10 +5,10 @@ * Date: 29/08/2015 * Time: 23:44. */ -namespace RicardoFiorani\Exception; +namespace RicardoFiorani\Matcher\Exception; use Exception; -class ServiceNotAvailableException extends Exception +class VideoServiceNotCompatibleException extends Exception { } diff --git a/src/Matcher/VideoServiceMatcher.php b/src/Matcher/VideoServiceMatcher.php index 44fef16..499ea06 100644 --- a/src/Matcher/VideoServiceMatcher.php +++ b/src/Matcher/VideoServiceMatcher.php @@ -5,7 +5,7 @@ use RicardoFiorani\Adapter\VideoAdapterInterface; use RicardoFiorani\Container\Factory\ServicesContainerFactory; use RicardoFiorani\Container\ServicesContainer; -use RicardoFiorani\Exception\ServiceNotAvailableException; +use RicardoFiorani\Matcher\Exception\VideoServiceNotCompatibleException; /** * @author Ricardo Fiorani @@ -35,13 +35,14 @@ public function __construct() * * @return VideoAdapterInterface * - * @throws ServiceNotAvailableException + * @throws VideoServiceNotCompatibleException */ public function parse($url) { if (isset($this->parsedUrls[$url])) { return $this->parsedUrls[$url]; } + /** @var array $patterns */ /** @var string $serviceName */ foreach ($this->getServiceContainer()->getPatterns() as $serviceName => $patterns) { @@ -55,8 +56,10 @@ public function parse($url) } } } - throw new ServiceNotAvailableException(sprintf('The url "%s" could not be parsed by any of the services available.', - $url)); + + throw new VideoServiceNotCompatibleException( + sprintf('The url "%s" could not be parsed by any of the services available.', $url) + ); } /** diff --git a/test/Adapter/AbstractServiceAdapterTest.php b/test/Adapter/AbstractServiceAdapterTest.php index 22c4e09..caf1f23 100644 --- a/test/Adapter/AbstractServiceAdapterTest.php +++ b/test/Adapter/AbstractServiceAdapterTest.php @@ -10,7 +10,7 @@ use PHPUnit_Framework_TestCase; use RicardoFiorani\Adapter\Facebook\FacebookServiceAdapter; -use RicardoFiorani\Exception\ServiceNotAvailableException; +use RicardoFiorani\Matcher\Exception\VideoServiceNotCompatibleException; use RicardoFiorani\Matcher\VideoServiceMatcher; use RicardoFiorani\Renderer\DefaultRenderer; @@ -116,7 +116,7 @@ public function exampleUrlDataProvider() /** * @param $url * @return FacebookServiceAdapter - * @throws ServiceNotAvailableException + * @throws VideoServiceNotCompatibleException */ public function getMockingObject($url) { diff --git a/test/Adapter/DailymotionServiceAdapterTest.php b/test/Adapter/DailymotionServiceAdapterTest.php index 5833fda..0f658eb 100644 --- a/test/Adapter/DailymotionServiceAdapterTest.php +++ b/test/Adapter/DailymotionServiceAdapterTest.php @@ -11,7 +11,7 @@ use PHPUnit_Framework_TestCase; use RicardoFiorani\Adapter\Dailymotion\DailymotionServiceAdapter; use RicardoFiorani\Matcher\VideoServiceMatcher; -use RicardoFiorani\Exception\ServiceNotAvailableException; +use RicardoFiorani\Matcher\Exception\VideoServiceNotCompatibleException; class DailymotionServiceAdapterTest extends PHPUnit_Framework_TestCase @@ -69,7 +69,7 @@ public function testIfGetThumbnailIsString($url) public function testThrowsExceptionOnRequestThumbnailWithAnInvalidSize($url) { $dailymotionVideo = $this->getMockingObject($url); - $this->setExpectedException('\\RicardoFiorani\\Exception\\InvalidThumbnailSizeException'); + $this->setExpectedException('\\RicardoFiorani\\Adapter\\Exception\\InvalidThumbnailSizeException'); $dailymotionVideo->getThumbnail('This Size does not exists :)'); } @@ -122,7 +122,7 @@ public function exampleUrlDataProvider() /** * @param $url * @return DailymotionServiceAdapter - * @throws ServiceNotAvailableException + * @throws VideoServiceNotCompatibleException */ public function getMockingObject($url) { diff --git a/test/Adapter/FacebookServiceAdapterTest.php b/test/Adapter/FacebookServiceAdapterTest.php index 2bf5db9..14b9bfd 100644 --- a/test/Adapter/FacebookServiceAdapterTest.php +++ b/test/Adapter/FacebookServiceAdapterTest.php @@ -11,7 +11,7 @@ use PHPUnit_Framework_TestCase; use RicardoFiorani\Adapter\Facebook\FacebookServiceAdapter; use RicardoFiorani\Matcher\VideoServiceMatcher; -use RicardoFiorani\Exception\ServiceNotAvailableException; +use RicardoFiorani\Matcher\Exception\VideoServiceNotCompatibleException; class FacebookServiceAdapterTest extends PHPUnit_Framework_TestCase { @@ -67,7 +67,7 @@ public function testIfGetThumbnailIsString($url) public function testThrowsExceptionOnRequestThumbnailWithAnInvalidSize($url) { $facebookVideo = $this->getMockingObject($url); - $this->setExpectedException('\\RicardoFiorani\\Exception\\InvalidThumbnailSizeException'); + $this->setExpectedException('\\RicardoFiorani\\Adapter\\Exception\\InvalidThumbnailSizeException'); $facebookVideo->getThumbnail('This Size does not exists :)'); } @@ -143,7 +143,7 @@ public function exampleUrlDataProvider() /** * @param $url * @return FacebookServiceAdapter - * @throws ServiceNotAvailableException + * @throws VideoServiceNotCompatibleException */ public function getMockingObject($url) { diff --git a/test/Adapter/VimeoServiceAdapterTest.php b/test/Adapter/VimeoServiceAdapterTest.php index c60afaf..cf0b2c2 100644 --- a/test/Adapter/VimeoServiceAdapterTest.php +++ b/test/Adapter/VimeoServiceAdapterTest.php @@ -12,7 +12,7 @@ use PHPUnit_Framework_TestCase; use RicardoFiorani\Adapter\Vimeo\VimeoServiceAdapter; use RicardoFiorani\Matcher\VideoServiceMatcher; -use RicardoFiorani\Exception\ServiceNotAvailableException; +use RicardoFiorani\Matcher\Exception\VideoServiceNotCompatibleException; class VimeoServiceAdapterTest extends PHPUnit_Framework_TestCase { @@ -87,7 +87,7 @@ public function testIfGetThumbnailIsString($url) public function testThrowsExceptionOnRequestThumbnailWithAnInvalidSize($url) { $vimeoVideo = $this->getMockingObject($url); - $this->setExpectedException('\\RicardoFiorani\\Exception\\InvalidThumbnailSizeException'); + $this->setExpectedException('\\RicardoFiorani\\Adapter\\Exception\\InvalidThumbnailSizeException'); $vimeoVideo->getThumbnail('This Size does not exists :)'); } @@ -141,7 +141,7 @@ public function exampleUrlDataProvider() /** * @param $url * @return VimeoServiceAdapter - * @throws ServiceNotAvailableException + * @throws VideoServiceNotCompatibleException */ public function getMockingObject($url) { diff --git a/test/Adapter/YoutubeServiceAdapterTest.php b/test/Adapter/YoutubeServiceAdapterTest.php index 81a477d..37d1261 100644 --- a/test/Adapter/YoutubeServiceAdapterTest.php +++ b/test/Adapter/YoutubeServiceAdapterTest.php @@ -12,7 +12,7 @@ use PHPUnit_Framework_TestCase; use RicardoFiorani\Adapter\Youtube\YoutubeServiceAdapter; use RicardoFiorani\Matcher\VideoServiceMatcher; -use RicardoFiorani\Exception\ServiceNotAvailableException; +use RicardoFiorani\Matcher\Exception\VideoServiceNotCompatibleException; class YoutubeServiceAdapterTest extends PHPUnit_Framework_TestCase { @@ -67,7 +67,7 @@ public function testIfGetThumbnailIsString($url) public function testThrowsExceptionOnRequestThumbnailWithAnInvalidSize($url) { $youtubeVideo = $this->getMockingObject($url); - $this->setExpectedException('\\RicardoFiorani\\Exception\\InvalidThumbnailSizeException'); + $this->setExpectedException('\\RicardoFiorani\\Adapter\\Exception\\InvalidThumbnailSizeException'); $youtubeVideo->getThumbnail('This Size does not exists :)'); } @@ -123,7 +123,7 @@ public function exampleUrlDataProvider() /** * @param $url * @return YoutubeServiceAdapter - * @throws ServiceNotAvailableException + * @throws VideoServiceNotCompatibleException */ public function getMockingObject($url) { diff --git a/test/Container/ServicesContainerTest.php b/test/Container/ServicesContainerTest.php index 478e782..6c1f6f4 100644 --- a/test/Container/ServicesContainerTest.php +++ b/test/Container/ServicesContainerTest.php @@ -31,7 +31,7 @@ public function testServiceContainerServiceRegistrationByInjection() }); $this->assertContains('TestService', $serviceContainer->getServiceNameList()); - $this->setExpectedException('\\RicardoFiorani\\Exception\\DuplicatedServiceNameException'); + $this->setExpectedException('\\RicardoFiorani\\Container\\Exception\\DuplicatedServiceNameException'); $serviceContainer->registerService('TestService', array('#testPattern#'), function () { }); } diff --git a/test/Detector/VideoServiceMatcherTest.php b/test/Detector/VideoServiceMatcherTest.php index f4c0747..8ee5bb8 100644 --- a/test/Detector/VideoServiceMatcherTest.php +++ b/test/Detector/VideoServiceMatcherTest.php @@ -11,7 +11,7 @@ use PHPUnit_Framework_TestCase; use RicardoFiorani\Container\Factory\ServicesContainerFactory; use RicardoFiorani\Matcher\VideoServiceMatcher; -use RicardoFiorani\Exception\ServiceNotAvailableException; +use RicardoFiorani\Matcher\Exception\VideoServiceNotCompatibleException; class VideoServiceMatcherTest extends PHPUnit_Framework_TestCase { @@ -19,7 +19,7 @@ class VideoServiceMatcherTest extends PHPUnit_Framework_TestCase * @dataProvider videoUrlProvider * @param $url * @param $expectedServiceName - * @throws ServiceNotAvailableException + * @throws VideoServiceNotCompatibleException */ public function testCanParseUrl($url, $expectedServiceName) { @@ -63,13 +63,13 @@ public function videoUrlProvider() } /** - * @throws ServiceNotAvailableException + * @throws VideoServiceNotCompatibleException * @dataProvider invalidVideoUrlProvider */ public function testThrowsExceptionOnInvalidUrl($url) { $detector = new VideoServiceMatcher(); - $this->setExpectedException('\\RicardoFiorani\\Exception\\ServiceNotAvailableException'); + $this->setExpectedException('\\RicardoFiorani\\Matcher\\Exception\\VideoServiceNotCompatibleException'); $video = $detector->parse($url); } @@ -94,7 +94,7 @@ public function invalidVideoUrlProvider() /** * @dataProvider videoUrlProvider * @param $url - * @throws ServiceNotAvailableException + * @throws VideoServiceNotCompatibleException */ public function testServiceDetectorDontReparseSameUrl($url) { From 7eacdb527baf78665669bdf7c9fcad28c37cc277 Mon Sep 17 00:00:00 2001 From: Ricardo Fiorani Date: Sat, 28 Jul 2018 11:45:29 +0200 Subject: [PATCH 02/11] WIP --- .travis.yml | 9 -- composer.json | 5 +- config/config.php | 16 +-- src/Adapter/AbstractServiceAdapter.php | 96 +++---------- ...CallableServiceAdapterFactoryInterface.php | 18 +-- .../Dailymotion/DailymotionServiceAdapter.php | 78 ++--------- .../DailymotionServiceAdapterFactory.php | 18 +-- .../InvalidThumbnailSizeException.php | 9 +- src/Adapter/Exception/InvalidUrlException.php | 5 +- .../Exception/NotEmbeddableException.php | 9 +- .../Exception/ServiceApiNotAvailable.php | 9 +- .../Facebook/FacebookServiceAdapter.php | 85 ++---------- .../Factory/FacebookServiceAdapterFactory.php | 18 +-- src/Adapter/VideoAdapterInterface.php | 99 ++------------ .../Factory/VimeoServiceAdapterFactory.php | 20 +-- src/Adapter/Vimeo/VimeoServiceAdapter.php | 126 +++--------------- .../Factory/YoutubeServiceAdapterFactory.php | 18 +-- src/Adapter/Youtube/YoutubeServiceAdapter.php | 85 ++---------- .../DuplicatedServiceNameException.php | 9 +- .../Factory/ServicesContainerFactory.php | 13 +- src/Container/ServicesContainer.php | 90 ++----------- src/Exception/ThumbnailSizeNotAvailable.php | 9 +- .../VideoServiceNotCompatibleException.php | 9 +- src/Matcher/VideoServiceMatcher.php | 29 +--- src/Renderer/DefaultRenderer.php | 20 ++- src/Renderer/EmbedRendererInterface.php | 18 +-- .../Factory/DefaultRendererFactory.php | 14 +- .../Factory/RendererFactoryInterface.php | 14 +- test/Adapter/AbstractServiceAdapterTest.php | 10 +- .../Adapter/DailymotionServiceAdapterTest.php | 4 +- test/Adapter/FacebookServiceAdapterTest.php | 4 +- test/Adapter/VimeoServiceAdapterTest.php | 4 +- test/Adapter/YoutubeServiceAdapterTest.php | 4 +- test/Container/ServicesContainerTest.php | 4 +- test/Detector/VideoServiceMatcherTest.php | 4 +- test/Renderer/DefaultRendererTest.php | 4 +- 36 files changed, 180 insertions(+), 806 deletions(-) diff --git a/.travis.yml b/.travis.yml index 1fe0c81..ba120b8 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,18 +4,9 @@ sudo: false # Setup build matrix language: php php: - - 5.4 - - 5.5 - - 5.6 - - 7.0 - 7.1 - 7.2 -matrix: - include: - - php: 5.3 - dist: precise - # Dependencies before_install: - composer self-update diff --git a/composer.json b/composer.json index d0c4485..f2b3367 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "ricardofiorani/php-video-url-parser", "description": "A Simple and efficient PHP Video URL Parser that gives you thumbnails and embed codes for various services as Youtube, Vimeo, DailyMotion and Facebook", "require-dev": { - "phpunit/phpunit": "^4.8", + "phpunit/phpunit": "^7.2", "fabpot/php-cs-fixer" : ">= 1.0" }, "license": "MIT", @@ -13,7 +13,8 @@ } ], "require": { - "php": ">=5.3" + "php": ">=7.1", + "mockery/mockery": "^1.1" }, "autoload": { "psr-4": { diff --git a/config/config.php b/config/config.php index c84396c..edfc3c4 100644 --- a/config/config.php +++ b/config/config.php @@ -1,10 +1,4 @@ array( @@ -13,20 +7,20 @@ '#(https?://vimeo.com)/([0-9]+)#i', '#(https?://vimeo.com)/channels/staffpicks/([0-9]+)#i', ), - 'factory' => '\\RicardoFiorani\\Adapter\\Vimeo\\Factory\\VimeoServiceAdapterFactory', + 'factory' => \RicardoFiorani\Adapter\Vimeo\Factory\VimeoServiceAdapterFactory::class, ), 'Youtube' => array( 'patterns' => array( '#(?:<\>]+href=\")?(?:http://)?((?:[a-zA-Z]{1,4}\.)?youtube.com/(?:watch)?\?v=(.{11}?))[^"]*(?:\"[^\<\>]*>)?([^\<\>]*)(?:)?#', '%(?:youtube\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', ), - 'factory' => '\\RicardoFiorani\\Adapter\\Youtube\\Factory\\YoutubeServiceAdapterFactory', + 'factory' => \RicardoFiorani\Adapter\Youtube\Factory\YoutubeServiceAdapterFactory::class, ), 'Dailymotion' => array( 'patterns' => array( '#https?://www.dailymotion.com/video/([A-Za-z0-9]+)#s', ), - 'factory' => '\\RicardoFiorani\\Adapter\\Dailymotion\\Factory\\DailymotionServiceAdapterFactory', + 'factory' => \RicardoFiorani\Adapter\Dailymotion\Factory\DailymotionServiceAdapterFactory::class, ), 'Facebook' => array( 'patterns' => array( @@ -35,11 +29,11 @@ '~^https?://www\.facebook\.com/.*?/videos/(\d+)/?$~m', ), - 'factory' => '\\RicardoFiorani\\Adapter\\Facebook\\Factory\\FacebookServiceAdapterFactory', + 'factory' => \RicardoFiorani\Adapter\Facebook\Factory\FacebookServiceAdapterFactory::class, ), ), 'renderer' => array( 'name' => 'DefaultRenderer', - 'factory' => '\\RicardoFiorani\\Renderer\\Factory\\DefaultRendererFactory', + 'factory' => \RicardoFiorani\Renderer\Factory\DefaultRendererFactory::class, ) ); diff --git a/src/Adapter/AbstractServiceAdapter.php b/src/Adapter/AbstractServiceAdapter.php index ae2633b..f0e4e93 100644 --- a/src/Adapter/AbstractServiceAdapter.php +++ b/src/Adapter/AbstractServiceAdapter.php @@ -1,10 +1,4 @@ -rawUrl = $url; $this->pattern = $pattern; $this->renderer = $renderer; } - /** - * Returns the input URL. - * - * @return string - */ - public function getRawUrl() + public function getRawUrl(): string { return $this->rawUrl; } - /** - * @param string $rawUrl - */ - public function setRawUrl($rawUrl) + public function setRawUrl(string $rawUrl) { $this->rawUrl = $rawUrl; } - /** - * @return string - */ - public function getVideoId() + public function getVideoId(): string { return $this->videoId; } - /** - * @param string $videoId - */ - public function setVideoId($videoId) + public function setVideoId(string $videoId) { $this->videoId = $videoId; } - /** - * @return string - */ - public function getPattern() + public function getPattern(): string { return $this->pattern; } - /** - * @param string $pattern - */ - public function setPattern($pattern) + public function setPattern(string $pattern) { $this->pattern = $pattern; } - /** - * @return EmbedRendererInterface - */ - public function getRenderer() + public function getRenderer(): EmbedRendererInterface { return $this->renderer; } - /** - * @param EmbedRendererInterface $renderer - */ - public function setRenderer($renderer) + public function setRenderer(EmbedRendererInterface $renderer) { $this->renderer = $renderer; } /** - * @param int $width - * @param int $height - * @param bool $forceAutoplay - * @param bool $forceSecure - * - * @return string * @throws NotEmbeddableException */ - public function getEmbedCode($width, $height, $forceAutoplay = false, $forceSecure = false) - { + public function getEmbedCode( + int $width, + int $height, + bool $forceAutoplay = false, + bool $forceSecure = false + ): string { if (false === $this->isEmbeddable()) { throw new NotEmbeddableException( sprintf('The service "%s" does not provide embeddable videos', $this->getServiceName()) @@ -139,13 +83,9 @@ public function getEmbedCode($width, $height, $forceAutoplay = false, $forceSecu } /** - * Switches the protocol scheme between http and https in case you want to force https - * - * @param bool|false $forceSecure - * @return string * @throws InvalidUrlException */ - public function getScheme($forceSecure = false) + public function getScheme(bool $forceSecure = false): string { if ($forceSecure) { return 'https'; @@ -160,7 +100,7 @@ public function getScheme($forceSecure = false) throw new InvalidUrlException(sprintf('The URL %s is not valid', $this->rawUrl)); } - public function isThumbnailSizeAvailable($intendedSize) + public function isThumbnailSizeAvailable($intendedSize): bool { return in_array($intendedSize, $this->getThumbNailSizes()); } diff --git a/src/Adapter/CallableServiceAdapterFactoryInterface.php b/src/Adapter/CallableServiceAdapterFactoryInterface.php index d3bf376..b174ca1 100644 --- a/src/Adapter/CallableServiceAdapterFactoryInterface.php +++ b/src/Adapter/CallableServiceAdapterFactoryInterface.php @@ -1,10 +1,5 @@ -setVideoId($videoId); @@ -30,32 +18,17 @@ public function __construct($url, $pattern, EmbedRendererInterface $renderer) return parent::__construct($url, $pattern, $renderer); } - /** - * Returns the service name (ie: "Youtube" or "Vimeo"). - * - * @return string - */ - public function getServiceName() + public function getServiceName(): string { return 'Dailymotion'; } - /** - * Returns if the service has a thumbnail image. - * - * @return bool - */ - public function hasThumbnail() + public function hasThumbnail(): bool { return true; } - /** - * Returns all thumbnails available sizes. - * - * @return array - */ - public function getThumbNailSizes() + public function getThumbNailSizes(): array { return array( self::THUMBNAIL_DEFAULT, @@ -63,14 +36,10 @@ public function getThumbNailSizes() } /** - * @param string $size - * @param bool $forceSecure - * - * @return string * @throws InvalidThumbnailSizeException * @throws \RicardoFiorani\Adapter\Exception\InvalidUrlException */ - public function getThumbnail($size, $forceSecure = false) + public function getThumbnail(string $size, bool $forceSecure = false): string { if (false == in_array($size, $this->getThumbNailSizes())) { throw new InvalidThumbnailSizeException(); @@ -80,75 +49,54 @@ public function getThumbnail($size, $forceSecure = false) } /** - * Returns the small thumbnail's url. - * - * @param bool $forceSecure - * @return string * @throws InvalidThumbnailSizeException * @throws \RicardoFiorani\Adapter\Exception\InvalidUrlException */ - public function getSmallThumbnail($forceSecure = false) + public function getSmallThumbnail(bool $forceSecure = false): string { //Since this service does not provide other thumbnails sizes we just return the default size return $this->getThumbnail(self::THUMBNAIL_DEFAULT, $forceSecure); } /** - * Returns the medium thumbnail's url. - * - * @param bool $forceSecure - * @return string * @throws InvalidThumbnailSizeException * @throws \RicardoFiorani\Adapter\Exception\InvalidUrlException */ - public function getMediumThumbnail($forceSecure = false) + public function getMediumThumbnail(bool $forceSecure = false): string { //Since this service does not provide other thumbnails sizes we just return the default size return $this->getThumbnail(self::THUMBNAIL_DEFAULT, $forceSecure); } /** - * Returns the large thumbnail's url. - * - * @param bool $forceSecure - * @return string * @throws InvalidThumbnailSizeException * @throws \RicardoFiorani\Adapter\Exception\InvalidUrlException */ - public function getLargeThumbnail($forceSecure = false) + public function getLargeThumbnail(bool $forceSecure = false): string { //Since this service does not provide other thumbnails sizes we just return the default size return $this->getThumbnail(self::THUMBNAIL_DEFAULT, $forceSecure); } /** - * Returns the largest thumnbnaail's url. - * @param bool $forceSecure - * @return string * @throws InvalidThumbnailSizeException * @throws \RicardoFiorani\Adapter\Exception\InvalidUrlException */ - public function getLargestThumbnail($forceSecure = false) + public function getLargestThumbnail(bool $forceSecure = false): string { //Since this service does not provide other thumbnails sizes we just return the default size return $this->getThumbnail(self::THUMBNAIL_DEFAULT, $forceSecure); } /** - * @param bool $forceAutoplay - * @param bool $forceSecure - * @return string * @throws \RicardoFiorani\Adapter\Exception\InvalidUrlException */ - public function getEmbedUrl($forceAutoplay = false, $forceSecure = false) + public function getEmbedUrl(bool $forceAutoplay = false, bool $forceSecure = false): string { return $this->getScheme($forceSecure) . '://www.dailymotion.com/embed/video/' . $this->videoId . ($forceAutoplay ? '?amp&autoplay=1' : ''); } - /** - * @return bool - */ - public function isEmbeddable() + public function isEmbeddable(): bool { return true; } diff --git a/src/Adapter/Dailymotion/Factory/DailymotionServiceAdapterFactory.php b/src/Adapter/Dailymotion/Factory/DailymotionServiceAdapterFactory.php index 62368d9..f0cf295 100644 --- a/src/Adapter/Dailymotion/Factory/DailymotionServiceAdapterFactory.php +++ b/src/Adapter/Dailymotion/Factory/DailymotionServiceAdapterFactory.php @@ -1,10 +1,5 @@ -getThumbNailSizes())) { throw new InvalidThumbnailSizeException(); @@ -79,68 +45,43 @@ public function getThumbnail($size, $forceSecure = false) } /** - * Returns the small thumbnail's url. - * - * @param bool $forceSecure - * @return string * @throws ThumbnailSizeNotAvailable */ - public function getSmallThumbnail($forceSecure = false) + public function getSmallThumbnail(bool $forceSecure = false): string { throw new ThumbnailSizeNotAvailable(); } /** - * Returns the medium thumbnail's url. - * - * @param bool $forceSecure - * @return string * @throws InvalidThumbnailSizeException */ - public function getMediumThumbnail($forceSecure = false) + public function getMediumThumbnail(bool $forceSecure = false): string { return $this->getThumbnail(self::THUMBNAIL_SIZE_DEFAULT, $forceSecure); } /** - * Returns the large thumbnail's url. - * - * @param bool $forceSecure - * @return string * @throws ThumbnailSizeNotAvailable */ - public function getLargeThumbnail($forceSecure = false) + public function getLargeThumbnail(bool $forceSecure = false): string { throw new ThumbnailSizeNotAvailable(); } /** - * Returns the largest thumbnail's url. - * - * @param bool $forceSecure - * @return string * @throws InvalidThumbnailSizeException */ - public function getLargestThumbnail($forceSecure = false) + public function getLargestThumbnail(bool $forceSecure = false): string { return $this->getThumbnail(self::THUMBNAIL_SIZE_DEFAULT, $forceSecure); } - /** - * @param bool $forceAutoplay - * - * @param bool $forceSecure - * @return string - */ - public function getEmbedUrl($forceAutoplay = false, $forceSecure = false) + public function getEmbedUrl(bool $forceAutoplay = false, bool $forceSecure = false): string { return $this->getScheme($forceSecure) . '://www.facebook.com/video/embed?video_id=' . $this->getVideoId(); } - /** - * @return bool - */ - public function isEmbeddable() + public function isEmbeddable(): string { return true; } diff --git a/src/Adapter/Facebook/Factory/FacebookServiceAdapterFactory.php b/src/Adapter/Facebook/Factory/FacebookServiceAdapterFactory.php index def56c3..0e7164d 100644 --- a/src/Adapter/Facebook/Factory/FacebookServiceAdapterFactory.php +++ b/src/Adapter/Facebook/Factory/FacebookServiceAdapterFactory.php @@ -1,10 +1,5 @@ -getVideoIdByPattern($url, $pattern); $this->setVideoId($videoId); @@ -58,61 +39,36 @@ public function __construct($url, $pattern, EmbedRendererInterface $renderer) return parent::__construct($url, $pattern, $renderer); } - /** - * Returns the service name (ie: "Youtube" or "Vimeo"). - * - * @return string - */ - public function getServiceName() + public function getServiceName(): string { return 'Vimeo'; } - /** - * Returns if the service has a thumbnail image. - * - * @return bool - */ - public function hasThumbnail() + public function hasThumbnail(): bool { return false == empty($this->thumbnails); } - /** - * @return string - */ - public function getTitle() + public function getTitle(): string { return $this->title; } - /** - * @param string $title - */ - public function setTitle($title) + public function setTitle(string $title) { $this->title = $title; } - /** - * @return string - */ - public function getDescription() + public function getDescription(): string { return $this->description; } - /** - * @param string $description - */ - public function setDescription($description) + public function setDescription(string $description) { $this->description = $description; } - /** - * @param array $thumbnails - */ private function setThumbnails(array $thumbnails) { foreach ($thumbnails as $key => $thumbnail) { @@ -121,14 +77,10 @@ private function setThumbnails(array $thumbnails) } /** - * @param string $size - * @param bool $forceSecure - * @return string - * * @throws InvalidThumbnailSizeException * @throws InvalidUrlException */ - public function getThumbnail($size, $forceSecure = false) + public function getThumbnail(string $size, bool $forceSecure = false): string { if (false == in_array($size, $this->getThumbNailSizes())) { throw new InvalidThumbnailSizeException(); @@ -143,22 +95,14 @@ public function getThumbnail($size, $forceSecure = false) } /** - * @param bool $forceAutoplay - * - * @return string * @throws InvalidUrlException */ - public function getEmbedUrl($forceAutoplay = false, $forceSecure = false) + public function getEmbedUrl(bool $forceAutoplay = false, bool $forceSecure = false): string { return $this->getScheme($forceSecure) . '://player.vimeo.com/video/' . $this->getVideoId() . ($forceAutoplay ? '?autoplay=1' : ''); } - /** - * Returns all thumbnails available sizes. - * - * @return array - */ - public function getThumbNailSizes() + public function getThumbNailSizes(): array { return array( self::THUMBNAIL_SMALL, @@ -168,72 +112,47 @@ public function getThumbNailSizes() } /** - * Returns the small thumbnail's url. - * - * @param bool $forceSecure - * @return string * @throws InvalidThumbnailSizeException * @throws InvalidUrlException */ - public function getSmallThumbnail($forceSecure = false) + public function getSmallThumbnail(bool $forceSecure = false): string { return $this->getThumbnail(self::THUMBNAIL_SMALL,$forceSecure); } /** - * Returns the medium thumbnail's url. - * - * @param bool $forceSecure - * @return string * @throws InvalidThumbnailSizeException * @throws InvalidUrlException */ - public function getMediumThumbnail($forceSecure = false) + public function getMediumThumbnail(bool $forceSecure = false): string { return $this->getThumbnail(self::THUMBNAIL_MEDIUM,$forceSecure); } /** - * Returns the large thumbnail's url. - * - * @param bool $forceSecure - * @return string * @throws InvalidThumbnailSizeException * @throws InvalidUrlException */ - public function getLargeThumbnail($forceSecure = false) + public function getLargeThumbnail(bool $forceSecure = false): string { return $this->getThumbnail(self::THUMBNAIL_LARGE,$forceSecure); } /** - * Returns the largest thumnbnaail's url. - * - * @param bool $forceSecure - * @return string * @throws InvalidThumbnailSizeException * @throws InvalidUrlException */ - public function getLargestThumbnail($forceSecure = false) + public function getLargestThumbnail(bool $forceSecure = false): string { return $this->getThumbnail(self::THUMBNAIL_LARGE,$forceSecure); } - /** - * @return bool - */ - public function isEmbeddable() + public function isEmbeddable(): string { return true; } - /** - * @param string $url - * @param string $pattern - * - * @return int - */ - private function getVideoIdByPattern($url, $pattern) + private function getVideoIdByPattern(string $url, string $pattern): int { $match = array(); preg_match($pattern, $url, $match); @@ -243,15 +162,10 @@ private function getVideoIdByPattern($url, $pattern) } /** - * Uses the Vimeo video API to get video info. - * - * @todo make this better by using guzzle - * - * @return array - * + * TODO make this better by using guzzle * @throws ServiceApiNotAvailable */ - private function getVideoDataFromServiceApi() + private function getVideoDataFromServiceApi(): array { $contents = file_get_contents('http://vimeo.com/api/v2/video/' . $this->getVideoId() . '.php'); if (false === $contents) { diff --git a/src/Adapter/Youtube/Factory/YoutubeServiceAdapterFactory.php b/src/Adapter/Youtube/Factory/YoutubeServiceAdapterFactory.php index 9815da3..8a1b873 100644 --- a/src/Adapter/Youtube/Factory/YoutubeServiceAdapterFactory.php +++ b/src/Adapter/Youtube/Factory/YoutubeServiceAdapterFactory.php @@ -1,25 +1,15 @@ -getThumbNailSizes())) { throw new InvalidThumbnailSizeException(); @@ -74,10 +50,7 @@ public function getThumbnail($size, $forceSecure = false) return $this->getScheme($forceSecure) . '://img.youtube.com/vi/' . $this->getVideoId() . '/' . $size . '.jpg'; } - /** - * @return array - */ - public function getThumbNailSizes() + public function getThumbNailSizes(): array { return array( self::THUMBNAIL_DEFAULT, @@ -88,12 +61,7 @@ public function getThumbNailSizes() ); } - /** - * @param bool $forceAutoplay - * @param bool $forceSecure - * @return string - */ - public function getEmbedUrl($forceAutoplay = false, $forceSecure = false) + public function getEmbedUrl(bool $forceAutoplay = false, bool $forceSecure = false): string { $queryString = $this->generateQuerystring($forceAutoplay); @@ -105,59 +73,32 @@ public function getEmbedUrl($forceAutoplay = false, $forceSecure = false) ); } - /** - * Returns the small thumbnail's url. - * - * @return string - */ - public function getSmallThumbnail($forceSecure = false) + public function getSmallThumbnail(bool $forceSecure = false): string { return $this->getThumbnail(self::THUMBNAIL_STANDARD_DEFINITION, $forceSecure); } - /** - * Returns the medium thumbnail's url. - * - * @return string - */ - public function getMediumThumbnail($forceSecure = false) + public function getMediumThumbnail(bool $forceSecure = false): string { return $this->getThumbnail(self::THUMBNAIL_MEDIUM_QUALITY, $forceSecure); } - /** - * Returns the large thumbnail's url. - * - * @return string - */ - public function getLargeThumbnail($forceSecure = false) + public function getLargeThumbnail(bool $forceSecure = false): string { return $this->getThumbnail(self::THUMBNAIL_HIGH_QUALITY, $forceSecure); } - /** - * Returns the largest thumnbnaail's url. - * - * @return string - */ - public function getLargestThumbnail($forceSecure = false) + public function getLargestThumbnail(bool $forceSecure = false): string { return $this->getThumbnail(self::THUMBNAIL_MAX_QUALITY, $forceSecure); } - /** - * @return bool - */ - public function isEmbeddable() + public function isEmbeddable(): bool { return true; } - /** - * @param bool $forceAutoplay - * @return array - */ - private function generateQuerystring($forceAutoplay = false) + private function generateQuerystring(bool $forceAutoplay = false): array { parse_str(parse_url($this->rawUrl, PHP_URL_QUERY), $queryString); unset($queryString['v']); diff --git a/src/Container/Exception/DuplicatedServiceNameException.php b/src/Container/Exception/DuplicatedServiceNameException.php index 329a7ab..4bce384 100644 --- a/src/Container/Exception/DuplicatedServiceNameException.php +++ b/src/Container/Exception/DuplicatedServiceNameException.php @@ -1,10 +1,5 @@ -registerFromConfig($config); @@ -57,10 +26,6 @@ public function __construct(array $config = array()) } /** - * Loads de default config file. - * - * @param array $config - * * @throws DuplicatedServiceNameException */ private function registerFromConfig(array $config) @@ -68,19 +33,14 @@ 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|callable $factory - * * @throws DuplicatedServiceNameException */ - public function registerService($serviceName, array $regex, $factory) + public function registerService(string $serviceName, array $regex, string $factory) { if ($this->hasService($serviceName)) { throw new DuplicatedServiceNameException( @@ -93,65 +53,39 @@ public function registerService($serviceName, array $regex, $factory) $this->factories[$serviceName] = $factory; } - /** - * @param string $rendererName - * @param string $rendererFactory - */ - public function setRenderer($rendererName, $rendererFactory) + public function setRenderer(string $rendererName, string $rendererFactory) { $this->rendererName = $rendererName; $factory = new $rendererFactory(); $this->renderer = $factory(); } - /** - * @return EmbedRendererInterface - */ - public function getRenderer() + public function getRenderer(): EmbedRendererInterface { return $this->renderer; } - /** - * @return array - */ - public function getServiceNameList() + public function getServiceNameList(): array { return $this->services; } - /** - * @param string $serviceName - * - * @return bool - */ - public function hasService($serviceName) + public function hasService($serviceName): bool { return in_array($serviceName, $this->services); } - /** - * @return array - */ - public function getServices() + public function getServices(): array { return $this->services; } - /** - * @return array - */ - public function getPatterns() + public function getPatterns(): array { return $this->patterns; } - /** - * @param string $serviceName - * - * @return CallableServiceAdapterFactoryInterface - */ - public function getFactory($serviceName) + public function getFactory($serviceName): CallableServiceAdapterFactoryInterface { $factory = new $this->factories[$serviceName](); if (isset($this->instantiatedFactories[$serviceName])) { diff --git a/src/Exception/ThumbnailSizeNotAvailable.php b/src/Exception/ThumbnailSizeNotAvailable.php index ba4beb2..51212ea 100644 --- a/src/Exception/ThumbnailSizeNotAvailable.php +++ b/src/Exception/ThumbnailSizeNotAvailable.php @@ -1,10 +1,5 @@ -serviceContainer = ServicesContainerFactory::createNewServiceMatcher(); } /** - * @param string $url - * - * @return VideoAdapterInterface - * * @throws VideoServiceNotCompatibleException */ - public function parse($url) + public function parse($url): VideoAdapterInterface { if (isset($this->parsedUrls[$url])) { return $this->parsedUrls[$url]; @@ -62,17 +45,11 @@ public function parse($url) ); } - /** - * @return ServicesContainer - */ - public function getServiceContainer() + public function getServiceContainer(): ServicesContainer { return $this->serviceContainer; } - /** - * @param ServicesContainer $serviceContainer - */ public function setServiceContainer(ServicesContainer $serviceContainer) { $this->serviceContainer = $serviceContainer; diff --git a/src/Renderer/DefaultRenderer.php b/src/Renderer/DefaultRenderer.php index cfb8cc6..58b5029 100644 --- a/src/Renderer/DefaultRenderer.php +++ b/src/Renderer/DefaultRenderer.php @@ -1,7 +1,4 @@ - +HTML; + + return sprintf( - '', + $htmlTag, $width, $height, addslashes($embedUrl) diff --git a/src/Renderer/EmbedRendererInterface.php b/src/Renderer/EmbedRendererInterface.php index 4cd4388..a1c0aa8 100644 --- a/src/Renderer/EmbedRendererInterface.php +++ b/src/Renderer/EmbedRendererInterface.php @@ -1,20 +1,8 @@ - Date: Sat, 28 Jul 2018 13:03:33 +0200 Subject: [PATCH 03/11] finished php 7 new features implementation --- documentation/RegisteringANewService.md | 2 +- .../Facebook/FacebookServiceAdapter.php | 2 +- .../Factory/VimeoServiceAdapterFactory.php | 1 + src/Adapter/Vimeo/VimeoServiceAdapter.php | 4 +-- .../Factory/ServicesContainerFactory.php | 2 +- src/Container/ServicesContainer.php | 9 ++++-- .../Adapter/DailymotionServiceAdapterTest.php | 9 ++---- test/Adapter/FacebookServiceAdapterTest.php | 14 ++++----- test/Adapter/VimeoServiceAdapterTest.php | 9 ++---- test/Adapter/YoutubeServiceAdapterTest.php | 9 ++---- test/Container/ServicesContainerTest.php | 29 +++++++++---------- test/Detector/VideoServiceMatcherTest.php | 27 +++++++++-------- test/Renderer/DefaultRendererTest.php | 6 ---- 13 files changed, 50 insertions(+), 73 deletions(-) diff --git a/documentation/RegisteringANewService.md b/documentation/RegisteringANewService.md index d9798d5..baa59fc 100644 --- a/documentation/RegisteringANewService.md +++ b/documentation/RegisteringANewService.md @@ -188,7 +188,7 @@ $patterns = array( ); //Register the new service -$vsd->getServiceContainer()->registerService($serviceName, $patterns, MyVendor\ServiceAdapter\Factory\DailymotionServiceAdapterFactory::class); +$vsd->getServiceContainer()->registerService($serviceName, $patterns, new MyVendor\ServiceAdapter\Factory\DailymotionServiceAdapterFactory()); //This will get you an DailymotionServiceAdapter $video = $vsd->parse('http://www.dailymotion.com/video/x33ncwc_kittens-fight-in-tiny-boxing-ring_animals'); diff --git a/src/Adapter/Facebook/FacebookServiceAdapter.php b/src/Adapter/Facebook/FacebookServiceAdapter.php index 7fdea39..34afbf4 100644 --- a/src/Adapter/Facebook/FacebookServiceAdapter.php +++ b/src/Adapter/Facebook/FacebookServiceAdapter.php @@ -81,7 +81,7 @@ public function getEmbedUrl(bool $forceAutoplay = false, bool $forceSecure = fal return $this->getScheme($forceSecure) . '://www.facebook.com/video/embed?video_id=' . $this->getVideoId(); } - public function isEmbeddable(): string + public function isEmbeddable(): bool { return true; } diff --git a/src/Adapter/Vimeo/Factory/VimeoServiceAdapterFactory.php b/src/Adapter/Vimeo/Factory/VimeoServiceAdapterFactory.php index 52be967..bf32703 100644 --- a/src/Adapter/Vimeo/Factory/VimeoServiceAdapterFactory.php +++ b/src/Adapter/Vimeo/Factory/VimeoServiceAdapterFactory.php @@ -3,6 +3,7 @@ namespace RicardoFiorani\Adapter\Vimeo\Factory; use RicardoFiorani\Adapter\CallableServiceAdapterFactoryInterface; +use RicardoFiorani\Adapter\VideoAdapterInterface; use RicardoFiorani\Adapter\Vimeo\VimeoServiceAdapter; use RicardoFiorani\Renderer\EmbedRendererInterface; diff --git a/src/Adapter/Vimeo/VimeoServiceAdapter.php b/src/Adapter/Vimeo/VimeoServiceAdapter.php index d65506b..338ce89 100644 --- a/src/Adapter/Vimeo/VimeoServiceAdapter.php +++ b/src/Adapter/Vimeo/VimeoServiceAdapter.php @@ -147,12 +147,12 @@ public function getLargestThumbnail(bool $forceSecure = false): string return $this->getThumbnail(self::THUMBNAIL_LARGE,$forceSecure); } - public function isEmbeddable(): string + public function isEmbeddable(): bool { return true; } - private function getVideoIdByPattern(string $url, string $pattern): int + private function getVideoIdByPattern(string $url, string $pattern): string { $match = array(); preg_match($pattern, $url, $match); diff --git a/src/Container/Factory/ServicesContainerFactory.php b/src/Container/Factory/ServicesContainerFactory.php index 7a3b819..331a7bd 100644 --- a/src/Container/Factory/ServicesContainerFactory.php +++ b/src/Container/Factory/ServicesContainerFactory.php @@ -14,7 +14,7 @@ public function __invoke(): ServicesContainer return $servicesContainer; } - public static function createNewServiceMatcher(): ServicesContainerFactory + public static function createNewServiceMatcher(): ServicesContainer { $factory = new self(); diff --git a/src/Container/ServicesContainer.php b/src/Container/ServicesContainer.php index 9bf9b8d..39972df 100644 --- a/src/Container/ServicesContainer.php +++ b/src/Container/ServicesContainer.php @@ -31,7 +31,7 @@ public function __construct(array $config = []) private function registerFromConfig(array $config) { foreach ($config['services'] as $serviceName => $serviceConfig) { - $this->registerService($serviceName, $serviceConfig['patterns'], $serviceConfig['factory']); + $this->registerService($serviceName, $serviceConfig['patterns'], new $serviceConfig['factory']); } $this->setRenderer($config['renderer']['name'], $config['renderer']['factory']); @@ -40,11 +40,14 @@ private function registerFromConfig(array $config) /** * @throws DuplicatedServiceNameException */ - public function registerService(string $serviceName, array $regex, string $factory) + public function registerService(string $serviceName, array $regex, callable $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/Adapter/DailymotionServiceAdapterTest.php b/test/Adapter/DailymotionServiceAdapterTest.php index 2e039bd..6dd957f 100644 --- a/test/Adapter/DailymotionServiceAdapterTest.php +++ b/test/Adapter/DailymotionServiceAdapterTest.php @@ -1,15 +1,10 @@ getMockingObject($url); - $this->setExpectedException('\\RicardoFiorani\\Adapter\\Exception\\InvalidThumbnailSizeException'); + $this->expectException(InvalidThumbnailSizeException::class); $dailymotionVideo->getThumbnail('This Size does not exists :)'); } diff --git a/test/Adapter/FacebookServiceAdapterTest.php b/test/Adapter/FacebookServiceAdapterTest.php index e77845c..0879130 100644 --- a/test/Adapter/FacebookServiceAdapterTest.php +++ b/test/Adapter/FacebookServiceAdapterTest.php @@ -1,15 +1,11 @@ getMockingObject($url); - $this->setExpectedException('\\RicardoFiorani\\Adapter\\Exception\\InvalidThumbnailSizeException'); + $this->expectException(InvalidThumbnailSizeException::class); $facebookVideo->getThumbnail('This Size does not exists :)'); } @@ -78,7 +74,7 @@ public function testThrowsExceptionOnRequestThumbnailWithAnInvalidSize($url) public function testGetSmallThumbnailThrowsException($url) { $facebookVideo = $this->getMockingObject($url); - $this->setExpectedException('\\RicardoFiorani\\Exception\\ThumbnailSizeNotAvailable'); + $this->expectException(ThumbnailSizeNotAvailable::class); $facebookVideo->getSmallThumbnail(); } @@ -89,7 +85,7 @@ public function testGetSmallThumbnailThrowsException($url) public function testGetLargeThumbnailThrowsException($url) { $facebookVideo = $this->getMockingObject($url); - $this->setExpectedException('\\RicardoFiorani\\Exception\\ThumbnailSizeNotAvailable'); + $this->expectException(ThumbnailSizeNotAvailable::class); $facebookVideo->getLargeThumbnail(); } diff --git a/test/Adapter/VimeoServiceAdapterTest.php b/test/Adapter/VimeoServiceAdapterTest.php index d62108d..6ddae16 100644 --- a/test/Adapter/VimeoServiceAdapterTest.php +++ b/test/Adapter/VimeoServiceAdapterTest.php @@ -1,15 +1,10 @@ getMockingObject($url); - $this->setExpectedException('\\RicardoFiorani\\Adapter\\Exception\\InvalidThumbnailSizeException'); + $this->expectException(InvalidThumbnailSizeException::class); $vimeoVideo->getThumbnail('This Size does not exists :)'); } diff --git a/test/Adapter/YoutubeServiceAdapterTest.php b/test/Adapter/YoutubeServiceAdapterTest.php index a041635..b1567b4 100644 --- a/test/Adapter/YoutubeServiceAdapterTest.php +++ b/test/Adapter/YoutubeServiceAdapterTest.php @@ -1,15 +1,10 @@ getMockingObject($url); - $this->setExpectedException('\\RicardoFiorani\\Adapter\\Exception\\InvalidThumbnailSizeException'); + $this->expectException(InvalidThumbnailSizeException::class); $youtubeVideo->getThumbnail('This Size does not exists :)'); } diff --git a/test/Container/ServicesContainerTest.php b/test/Container/ServicesContainerTest.php index 03d7516..aaca9c8 100644 --- a/test/Container/ServicesContainerTest.php +++ b/test/Container/ServicesContainerTest.php @@ -1,16 +1,14 @@ getMockConfig(); $serviceContainer = $this->createServiceContainer($config); $this->assertTrue($serviceContainer->hasService('Youtube')); - $this->assertInstanceOf('\\RicardoFiorani\\Renderer\\DefaultRenderer', $serviceContainer->getRenderer()); + $this->assertInstanceOf(DefaultRenderer::class, $serviceContainer->getRenderer()); } public function testServiceContainerServiceRegistrationByInjection() { $serviceContainer = $this->createServiceContainer(); - $serviceContainer->registerService('TestService', array('#testPattern#'), function () { + $serviceContainer->registerService('TestService', ['#testPattern#'], function () { // @todo test the injected service maybe ? }); $this->assertContains('TestService', $serviceContainer->getServiceNameList()); - $this->setExpectedException('\\RicardoFiorani\\Container\\Exception\\DuplicatedServiceNameException'); - $serviceContainer->registerService('TestService', array('#testPattern#'), function () { + $this->expectException(DuplicatedServiceNameException::class); + $serviceContainer->registerService('TestService', ['#testPattern#'], function () { }); } @@ -44,14 +42,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(YoutubeServiceAdapterFactory::class, $factory); $alreadyInstantiatedFactory = $serviceContainer->getFactory('Youtube'); - $this->assertEquals($factory,$alreadyInstantiatedFactory); + $this->assertEquals($factory, $alreadyInstantiatedFactory); } /** @@ -76,12 +75,12 @@ private function getMockConfig() '#(?:<\>]+href=\")?(?:http://)?((?:[a-zA-Z]{1,4}\.)?youtube.com/(?:watch)?\?v=(.{11}?))[^"]*(?:\"[^\<\>]*>)?([^\<\>]*)(?:)?#', '%(?:youtube\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', ), - 'factory' => '\\RicardoFiorani\\Adapter\\Youtube\\Factory\\YoutubeServiceAdapterFactory', + 'factory' => YoutubeServiceAdapterFactory::class, ), ), 'renderer' => array( 'name' => 'DefaultRenderer', - 'factory' => '\\RicardoFiorani\\Renderer\\Factory\\DefaultRendererFactory', + 'factory' => DefaultRendererFactory::class, ) ); } diff --git a/test/Detector/VideoServiceMatcherTest.php b/test/Detector/VideoServiceMatcherTest.php index b8531a7..55526d8 100644 --- a/test/Detector/VideoServiceMatcherTest.php +++ b/test/Detector/VideoServiceMatcherTest.php @@ -1,15 +1,14 @@ array( 'https://www.youtube.com/watch?v=mWRsgZuwf_8', - '\\RicardoFiorani\\Adapter\\Youtube\\YoutubeServiceAdapter', + YoutubeServiceAdapter::class, ), 'Short Youtube URL' => array( 'https://youtu.be/JMLBOKVfHaA', - 'RicardoFiorani\\Adapter\\Youtube\\YoutubeServiceAdapter', + YoutubeServiceAdapter::class, ), 'Embed Youtube URL' => array( '', - '\\RicardoFiorani\\Adapter\\Youtube\\YoutubeServiceAdapter', + YoutubeServiceAdapter::class, ), 'Common Vimeo URL' => array( 'https://vimeo.com/137781541', - '\\RicardoFiorani\\Adapter\\Vimeo\\VimeoServiceAdapter', + VimeoServiceAdapter::class, ), 'Commom Dailymotion URL' => array( 'http://www.dailymotion.com/video/x332a71_que-categoria-jogador-lucas-lima-faz-golaco-em-treino-do-santos_sport', - '\\RicardoFiorani\\Adapter\\Dailymotion\\DailymotionServiceAdapter', + DailymotionServiceAdapter::class, ), 'Commom Facebook Video URL' => array( 'https://www.facebook.com/RantPets/videos/583336855137988/', - '\\RicardoFiorani\\Adapter\\Facebook\\FacebookServiceAdapter', + FacebookServiceAdapter::class, ) ); } @@ -69,7 +68,7 @@ public function videoUrlProvider() public function testThrowsExceptionOnInvalidUrl($url) { $detector = new VideoServiceMatcher(); - $this->setExpectedException('\\RicardoFiorani\\Matcher\\Exception\\VideoServiceNotCompatibleException'); + $this->expectException(VideoServiceNotCompatibleException::class); $video = $detector->parse($url); } @@ -110,7 +109,7 @@ public function testServiceDetectorDontReparseSameUrl($url) public function testServiceContainerGetter() { $detector = new VideoServiceMatcher(); - $this->assertInstanceOf('RicardoFiorani\\Container\\ServicesContainer', $detector->getServiceContainer()); + $this->assertInstanceOf(ServicesContainer::class, $detector->getServiceContainer()); } /** diff --git a/test/Renderer/DefaultRendererTest.php b/test/Renderer/DefaultRendererTest.php index a88c8d3..dd8bc62 100644 --- a/test/Renderer/DefaultRendererTest.php +++ b/test/Renderer/DefaultRendererTest.php @@ -1,10 +1,4 @@ Date: Sat, 28 Jul 2018 13:57:09 +0200 Subject: [PATCH 04/11] added php 7.0 and updated docs --- .travis.yml | 1 + composer.json | 2 +- documentation/IntegratingYourOwnRenderer.md | 2 +- src/Container/ServicesContainer.php | 7 +++---- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/.travis.yml b/.travis.yml index ba120b8..62c2731 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,6 +4,7 @@ sudo: false # Setup build matrix language: php php: + - 7.0 - 7.1 - 7.2 diff --git a/composer.json b/composer.json index f2b3367..5799802 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,7 @@ } ], "require": { - "php": ">=7.1", + "php": ">=7.0", "mockery/mockery": "^1.1" }, "autoload": { diff --git a/documentation/IntegratingYourOwnRenderer.md b/documentation/IntegratingYourOwnRenderer.md index a256b40..b85999e 100644 --- a/documentation/IntegratingYourOwnRenderer.md +++ b/documentation/IntegratingYourOwnRenderer.md @@ -68,7 +68,7 @@ require __DIR__ . '/vendor/autoload.php'; $vsm = new VideoServiceMatcher(); //This is where you attach your own renderer to be used instead of the default one -$vsm->getServiceContainer()->setRenderer('MyOwnRenderer', MyVendor\MyRenderer\Factory\MyOwnRendererFactory::class); +$vsm->getServiceContainer()->setRenderer('MyOwnRenderer', new MyVendor\MyRenderer\Factory\MyOwnRendererFactory()); $video = $vsm->parse('https://www.youtube.com/watch?v=PkOcm_XaWrw'); diff --git a/src/Container/ServicesContainer.php b/src/Container/ServicesContainer.php index 39972df..a2ff65c 100644 --- a/src/Container/ServicesContainer.php +++ b/src/Container/ServicesContainer.php @@ -34,7 +34,7 @@ private function registerFromConfig(array $config) $this->registerService($serviceName, $serviceConfig['patterns'], new $serviceConfig['factory']); } - $this->setRenderer($config['renderer']['name'], $config['renderer']['factory']); + $this->setRenderer($config['renderer']['name'], new $config['renderer']['factory']); } /** @@ -56,11 +56,10 @@ public function registerService(string $serviceName, array $regex, callable $fac $this->factories[$serviceName] = $factory; } - public function setRenderer(string $rendererName, string $rendererFactory) + public function setRenderer(string $rendererName, callable $rendererFactory) { $this->rendererName = $rendererName; - $factory = new $rendererFactory(); - $this->renderer = $factory(); + $this->renderer = $rendererFactory(); } public function getRenderer(): EmbedRendererInterface From a151cce2636c46b9c34e7ba55967dcbc23dcb85b Mon Sep 17 00:00:00 2001 From: Ricardo Fiorani Date: Sat, 28 Jul 2018 14:09:13 +0200 Subject: [PATCH 05/11] no php7.0 for you... also some dev dependencies updated --- .travis.yml | 1 - README.md | 5 ----- composer.json | 6 +++--- 3 files changed, 3 insertions(+), 9 deletions(-) diff --git a/.travis.yml b/.travis.yml index 62c2731..ba120b8 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,7 +4,6 @@ sudo: false # Setup build matrix language: php php: - - 7.0 - 7.1 - 7.2 diff --git a/README.md b/README.md index 53331d4..e835d1e 100644 --- a/README.md +++ b/README.md @@ -81,11 +81,6 @@ A Fully functional example can be found [Here](https://github.com/ricardofiorani * Facebook Videos ## Currently Supported PHP Versions -* PHP 5.3 -* PHP 5.4 -* PHP 5.5 -* PHP 5.6 -* PHP 7.0 * PHP 7.1 * PHP 7.2 diff --git a/composer.json b/composer.json index 5799802..48cfbd9 100644 --- a/composer.json +++ b/composer.json @@ -3,7 +3,8 @@ "description": "A Simple and efficient PHP Video URL Parser that gives you thumbnails and embed codes for various services as Youtube, Vimeo, DailyMotion and Facebook", "require-dev": { "phpunit/phpunit": "^7.2", - "fabpot/php-cs-fixer" : ">= 1.0" + "friendsofphp/php-cs-fixer": "^2.12", + "mockery/mockery": "^1.1" }, "license": "MIT", "authors": [ @@ -13,8 +14,7 @@ } ], "require": { - "php": ">=7.0", - "mockery/mockery": "^1.1" + "php": ">=7.1" }, "autoload": { "psr-4": { From 21318082a63fea9dae34c1bb800eaa94b7757baf Mon Sep 17 00:00:00 2001 From: Ricardo Fiorani Date: Sat, 28 Jul 2018 14:47:22 +0200 Subject: [PATCH 06/11] wip --- documentation/IntegratingYourOwnRenderer.md | 2 +- src/Adapter/Exception/InvalidUrlException.php | 3 ++- src/Container/ServicesContainer.php | 4 +--- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/documentation/IntegratingYourOwnRenderer.md b/documentation/IntegratingYourOwnRenderer.md index b85999e..dc1de52 100644 --- a/documentation/IntegratingYourOwnRenderer.md +++ b/documentation/IntegratingYourOwnRenderer.md @@ -68,7 +68,7 @@ require __DIR__ . '/vendor/autoload.php'; $vsm = new VideoServiceMatcher(); //This is where you attach your own renderer to be used instead of the default one -$vsm->getServiceContainer()->setRenderer('MyOwnRenderer', new MyVendor\MyRenderer\Factory\MyOwnRendererFactory()); +$vsm->getServiceContainer()->setRenderer(new MyVendor\MyRenderer\Factory\MyOwnRendererFactory()); $video = $vsm->parse('https://www.youtube.com/watch?v=PkOcm_XaWrw'); diff --git a/src/Adapter/Exception/InvalidUrlException.php b/src/Adapter/Exception/InvalidUrlException.php index cb8cc9a..e688559 100644 --- a/src/Adapter/Exception/InvalidUrlException.php +++ b/src/Adapter/Exception/InvalidUrlException.php @@ -6,4 +6,5 @@ class InvalidUrlException extends Exception { -} \ No newline at end of file + +} diff --git a/src/Container/ServicesContainer.php b/src/Container/ServicesContainer.php index a2ff65c..f36d351 100644 --- a/src/Container/ServicesContainer.php +++ b/src/Container/ServicesContainer.php @@ -13,7 +13,6 @@ class ServicesContainer private $factories = array(); private $instantiatedFactories = array(); private $renderer; - private $rendererName; /** * @throws DuplicatedServiceNameException @@ -56,9 +55,8 @@ public function registerService(string $serviceName, array $regex, callable $fac $this->factories[$serviceName] = $factory; } - public function setRenderer(string $rendererName, callable $rendererFactory) + public function setRenderer(callable $rendererFactory) { - $this->rendererName = $rendererName; $this->renderer = $rendererFactory(); } From ec6a669081c3bc6ff8fd7119e5c720c4cb16a78d Mon Sep 17 00:00:00 2001 From: Ricardo Fiorani Date: Sat, 28 Jul 2018 15:00:17 +0200 Subject: [PATCH 07/11] wip --- composer.json | 3 +++ config/config.php | 1 - src/Container/ServicesContainer.php | 2 +- 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 48cfbd9..fc23110 100644 --- a/composer.json +++ b/composer.json @@ -20,5 +20,8 @@ "psr-4": { "RicardoFiorani\\": "src/" } + }, + "scripts": { + "test": "php vendor/bin/phpunit" } } diff --git a/config/config.php b/config/config.php index edfc3c4..2a648c1 100644 --- a/config/config.php +++ b/config/config.php @@ -33,7 +33,6 @@ ), ), 'renderer' => array( - 'name' => 'DefaultRenderer', 'factory' => \RicardoFiorani\Renderer\Factory\DefaultRendererFactory::class, ) ); diff --git a/src/Container/ServicesContainer.php b/src/Container/ServicesContainer.php index f36d351..a8f4c2b 100644 --- a/src/Container/ServicesContainer.php +++ b/src/Container/ServicesContainer.php @@ -33,7 +33,7 @@ private function registerFromConfig(array $config) $this->registerService($serviceName, $serviceConfig['patterns'], new $serviceConfig['factory']); } - $this->setRenderer($config['renderer']['name'], new $config['renderer']['factory']); + $this->setRenderer(new $config['renderer']['factory']); } /** From 812b717fbc4bab14bd928c2b848f4d5266a65bb3 Mon Sep 17 00:00:00 2001 From: Renan Date: Wed, 29 Aug 2018 18:20:20 +0200 Subject: [PATCH 08/11] allow PSR7 UriInterface as argument closes #11 --- composer.json | 3 ++- src/Matcher/VideoServiceMatcher.php | 2 ++ test/Detector/VideoServiceMatcherTest.php | 8 ++++++++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index fc23110..2e77541 100644 --- a/composer.json +++ b/composer.json @@ -4,7 +4,8 @@ "require-dev": { "phpunit/phpunit": "^7.2", "friendsofphp/php-cs-fixer": "^2.12", - "mockery/mockery": "^1.1" + "mockery/mockery": "^1.1", + "zendframework/zend-diactoros": "^1.8" }, "license": "MIT", "authors": [ diff --git a/src/Matcher/VideoServiceMatcher.php b/src/Matcher/VideoServiceMatcher.php index 5824fd1..21bc505 100644 --- a/src/Matcher/VideoServiceMatcher.php +++ b/src/Matcher/VideoServiceMatcher.php @@ -18,10 +18,12 @@ public function __construct() } /** + * @param string|\Psr\Http\Message\UriInterface $url * @throws VideoServiceNotCompatibleException */ public function parse($url): VideoAdapterInterface { + $url = (string) $url; if (isset($this->parsedUrls[$url])) { return $this->parsedUrls[$url]; } diff --git a/test/Detector/VideoServiceMatcherTest.php b/test/Detector/VideoServiceMatcherTest.php index 55526d8..e625285 100644 --- a/test/Detector/VideoServiceMatcherTest.php +++ b/test/Detector/VideoServiceMatcherTest.php @@ -5,12 +5,14 @@ use PHPUnit\Framework\TestCase; use RicardoFiorani\Adapter\Dailymotion\DailymotionServiceAdapter; use RicardoFiorani\Adapter\Facebook\FacebookServiceAdapter; +use RicardoFiorani\Adapter\VideoAdapterInterface; use RicardoFiorani\Adapter\Vimeo\VimeoServiceAdapter; use RicardoFiorani\Adapter\Youtube\YoutubeServiceAdapter; use RicardoFiorani\Container\Factory\ServicesContainerFactory; use RicardoFiorani\Container\ServicesContainer; use RicardoFiorani\Matcher\VideoServiceMatcher; use RicardoFiorani\Matcher\Exception\VideoServiceNotCompatibleException; +use Zend\Diactoros\Uri; class VideoServiceMatcherTest extends TestCase { @@ -123,4 +125,10 @@ public function testServiceContainerSetter() $this->assertSame($serviceContainer, $detector->getServiceContainer()); } + public function testCanParsePsr7Uri() + { + $detector = new VideoServiceMatcher(); + $video = $detector->parse(new Uri('https://www.youtube.com/watch?v=PkOcm_XaWrw')); + $this->assertInstanceOf(VideoAdapterInterface::class, $video); + } } From ae6d69896fe20211c8769ab9bdf831011c7edca8 Mon Sep 17 00:00:00 2001 From: Renan Date: Sat, 1 Sep 2018 13:24:30 +0200 Subject: [PATCH 09/11] Add PSR-16 support --- composer.json | 4 +++- src/Matcher/VideoServiceMatcher.php | 22 +++++++++++++++++----- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/composer.json b/composer.json index 2e77541..614fa88 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,9 @@ } ], "require": { - "php": ">=7.1" + "php": ">=7.1", + "psr/simple-cache": "^1.0", + "roave/doctrine-simplecache": "^2.1" }, "autoload": { "psr-4": { diff --git a/src/Matcher/VideoServiceMatcher.php b/src/Matcher/VideoServiceMatcher.php index 21bc505..0decf77 100644 --- a/src/Matcher/VideoServiceMatcher.php +++ b/src/Matcher/VideoServiceMatcher.php @@ -2,19 +2,28 @@ namespace RicardoFiorani\Matcher; +use Doctrine\Common\Cache\ArrayCache; +use Psr\SimpleCache\CacheItemInterface; use RicardoFiorani\Adapter\VideoAdapterInterface; use RicardoFiorani\Container\Factory\ServicesContainerFactory; use RicardoFiorani\Container\ServicesContainer; use RicardoFiorani\Matcher\Exception\VideoServiceNotCompatibleException; +use Roave\DoctrineSimpleCache\SimpleCacheAdapter; class VideoServiceMatcher { private $serviceContainer; - private $parsedUrls = array(); + private $cache; public function __construct() { $this->serviceContainer = ServicesContainerFactory::createNewServiceMatcher(); + $this->cache = new SimpleCacheAdapter(new ArrayCache()); + } + + public function setCache(CacheItemInterface $cache) + { + $this->cache = $cache; } /** @@ -24,8 +33,9 @@ public function __construct() public function parse($url): VideoAdapterInterface { $url = (string) $url; - if (isset($this->parsedUrls[$url])) { - return $this->parsedUrls[$url]; + $cacheKey = hash('sha256', $url); + if ($this->cache->has($cacheKey)) { + return $this->cache->get($cacheKey); } /** @var array $patterns */ @@ -35,9 +45,11 @@ public function parse($url): VideoAdapterInterface foreach ($patterns as $pattern) { if (false != preg_match($pattern, $url)) { $factory = $this->getServiceContainer()->getFactory($serviceName); + $renderer = $this->getServiceContainer()->getRenderer(); + $adapter = $factory($url, $pattern, $renderer); + $this->cache->set($cacheKey, $adapter); - return $this->parsedUrls[$url] = $factory($url, $pattern, - $this->getServiceContainer()->getRenderer()); + return $adapter; } } } From bbf1997017ccf4f95084e9681564b75335e57d9b Mon Sep 17 00:00:00 2001 From: Renan Date: Sun, 9 Sep 2018 11:59:00 +0200 Subject: [PATCH 10/11] use CRC32 or MD5 instead of sha256 for cache key generator --- src/Matcher/VideoServiceMatcher.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Matcher/VideoServiceMatcher.php b/src/Matcher/VideoServiceMatcher.php index 0decf77..07ddd45 100644 --- a/src/Matcher/VideoServiceMatcher.php +++ b/src/Matcher/VideoServiceMatcher.php @@ -33,7 +33,7 @@ public function setCache(CacheItemInterface $cache) public function parse($url): VideoAdapterInterface { $url = (string) $url; - $cacheKey = hash('sha256', $url); + $cacheKey = (string) crc32($url); if ($this->cache->has($cacheKey)) { return $this->cache->get($cacheKey); } From bb5e16f75f44bbdfa718c8af4a8acfa8bc6858f3 Mon Sep 17 00:00:00 2001 From: Renan Date: Sun, 9 Sep 2018 12:02:34 +0200 Subject: [PATCH 11/11] use md5 for cache key --- src/Matcher/VideoServiceMatcher.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Matcher/VideoServiceMatcher.php b/src/Matcher/VideoServiceMatcher.php index 07ddd45..8acc285 100644 --- a/src/Matcher/VideoServiceMatcher.php +++ b/src/Matcher/VideoServiceMatcher.php @@ -33,7 +33,7 @@ public function setCache(CacheItemInterface $cache) public function parse($url): VideoAdapterInterface { $url = (string) $url; - $cacheKey = (string) crc32($url); + $cacheKey = hash('md5', $url); if ($this->cache->has($cacheKey)) { return $this->cache->get($cacheKey); }