diff options
Diffstat (limited to 'src/Wallabag/CoreBundle/Helper/ContentProxy.php')
-rw-r--r-- | src/Wallabag/CoreBundle/Helper/ContentProxy.php | 60 |
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 | |||
3 | namespace Wallabag\CoreBundle\Helper; | ||
4 | |||
5 | use Graby\Graby; | ||
6 | use 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 | */ | ||
12 | class 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 | } | ||