From 558d9aabab7e01c2e2b506aa362c70a568b953aa Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Thu, 10 Sep 2015 21:57:25 +0200 Subject: Move fetching content in a separate class --- src/Wallabag/CoreBundle/Helper/ContentProxy.php | 60 +++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 src/Wallabag/CoreBundle/Helper/ContentProxy.php (limited to 'src/Wallabag/CoreBundle/Helper/ContentProxy.php') diff --git a/src/Wallabag/CoreBundle/Helper/ContentProxy.php b/src/Wallabag/CoreBundle/Helper/ContentProxy.php new file mode 100644 index 00000000..2dd70e51 --- /dev/null +++ b/src/Wallabag/CoreBundle/Helper/ContentProxy.php @@ -0,0 +1,60 @@ +graby = $graby; + } + + /** + * Fetch content using graby and hydrate given entry with results information. + * In case we couldn't find content, we'll try to use Open Graph data + * + * @param Entry $entry Entry to update + * @param string $url Url to grab content for + * + * @return Entry + */ + public function updateEntry(Entry $entry, $url) + { + $content = $this->graby->fetchContent($url); + + $title = $content['title']; + if (!$title && isset($content['open_graph']['og_title'])) { + $title = $content['open_graph']['og_title']; + } + + $html = $content['html']; + if (false === $html) { + $html = '

Unable to retrieve readable content.

'; + + if (isset($content['open_graph']['og_description'])) { + $html .= '

But we found a short description:

'; + $html .= $content['open_graph']['og_description']; + } + } + + $entry->setUrl($content['url'] ?: $url); + $entry->setTitle($title); + $entry->setContent($html); + $entry->setMimetype($content['content_type']); + + if (isset($content['open_graph']['og_image'])) { + $entry->setPreviewPicture($content['open_graph']['og_image']); + } + + return $entry; + } +} -- cgit v1.2.3 From f1e29e69cb0ba5a0f05190c62e7a4afd43d03436 Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Thu, 10 Sep 2015 22:00:53 +0200 Subject: CS --- src/Wallabag/CoreBundle/Helper/ContentProxy.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'src/Wallabag/CoreBundle/Helper/ContentProxy.php') diff --git a/src/Wallabag/CoreBundle/Helper/ContentProxy.php b/src/Wallabag/CoreBundle/Helper/ContentProxy.php index 2dd70e51..4565d8e7 100644 --- a/src/Wallabag/CoreBundle/Helper/ContentProxy.php +++ b/src/Wallabag/CoreBundle/Helper/ContentProxy.php @@ -7,7 +7,7 @@ use Wallabag\CoreBundle\Entity\Entry; /** * This kind of proxy class take care of getting the content from an url - * and update the entry with what it found + * and update the entry with what it found. */ class ContentProxy { @@ -20,10 +20,10 @@ class ContentProxy /** * Fetch content using graby and hydrate given entry with results information. - * In case we couldn't find content, we'll try to use Open Graph data + * In case we couldn't find content, we'll try to use Open Graph data. * - * @param Entry $entry Entry to update - * @param string $url Url to grab content for + * @param Entry $entry Entry to update + * @param string $url Url to grab content for * * @return Entry */ -- cgit v1.2.3