downloadImages = $downloadImages; $this->configClass = $configClass; $this->logger = $logger; } public function getSubscribedEvents() { return array( 'prePersist', 'preUpdate', ); } /** * In case of an entry has been updated. * We won't update the content field if it wasn't updated. * * @param LifecycleEventArgs $args */ public function preUpdate(LifecycleEventArgs $args) { $entity = $args->getEntity(); if (!$entity instanceof Entry) { return; } $config = new $this->configClass(); $config->setEntityManager($args->getEntityManager()); // field content has been updated if ($args->hasChangedField('content')) { $html = $this->downloadImages($config, $entity); if (false !== $html) { $args->setNewValue('content', $html); } } // field preview picture has been updated if ($args->hasChangedField('previewPicture')) { $previewPicture = $this->downloadPreviewImage($config, $entity); if (false !== $previewPicture) { $entity->setPreviewPicture($previewPicture); } } } /** * When a new entry is saved. * * @param LifecycleEventArgs $args */ public function prePersist(LifecycleEventArgs $args) { $entity = $args->getEntity(); 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); } } /** * Download all images from the html. * * @param Config $config * @param Entry $entry * * @return string|false False in case of async */ public function downloadImages(Config $config, Entry $entry) { $this->downloadImages->setWallabagUrl($config->get('wallabag_url')); // if ($config->get('download_images_with_rabbitmq')) { // } else if ($config->get('download_images_with_redis')) { // } return $this->downloadImages->processHtml( $entry->getContent(), $entry->getUrl() ); } /** * Download the preview picture. * * @param Config $config * @param Entry $entry * * @return string|false False in case of async */ public function downloadPreviewImage(Config $config, Entry $entry) { $this->downloadImages->setWallabagUrl($config->get('wallabag_url')); // if ($config->get('download_images_with_rabbitmq')) { // } else if ($config->get('download_images_with_redis')) { // } return $this->downloadImages->processSingleImage( $entry->getPreviewPicture(), $entry->getUrl() ); } }