wallabag/src/Wallabag/CoreBundle/Event/Subscriber/DownloadImagesSubscriber.php

122 lines
3.2 KiB
PHP
Raw Normal View History

2016-10-30 09:48:29 +00:00
<?php
namespace Wallabag\CoreBundle\Event\Subscriber;
2017-07-01 07:52:38 +00:00
use Doctrine\ORM\EntityManager;
2016-10-30 09:48:29 +00:00
use Psr\Log\LoggerInterface;
2017-07-01 07:52:38 +00:00
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
2016-10-30 09:48:29 +00:00
use Wallabag\CoreBundle\Entity\Entry;
use Wallabag\CoreBundle\Event\EntryDeletedEvent;
2017-07-01 07:52:38 +00:00
use Wallabag\CoreBundle\Event\EntrySavedEvent;
use Wallabag\CoreBundle\Helper\DownloadImages;
2016-10-30 09:48:29 +00:00
class DownloadImagesSubscriber implements EventSubscriberInterface
2016-10-30 09:48:29 +00:00
{
private $em;
2016-10-30 09:48:29 +00:00
private $downloadImages;
private $enabled;
2016-10-30 09:48:29 +00:00
private $logger;
public function __construct(EntityManager $em, DownloadImages $downloadImages, $enabled, LoggerInterface $logger)
2016-10-30 09:48:29 +00:00
{
$this->em = $em;
2016-10-30 09:48:29 +00:00
$this->downloadImages = $downloadImages;
$this->enabled = $enabled;
2016-10-30 09:48:29 +00:00
$this->logger = $logger;
}
public static function getSubscribedEvents()
2016-10-30 09:48:29 +00:00
{
return [
EntrySavedEvent::NAME => 'onEntrySaved',
EntryDeletedEvent::NAME => 'onEntryDeleted',
];
2016-10-30 09:48:29 +00:00
}
/**
* Download images and updated the data into the entry.
2016-10-30 09:48:29 +00:00
*
* @param EntrySavedEvent $event
2016-10-30 09:48:29 +00:00
*/
public function onEntrySaved(EntrySavedEvent $event)
2016-10-30 09:48:29 +00:00
{
if (!$this->enabled) {
$this->logger->debug('DownloadImagesSubscriber: disabled.');
2016-10-30 09:48:29 +00:00
return;
}
$entry = $event->getEntry();
$html = $this->downloadImages($entry);
if (false !== $html) {
$this->logger->debug('DownloadImagesSubscriber: updated html.');
2016-10-30 09:48:29 +00:00
$entry->setContent($html);
2016-10-30 09:48:29 +00:00
}
// update preview picture
$previewPicture = $this->downloadPreviewImage($entry);
if (false !== $previewPicture) {
$this->logger->debug('DownloadImagesSubscriber: update preview picture.');
2016-10-30 09:48:29 +00:00
$entry->setPreviewPicture($previewPicture);
2016-10-30 09:48:29 +00:00
}
$this->em->persist($entry);
$this->em->flush();
2016-10-30 09:48:29 +00:00
}
/**
* Remove images related to the entry.
2016-10-30 09:48:29 +00:00
*
* @param EntryDeletedEvent $event
2016-10-30 09:48:29 +00:00
*/
public function onEntryDeleted(EntryDeletedEvent $event)
2016-10-30 09:48:29 +00:00
{
if (!$this->enabled) {
$this->logger->debug('DownloadImagesSubscriber: disabled.');
2016-10-30 09:48:29 +00:00
return;
}
$this->downloadImages->removeImages($event->getEntry()->getId());
2016-10-30 09:48:29 +00:00
}
2016-10-30 10:27:09 +00:00
/**
* Download all images from the html.
*
2016-10-31 12:29:33 +00:00
* @todo If we want to add async download, it should be done in that method
*
* @param Entry $entry
2016-10-30 10:27:09 +00:00
*
* @return string|false False in case of async
*/
private function downloadImages(Entry $entry)
2016-10-30 09:48:29 +00:00
{
return $this->downloadImages->processHtml(
$entry->getId(),
2016-10-30 09:48:29 +00:00
$entry->getContent(),
$entry->getUrl()
);
}
2016-10-30 10:27:09 +00:00
/**
* Download the preview picture.
*
2016-10-31 12:29:33 +00:00
* @todo If we want to add async download, it should be done in that method
*
* @param Entry $entry
2016-10-30 10:27:09 +00:00
*
* @return string|false False in case of async
*/
private function downloadPreviewImage(Entry $entry)
2016-10-30 09:48:29 +00:00
{
return $this->downloadImages->processSingleImage(
$entry->getId(),
2016-10-30 09:48:29 +00:00
$entry->getPreviewPicture(),
$entry->getUrl()
);
}
}