From f0d362a474be04e85a8ed77f9f8ff185eda56a03 Mon Sep 17 00:00:00 2001 From: Raymond Date: Tue, 27 Jun 2017 13:39:46 +0200 Subject: [PATCH 1/3] Repeat process if no embed code was found, but only if alternative URL gets valid response. --- src/Embed.php | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/Embed.php b/src/Embed.php index b197cb68..005a9e51 100644 --- a/src/Embed.php +++ b/src/Embed.php @@ -77,7 +77,12 @@ public static function create($url, array $config = null, DispatcherInterface $d $to = preg_replace('|^(\w+://)|', '', rtrim($info->url, '/')); if ($from !== $to && empty($info->code)) { - return self::process(Url::create($info->url), $config, $dispatcher); + + //except new result if valid + if($new_info = self::process(Url::create($info->url), $config, $dispatcher, TRUE)){ + $info = $new_info; + } + } return $info; @@ -94,7 +99,7 @@ public static function create($url, array $config = null, DispatcherInterface $d * * @return Adapter */ - private static function process(Url $url, array $config, DispatcherInterface $dispatcher) + private static function process(Url $url, array $config, DispatcherInterface $dispatcher, $no_exception=FALSE) { $response = $dispatcher->dispatch($url); @@ -114,7 +119,12 @@ private static function process(Url $url, array $config, DispatcherInterface $di if (Adapters\Webpage::check($response)) { return new Adapters\Webpage($response, $config, $dispatcher); } - + + //Return false instead of throwing exception for second tries + if($no_exception){ + return false; + } + if ($response->getError() === null) { $exception = new Exceptions\InvalidUrlException(sprintf("Invalid url '%s' (Status code %s)", (string) $url, $response->getStatusCode())); } else { From ace9847de641afa25172aa9ffe3e1fdad787353f Mon Sep 17 00:00:00 2001 From: Raymond Date: Tue, 27 Jun 2017 19:43:57 +0200 Subject: [PATCH 2/3] using try/catch --- src/Embed.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/Embed.php b/src/Embed.php index 005a9e51..d52b1c2a 100644 --- a/src/Embed.php +++ b/src/Embed.php @@ -78,9 +78,11 @@ public static function create($url, array $config = null, DispatcherInterface $d if ($from !== $to && empty($info->code)) { - //except new result if valid - if($new_info = self::process(Url::create($info->url), $config, $dispatcher, TRUE)){ - $info = $new_info; + //accept new result if valid + try { + return self::process(Url::create($info->url), $config, $dispatcher, TRUE); + } catch (\Exception $e) { + return $info; } } From 1ef0735dbb5fc5ae887267c8e8b21c6e7b1168ab Mon Sep 17 00:00:00 2001 From: Raymond Date: Tue, 27 Jun 2017 20:13:28 +0200 Subject: [PATCH 3/3] and... taking out superfluous code:-) --- src/Embed.php | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/Embed.php b/src/Embed.php index d52b1c2a..af5dc992 100644 --- a/src/Embed.php +++ b/src/Embed.php @@ -80,7 +80,7 @@ public static function create($url, array $config = null, DispatcherInterface $d //accept new result if valid try { - return self::process(Url::create($info->url), $config, $dispatcher, TRUE); + return self::process(Url::create($info->url), $config, $dispatcher); } catch (\Exception $e) { return $info; } @@ -101,7 +101,7 @@ public static function create($url, array $config = null, DispatcherInterface $d * * @return Adapter */ - private static function process(Url $url, array $config, DispatcherInterface $dispatcher, $no_exception=FALSE) + private static function process(Url $url, array $config, DispatcherInterface $dispatcher) { $response = $dispatcher->dispatch($url); @@ -122,11 +122,6 @@ private static function process(Url $url, array $config, DispatcherInterface $di return new Adapters\Webpage($response, $config, $dispatcher); } - //Return false instead of throwing exception for second tries - if($no_exception){ - return false; - } - if ($response->getError() === null) { $exception = new Exceptions\InvalidUrlException(sprintf("Invalid url '%s' (Status code %s)", (string) $url, $response->getStatusCode())); } else {