3 namespace Wallabag\CoreBundle\Event\Subscriber
;
5 use Doctrine\ORM\EntityManager
;
6 use Psr\Log\LoggerInterface
;
7 use Symfony\Component\EventDispatcher\EventSubscriberInterface
;
8 use Wallabag\CoreBundle\Entity\Entry
;
9 use Wallabag\CoreBundle\Event\EntryDeletedEvent
;
10 use Wallabag\CoreBundle\Event\EntrySavedEvent
;
11 use Wallabag\CoreBundle\Helper\DownloadImages
;
13 class DownloadImagesSubscriber
implements EventSubscriberInterface
16 private $downloadImages;
20 public function __construct(EntityManager
$em, DownloadImages
$downloadImages, $enabled, LoggerInterface
$logger)
23 $this->downloadImages
= $downloadImages;
24 $this->enabled
= $enabled;
25 $this->logger
= $logger;
28 public static function getSubscribedEvents()
31 EntrySavedEvent
::NAME
=> 'onEntrySaved',
32 EntryDeletedEvent
::NAME
=> 'onEntryDeleted',
37 * Download images and updated the data into the entry.
39 * @param EntrySavedEvent $event
41 public function onEntrySaved(EntrySavedEvent
$event)
43 if (!$this->enabled
) {
44 $this->logger
->debug('DownloadImagesSubscriber: disabled.');
49 $entry = $event->getEntry();
51 $html = $this->downloadImages($entry);
52 if (false !== $html) {
53 $this->logger
->debug('DownloadImagesSubscriber: updated html.');
55 $entry->setContent($html);
58 // update preview picture
59 $previewPicture = $this->downloadPreviewImage($entry);
60 if (false !== $previewPicture) {
61 $this->logger
->debug('DownloadImagesSubscriber: update preview picture.');
63 $entry->setPreviewPicture($previewPicture);
66 $this->em
->persist($entry);
71 * Remove images related to the entry.
73 * @param EntryDeletedEvent $event
75 public function onEntryDeleted(EntryDeletedEvent
$event)
77 if (!$this->enabled
) {
78 $this->logger
->debug('DownloadImagesSubscriber: disabled.');
83 $this->downloadImages
->removeImages($event->getEntry()->getId());
87 * Download all images from the html.
89 * @todo If we want to add async download, it should be done in that method
93 * @return string|false False in case of async
95 private function downloadImages(Entry
$entry)
97 return $this->downloadImages
->processHtml(
105 * Download the preview picture.
107 * @todo If we want to add async download, it should be done in that method
109 * @param Entry $entry
111 * @return string|false False in case of async
113 private function downloadPreviewImage(Entry
$entry)
115 return $this->downloadImages
->processSingleImage(
117 $entry->getPreviewPicture(),