aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/CoreBundle/Helper/ContentProxy.php
diff options
context:
space:
mode:
authorJeremy Benoist <jeremy.benoist@gmail.com>2015-09-10 21:57:25 +0200
committerJeremy Benoist <jeremy.benoist@gmail.com>2015-09-10 21:57:25 +0200
commit558d9aabab7e01c2e2b506aa362c70a568b953aa (patch)
treee3fd1fee2ab5f8104edff10dc1d16b86146f9dba /src/Wallabag/CoreBundle/Helper/ContentProxy.php
parent75c3478a0c308c96bccd871f3bc1343356aafb6b (diff)
downloadwallabag-558d9aabab7e01c2e2b506aa362c70a568b953aa.tar.gz
wallabag-558d9aabab7e01c2e2b506aa362c70a568b953aa.tar.zst
wallabag-558d9aabab7e01c2e2b506aa362c70a568b953aa.zip
Move fetching content in a separate class
Diffstat (limited to 'src/Wallabag/CoreBundle/Helper/ContentProxy.php')
-rw-r--r--src/Wallabag/CoreBundle/Helper/ContentProxy.php60
1 files changed, 60 insertions, 0 deletions
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 @@
1<?php
2
3namespace Wallabag\CoreBundle\Helper;
4
5use Graby\Graby;
6use Wallabag\CoreBundle\Entity\Entry;
7
8/**
9 * This kind of proxy class take care of getting the content from an url
10 * and update the entry with what it found
11 */
12class ContentProxy
13{
14 protected $graby;
15
16 public function __construct(Graby $graby)
17 {
18 $this->graby = $graby;
19 }
20
21 /**
22 * Fetch content using graby and hydrate given entry with results information.
23 * In case we couldn't find content, we'll try to use Open Graph data
24 *
25 * @param Entry $entry Entry to update
26 * @param string $url Url to grab content for
27 *
28 * @return Entry
29 */
30 public function updateEntry(Entry $entry, $url)
31 {
32 $content = $this->graby->fetchContent($url);
33
34 $title = $content['title'];
35 if (!$title && isset($content['open_graph']['og_title'])) {
36 $title = $content['open_graph']['og_title'];
37 }
38
39 $html = $content['html'];
40 if (false === $html) {
41 $html = '<p>Unable to retrieve readable content.</p>';
42
43 if (isset($content['open_graph']['og_description'])) {
44 $html .= '<p><i>But we found a short description: </i></p>';
45 $html .= $content['open_graph']['og_description'];
46 }
47 }
48
49 $entry->setUrl($content['url'] ?: $url);
50 $entry->setTitle($title);
51 $entry->setContent($html);
52 $entry->setMimetype($content['content_type']);
53
54 if (isset($content['open_graph']['og_image'])) {
55 $entry->setPreviewPicture($content['open_graph']['og_image']);
56 }
57
58 return $entry;
59 }
60}