]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/CoreBundle/Helper/ContentProxy.php
Move readingTime & domainName in ContentProxy
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Helper / ContentProxy.php
1 <?php
2
3 namespace Wallabag\CoreBundle\Helper;
4
5 use Graby\Graby;
6 use Wallabag\CoreBundle\Entity\Entry;
7 use Wallabag\CoreBundle\Tools\Utils;
8
9 /**
10 * This kind of proxy class take care of getting the content from an url
11 * and update the entry with what it found.
12 */
13 class 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.
24 * In case we couldn't find content, we'll try to use Open Graph data.
25 *
26 * @param Entry $entry Entry to update
27 * @param string $url Url to grab content for
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);
53 $entry->setLanguage($content['language']);
54 $entry->setMimetype($content['content_type']);
55 $entry->setReadingTime(Utils::getReadingTime($html));
56 $entry->setDomainName(parse_url($entry->getUrl(), PHP_URL_HOST));
57
58 if (isset($content['open_graph']['og_image'])) {
59 $entry->setPreviewPicture($content['open_graph']['og_image']);
60 }
61
62 return $entry;
63 }
64 }