]> git.immae.eu Git - github/wallabag/wallabag.git/blame - 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
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 * @param EntrySavedEvent $event
7f559418 40 */
e0597476 41 public function onEntrySaved(EntrySavedEvent $event)
7f559418 42 {
e0597476
JB
43 if (!$this->enabled) {
44 $this->logger->debug('DownloadImagesSubscriber: disabled.');
7f559418 45
7f559418
JB
46 return;
47 }
48
e0597476 49 $entry = $event->getEntry();
d1495dd0 50
e0597476
JB
51 $html = $this->downloadImages($entry);
52 if (false !== $html) {
53 $this->logger->debug('DownloadImagesSubscriber: updated html.');
7f559418 54
e0597476 55 $entry->setContent($html);
7f559418
JB
56 }
57
e0597476
JB
58 // update preview picture
59 $previewPicture = $this->downloadPreviewImage($entry);
60 if (false !== $previewPicture) {
61 $this->logger->debug('DownloadImagesSubscriber: update preview picture.');
7f559418 62
e0597476 63 $entry->setPreviewPicture($previewPicture);
7f559418 64 }
e0597476
JB
65
66 $this->em->persist($entry);
67 $this->em->flush();
7f559418
JB
68 }
69
70 /**
e0597476 71 * Remove images related to the entry.
7f559418 72 *
e0597476 73 * @param EntryDeletedEvent $event
7f559418 74 */
e0597476 75 public function onEntryDeleted(EntryDeletedEvent $event)
7f559418 76 {
e0597476
JB
77 if (!$this->enabled) {
78 $this->logger->debug('DownloadImagesSubscriber: disabled.');
7f559418 79
d1495dd0
JB
80 return;
81 }
82
e0597476 83 $this->downloadImages->removeImages($event->getEntry()->getId());
7f559418
JB
84 }
85
48656e0e
JB
86 /**
87 * Download all images from the html.
88 *
aedd6ca0
JB
89 * @todo If we want to add async download, it should be done in that method
90 *
e0597476 91 * @param Entry $entry
48656e0e
JB
92 *
93 * @return string|false False in case of async
94 */
e0597476 95 private function downloadImages(Entry $entry)
7f559418 96 {
7f559418 97 return $this->downloadImages->processHtml(
e0597476 98 $entry->getId(),
7f559418
JB
99 $entry->getContent(),
100 $entry->getUrl()
101 );
102 }
103
48656e0e
JB
104 /**
105 * Download the preview picture.
106 *
aedd6ca0
JB
107 * @todo If we want to add async download, it should be done in that method
108 *
e0597476 109 * @param Entry $entry
48656e0e
JB
110 *
111 * @return string|false False in case of async
112 */
e0597476 113 private function downloadPreviewImage(Entry $entry)
7f559418 114 {
7f559418 115 return $this->downloadImages->processSingleImage(
e0597476 116 $entry->getId(),
7f559418
JB
117 $entry->getPreviewPicture(),
118 $entry->getUrl()
119 );
120 }
121}