]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/CoreBundle/Helper/ContentProxy.php
Merge pull request #1422 from wallabag/v2-ebook
[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;
da3d4998 7use Wallabag\CoreBundle\Tools\Utils;
558d9aab
JB
8
9/**
10 * This kind of proxy class take care of getting the content from an url
f1e29e69 11 * and update the entry with what it found.
558d9aab
JB
12 */
13class ContentProxy
14{
15 protected $graby;
16
17 public function __construct(Graby $graby)
18 {
19 $this->graby = $graby;
20 }
21
22 /**
23 * Fetch content using graby and hydrate given entry with results information.
f1e29e69 24 * In case we couldn't find content, we'll try to use Open Graph data.
558d9aab 25 *
f1e29e69
JB
26 * @param Entry $entry Entry to update
27 * @param string $url Url to grab content for
558d9aab
JB
28 *
29 * @return Entry
30 */
31 public function updateEntry(Entry $entry, $url)
32 {
33 $content = $this->graby->fetchContent($url);
34
35 $title = $content['title'];
36 if (!$title && isset($content['open_graph']['og_title'])) {
37 $title = $content['open_graph']['og_title'];
38 }
39
40 $html = $content['html'];
41 if (false === $html) {
42 $html = '<p>Unable to retrieve readable content.</p>';
43
44 if (isset($content['open_graph']['og_description'])) {
45 $html .= '<p><i>But we found a short description: </i></p>';
46 $html .= $content['open_graph']['og_description'];
47 }
48 }
49
50 $entry->setUrl($content['url'] ?: $url);
51 $entry->setTitle($title);
52 $entry->setContent($html);
98f0929f 53 $entry->setLanguage($content['language']);
558d9aab 54 $entry->setMimetype($content['content_type']);
da3d4998
JB
55 $entry->setReadingTime(Utils::getReadingTime($html));
56 $entry->setDomainName(parse_url($entry->getUrl(), PHP_URL_HOST));
558d9aab
JB
57
58 if (isset($content['open_graph']['og_image'])) {
59 $entry->setPreviewPicture($content['open_graph']['og_image']);
60 }
61
62 return $entry;
63 }
64}