]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/CoreBundle/Event/Subscriber/DownloadImagesSubscriber.php
Merge pull request #4152 from ldidry/add-env-var-dev.sh
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Event / Subscriber / DownloadImagesSubscriber.php
CommitLineData
7f559418
JB
1<?php
2
3namespace Wallabag\CoreBundle\Event\Subscriber;
4
f808b016 5use Doctrine\ORM\EntityManager;
7f559418 6use Psr\Log\LoggerInterface;
f808b016 7use Symfony\Component\EventDispatcher\EventSubscriberInterface;
7f559418 8use Wallabag\CoreBundle\Entity\Entry;
e0597476 9use Wallabag\CoreBundle\Event\EntryDeletedEvent;
f808b016
JB
10use Wallabag\CoreBundle\Event\EntrySavedEvent;
11use Wallabag\CoreBundle\Helper\DownloadImages;
7f559418 12
e0597476 13class DownloadImagesSubscriber implements EventSubscriberInterface
7f559418 14{
e0597476 15 private $em;
7f559418 16 private $downloadImages;
e0597476 17 private $enabled;
7f559418
JB
18 private $logger;
19
e0597476 20 public function __construct(EntityManager $em, DownloadImages $downloadImages, $enabled, LoggerInterface $logger)
7f559418 21 {
e0597476 22 $this->em = $em;
7f559418 23 $this->downloadImages = $downloadImages;
e0597476 24 $this->enabled = $enabled;
7f559418
JB
25 $this->logger = $logger;
26 }
27
e0597476 28 public static function getSubscribedEvents()
7f559418 29 {
e0597476
JB
30 return [
31 EntrySavedEvent::NAME => 'onEntrySaved',
32 EntryDeletedEvent::NAME => 'onEntryDeleted',
33 ];
7f559418
JB
34 }
35
36 /**
e0597476 37 * Download images and updated the data into the entry.
7f559418 38 */
e0597476 39 public function onEntrySaved(EntrySavedEvent $event)
7f559418 40 {
e0597476
JB
41 if (!$this->enabled) {
42 $this->logger->debug('DownloadImagesSubscriber: disabled.');
7f559418 43
7f559418
JB
44 return;
45 }
46
e0597476 47 $entry = $event->getEntry();
d1495dd0 48
e0597476
JB
49 $html = $this->downloadImages($entry);
50 if (false !== $html) {
51 $this->logger->debug('DownloadImagesSubscriber: updated html.');
7f559418 52
e0597476 53 $entry->setContent($html);
7f559418
JB
54 }
55
e0597476
JB
56 // update preview picture
57 $previewPicture = $this->downloadPreviewImage($entry);
58 if (false !== $previewPicture) {
59 $this->logger->debug('DownloadImagesSubscriber: update preview picture.');
7f559418 60
e0597476 61 $entry->setPreviewPicture($previewPicture);
7f559418 62 }
e0597476
JB
63
64 $this->em->persist($entry);
65 $this->em->flush();
7f559418
JB
66 }
67
68 /**
e0597476 69 * Remove images related to the entry.
7f559418 70 */
e0597476 71 public function onEntryDeleted(EntryDeletedEvent $event)
7f559418 72 {
e0597476
JB
73 if (!$this->enabled) {
74 $this->logger->debug('DownloadImagesSubscriber: disabled.');
7f559418 75
d1495dd0
JB
76 return;
77 }
78
e0597476 79 $this->downloadImages->removeImages($event->getEntry()->getId());
7f559418
JB
80 }
81
48656e0e
JB
82 /**
83 * Download all images from the html.
84 *
aedd6ca0
JB
85 * @todo If we want to add async download, it should be done in that method
86 *
48656e0e
JB
87 * @return string|false False in case of async
88 */
e0597476 89 private function downloadImages(Entry $entry)
7f559418 90 {
7f559418 91 return $this->downloadImages->processHtml(
e0597476 92 $entry->getId(),
7f559418
JB
93 $entry->getContent(),
94 $entry->getUrl()
95 );
96 }
97
48656e0e
JB
98 /**
99 * Download the preview picture.
100 *
aedd6ca0
JB
101 * @todo If we want to add async download, it should be done in that method
102 *
48656e0e
JB
103 * @return string|false False in case of async
104 */
e0597476 105 private function downloadPreviewImage(Entry $entry)
7f559418 106 {
7f559418 107 return $this->downloadImages->processSingleImage(
e0597476 108 $entry->getId(),
7f559418
JB
109 $entry->getPreviewPicture(),
110 $entry->getUrl()
111 );
112 }
113}