]> git.immae.eu Git - github/wallabag/wallabag.git/blobdiff - src/Wallabag/CoreBundle/Event/Subscriber/DownloadImagesSubscriber.php
Add a real configuration for CS-Fixer
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Event / Subscriber / DownloadImagesSubscriber.php
index 09f8e9119767d5f9870ff76091e42a4a9b6c7d2b..1dd0a1a42ed56d7c510daa211c4c7cee4bc1c238 100644 (file)
 
 namespace Wallabag\CoreBundle\Event\Subscriber;
 
-use Doctrine\Common\EventSubscriber;
-use Doctrine\ORM\Event\LifecycleEventArgs;
+use Doctrine\ORM\EntityManager;
 use Psr\Log\LoggerInterface;
-use Wallabag\CoreBundle\Helper\DownloadImages;
+use Symfony\Component\EventDispatcher\EventSubscriberInterface;
 use Wallabag\CoreBundle\Entity\Entry;
-use Doctrine\ORM\EntityManager;
-use Craue\ConfigBundle\Util\Config;
+use Wallabag\CoreBundle\Event\EntryDeletedEvent;
+use Wallabag\CoreBundle\Event\EntrySavedEvent;
+use Wallabag\CoreBundle\Helper\DownloadImages;
 
-class DownloadImagesSubscriber implements EventSubscriber
+class DownloadImagesSubscriber implements EventSubscriberInterface
 {
-    private $configClass;
+    private $em;
     private $downloadImages;
+    private $enabled;
     private $logger;
 
-    /**
-     * We inject the class instead of the service otherwise it generates a circular reference with the EntityManager.
-     * So we build the service ourself when we got the EntityManager (in downloadImages).
-     */
-    public function __construct(DownloadImages $downloadImages, $configClass, LoggerInterface $logger)
+    public function __construct(EntityManager $em, DownloadImages $downloadImages, $enabled, LoggerInterface $logger)
     {
+        $this->em = $em;
         $this->downloadImages = $downloadImages;
-        $this->configClass = $configClass;
+        $this->enabled = $enabled;
         $this->logger = $logger;
     }
 
-    public function getSubscribedEvents()
+    public static function getSubscribedEvents()
     {
-        return array(
-            'prePersist',
-            'preUpdate',
-        );
+        return [
+            EntrySavedEvent::NAME => 'onEntrySaved',
+            EntryDeletedEvent::NAME => 'onEntryDeleted',
+        ];
     }
 
     /**
-     * In case of an entry has been updated.
-     * We won't update the content field if it wasn't updated.
+     * Download images and updated the data into the entry.
      *
-     * @param LifecycleEventArgs $args
+     * @param EntrySavedEvent $event
      */
-    public function preUpdate(LifecycleEventArgs $args)
+    public function onEntrySaved(EntrySavedEvent $event)
     {
-        $entity = $args->getEntity();
+        if (!$this->enabled) {
+            $this->logger->debug('DownloadImagesSubscriber: disabled.');
 
-        if (!$entity instanceof Entry) {
             return;
         }
 
-        $config = new $this->configClass();
-        $config->setEntityManager($args->getEntityManager());
+        $entry = $event->getEntry();
 
-        // field content has been updated
-        if ($args->hasChangedField('content')) {
-            $html = $this->downloadImages($config, $entity);
+        $html = $this->downloadImages($entry);
+        if (false !== $html) {
+            $this->logger->debug('DownloadImagesSubscriber: updated html.');
 
-            if (false !== $html) {
-                $args->setNewValue('content', $html);
-            }
+            $entry->setContent($html);
         }
 
-        // field preview picture has been updated
-        if ($args->hasChangedField('previewPicture')) {
-            $previewPicture = $this->downloadPreviewImage($config, $entity);
+        // update preview picture
+        $previewPicture = $this->downloadPreviewImage($entry);
+        if (false !== $previewPicture) {
+            $this->logger->debug('DownloadImagesSubscriber: update preview picture.');
 
-            if (false !== $previewPicture) {
-                $entity->setPreviewPicture($previewPicture);
-            }
+            $entry->setPreviewPicture($previewPicture);
         }
+
+        $this->em->persist($entry);
+        $this->em->flush();
     }
 
     /**
-     * When a new entry is saved.
+     * Remove images related to the entry.
      *
-     * @param LifecycleEventArgs $args
+     * @param EntryDeletedEvent $event
      */
-    public function prePersist(LifecycleEventArgs $args)
+    public function onEntryDeleted(EntryDeletedEvent $event)
     {
-        $entity = $args->getEntity();
+        if (!$this->enabled) {
+            $this->logger->debug('DownloadImagesSubscriber: disabled.');
 
-        if (!$entity instanceof Entry) {
             return;
         }
 
-        $config = new $this->configClass();
-        $config->setEntityManager($args->getEntityManager());
-
-        // update all images inside the html
-        $html = $this->downloadImages($config, $entity);
-        if (false !== $html) {
-            $entity->setContent($html);
-        }
-
-        // update preview picture
-        $previewPicture = $this->downloadPreviewImage($config, $entity);
-        if (false !== $previewPicture) {
-            $entity->setPreviewPicture($previewPicture);
-        }
+        $this->downloadImages->removeImages($event->getEntry()->getId());
     }
 
     /**
      * Download all images from the html.
      *
-     * @param Config $config
-     * @param Entry  $entry
+     * @todo If we want to add async download, it should be done in that method
+     *
+     * @param Entry $entry
      *
      * @return string|false False in case of async
      */
-    public function downloadImages(Config $config, Entry $entry)
+    private function downloadImages(Entry $entry)
     {
-        // if ($config->get('download_images_with_rabbitmq')) {
-
-        // } else if ($config->get('download_images_with_redis')) {
-
-        // }
-
         return $this->downloadImages->processHtml(
+            $entry->getId(),
             $entry->getContent(),
             $entry->getUrl()
         );
@@ -125,20 +104,16 @@ class DownloadImagesSubscriber implements EventSubscriber
     /**
      * Download the preview picture.
      *
-     * @param Config $config
-     * @param Entry  $entry
+     * @todo If we want to add async download, it should be done in that method
+     *
+     * @param Entry $entry
      *
      * @return string|false False in case of async
      */
-    public function downloadPreviewImage(Config $config, Entry $entry)
+    private function downloadPreviewImage(Entry $entry)
     {
-        // if ($config->get('download_images_with_rabbitmq')) {
-
-        // } else if ($config->get('download_images_with_redis')) {
-
-        // }
-
         return $this->downloadImages->processSingleImage(
+            $entry->getId(),
             $entry->getPreviewPicture(),
             $entry->getUrl()
         );