]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | ||
3 | namespace Wallabag\CoreBundle\Event\Subscriber; | |
4 | ||
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; | |
12 | ||
13 | class DownloadImagesSubscriber implements EventSubscriberInterface | |
14 | { | |
15 | private $em; | |
16 | private $downloadImages; | |
17 | private $enabled; | |
18 | private $logger; | |
19 | ||
20 | public function __construct(EntityManager $em, DownloadImages $downloadImages, $enabled, LoggerInterface $logger) | |
21 | { | |
22 | $this->em = $em; | |
23 | $this->downloadImages = $downloadImages; | |
24 | $this->enabled = $enabled; | |
25 | $this->logger = $logger; | |
26 | } | |
27 | ||
28 | public static function getSubscribedEvents() | |
29 | { | |
30 | return [ | |
31 | EntrySavedEvent::NAME => 'onEntrySaved', | |
32 | EntryDeletedEvent::NAME => 'onEntryDeleted', | |
33 | ]; | |
34 | } | |
35 | ||
36 | /** | |
37 | * Download images and updated the data into the entry. | |
38 | */ | |
39 | public function onEntrySaved(EntrySavedEvent $event) | |
40 | { | |
41 | if (!$this->enabled) { | |
42 | $this->logger->debug('DownloadImagesSubscriber: disabled.'); | |
43 | ||
44 | return; | |
45 | } | |
46 | ||
47 | $entry = $event->getEntry(); | |
48 | ||
49 | $html = $this->downloadImages($entry); | |
50 | if (false !== $html) { | |
51 | $this->logger->debug('DownloadImagesSubscriber: updated html.'); | |
52 | ||
53 | $entry->setContent($html); | |
54 | } | |
55 | ||
56 | // update preview picture | |
57 | $previewPicture = $this->downloadPreviewImage($entry); | |
58 | if (false !== $previewPicture) { | |
59 | $this->logger->debug('DownloadImagesSubscriber: update preview picture.'); | |
60 | ||
61 | $entry->setPreviewPicture($previewPicture); | |
62 | } | |
63 | ||
64 | $this->em->persist($entry); | |
65 | $this->em->flush(); | |
66 | } | |
67 | ||
68 | /** | |
69 | * Remove images related to the entry. | |
70 | */ | |
71 | public function onEntryDeleted(EntryDeletedEvent $event) | |
72 | { | |
73 | if (!$this->enabled) { | |
74 | $this->logger->debug('DownloadImagesSubscriber: disabled.'); | |
75 | ||
76 | return; | |
77 | } | |
78 | ||
79 | $this->downloadImages->removeImages($event->getEntry()->getId()); | |
80 | } | |
81 | ||
82 | /** | |
83 | * Download all images from the html. | |
84 | * | |
85 | * @todo If we want to add async download, it should be done in that method | |
86 | * | |
87 | * @return string|false False in case of async | |
88 | */ | |
89 | private function downloadImages(Entry $entry) | |
90 | { | |
91 | return $this->downloadImages->processHtml( | |
92 | $entry->getId(), | |
93 | $entry->getContent(), | |
94 | $entry->getUrl() | |
95 | ); | |
96 | } | |
97 | ||
98 | /** | |
99 | * Download the preview picture. | |
100 | * | |
101 | * @todo If we want to add async download, it should be done in that method | |
102 | * | |
103 | * @return string|false False in case of async | |
104 | */ | |
105 | private function downloadPreviewImage(Entry $entry) | |
106 | { | |
107 | return $this->downloadImages->processSingleImage( | |
108 | $entry->getId(), | |
109 | $entry->getPreviewPicture(), | |
110 | $entry->getUrl() | |
111 | ); | |
112 | } | |
113 | } |