]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/CoreBundle/Helper/ContentProxy.php
Use tecnickcom/tcpdf
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Helper / ContentProxy.php
CommitLineData
558d9aab
JB
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
f1e29e69 10 * and update the entry with what it found.
558d9aab
JB
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.
f1e29e69 23 * In case we couldn't find content, we'll try to use Open Graph data.
558d9aab 24 *
f1e29e69
JB
25 * @param Entry $entry Entry to update
26 * @param string $url Url to grab content for
558d9aab
JB
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);
98f0929f 52 $entry->setLanguage($content['language']);
558d9aab
JB
53 $entry->setMimetype($content['content_type']);
54
55 if (isset($content['open_graph']['og_image'])) {
56 $entry->setPreviewPicture($content['open_graph']['og_image']);
57 }
58
59 return $entry;
60 }
61}