aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/CoreBundle/Event
diff options
context:
space:
mode:
Diffstat (limited to 'src/Wallabag/CoreBundle/Event')
-rw-r--r--src/Wallabag/CoreBundle/Event/EntryDeletedEvent.php26
-rw-r--r--src/Wallabag/CoreBundle/Event/EntrySavedEvent.php26
-rw-r--r--src/Wallabag/CoreBundle/Event/Subscriber/DownloadImagesSubscriber.php117
3 files changed, 96 insertions, 73 deletions
diff --git a/src/Wallabag/CoreBundle/Event/EntryDeletedEvent.php b/src/Wallabag/CoreBundle/Event/EntryDeletedEvent.php
new file mode 100644
index 00000000..e9061d04
--- /dev/null
+++ b/src/Wallabag/CoreBundle/Event/EntryDeletedEvent.php
@@ -0,0 +1,26 @@
1<?php
2
3namespace Wallabag\CoreBundle\Event;
4
5use Symfony\Component\EventDispatcher\Event;
6use Wallabag\CoreBundle\Entity\Entry;
7
8/**
9 * This event is fired as soon as an entry is deleted.
10 */
11class EntryDeletedEvent extends Event
12{
13 const NAME = 'entry.deleted';
14
15 protected $entry;
16
17 public function __construct(Entry $entry)
18 {
19 $this->entry = $entry;
20 }
21
22 public function getEntry()
23 {
24 return $this->entry;
25 }
26}
diff --git a/src/Wallabag/CoreBundle/Event/EntrySavedEvent.php b/src/Wallabag/CoreBundle/Event/EntrySavedEvent.php
new file mode 100644
index 00000000..5fdb5221
--- /dev/null
+++ b/src/Wallabag/CoreBundle/Event/EntrySavedEvent.php
@@ -0,0 +1,26 @@
1<?php
2
3namespace Wallabag\CoreBundle\Event;
4
5use Symfony\Component\EventDispatcher\Event;
6use Wallabag\CoreBundle\Entity\Entry;
7
8/**
9 * This event is fired as soon as an entry was saved.
10 */
11class EntrySavedEvent extends Event
12{
13 const NAME = 'entry.saved';
14
15 protected $entry;
16
17 public function __construct(Entry $entry)
18 {
19 $this->entry = $entry;
20 }
21
22 public function getEntry()
23 {
24 return $this->entry;
25 }
26}
diff --git a/src/Wallabag/CoreBundle/Event/Subscriber/DownloadImagesSubscriber.php b/src/Wallabag/CoreBundle/Event/Subscriber/DownloadImagesSubscriber.php
index 6fddcea9..4ebe837b 100644
--- a/src/Wallabag/CoreBundle/Event/Subscriber/DownloadImagesSubscriber.php
+++ b/src/Wallabag/CoreBundle/Event/Subscriber/DownloadImagesSubscriber.php
@@ -2,110 +2,85 @@
2 2
3namespace Wallabag\CoreBundle\Event\Subscriber; 3namespace Wallabag\CoreBundle\Event\Subscriber;
4 4
5use Doctrine\Common\EventSubscriber; 5use Symfony\Component\EventDispatcher\EventSubscriberInterface;
6use Doctrine\ORM\Event\LifecycleEventArgs;
7use Psr\Log\LoggerInterface; 6use Psr\Log\LoggerInterface;
8use Wallabag\CoreBundle\Helper\DownloadImages; 7use Wallabag\CoreBundle\Helper\DownloadImages;
9use Wallabag\CoreBundle\Entity\Entry; 8use Wallabag\CoreBundle\Entity\Entry;
9use Wallabag\CoreBundle\Event\EntrySavedEvent;
10use Wallabag\CoreBundle\Event\EntryDeletedEvent;
10use Doctrine\ORM\EntityManager; 11use Doctrine\ORM\EntityManager;
11use Craue\ConfigBundle\Util\Config;
12 12
13class DownloadImagesSubscriber implements EventSubscriber 13class DownloadImagesSubscriber implements EventSubscriberInterface
14{ 14{
15 private $configClass; 15 private $em;
16 private $downloadImages; 16 private $downloadImages;
17 private $enabled;
17 private $logger; 18 private $logger;
18 19
19 /** 20 public function __construct(EntityManager $em, DownloadImages $downloadImages, $enabled, LoggerInterface $logger)
20 * We inject the class instead of the service otherwise it generates a circular reference with the EntityManager.
21 * So we build the service ourself when we got the EntityManager (in downloadImages).
22 */
23 public function __construct(DownloadImages $downloadImages, $configClass, LoggerInterface $logger)
24 { 21 {
22 $this->em = $em;
25 $this->downloadImages = $downloadImages; 23 $this->downloadImages = $downloadImages;
26 $this->configClass = $configClass; 24 $this->enabled = $enabled;
27 $this->logger = $logger; 25 $this->logger = $logger;
28 } 26 }
29 27
30 public function getSubscribedEvents() 28 public static function getSubscribedEvents()
31 { 29 {
32 return array( 30 return [
33 'prePersist', 31 EntrySavedEvent::NAME => 'onEntrySaved',
34 'preUpdate', 32 EntryDeletedEvent::NAME => 'onEntryDeleted',
35 ); 33 ];
36 } 34 }
37 35
38 /** 36 /**
39 * In case of an entry has been updated. 37 * Download images and updated the data into the entry.
40 * We won't update the content field if it wasn't updated.
41 * 38 *
42 * @param LifecycleEventArgs $args 39 * @param EntrySavedEvent $event
43 */ 40 */
44 public function preUpdate(LifecycleEventArgs $args) 41 public function onEntrySaved(EntrySavedEvent $event)
45 { 42 {
46 $entity = $args->getEntity(); 43 if (!$this->enabled) {
44 $this->logger->debug('DownloadImagesSubscriber: disabled.');
47 45
48 if (!$entity instanceof Entry) {
49 return; 46 return;
50 } 47 }
51 48
52 $config = new $this->configClass(); 49 $entry = $event->getEntry();
53 $config->setEntityManager($args->getEntityManager());
54
55 if (!$config->get('download_images_enabled')) {
56 return;
57 }
58 50
59 // field content has been updated 51 $html = $this->downloadImages($entry);
60 if ($args->hasChangedField('content')) { 52 if (false !== $html) {
61 $html = $this->downloadImages($config, $entity); 53 $this->logger->debug('DownloadImagesSubscriber: updated html.');
62 54
63 if (false !== $html) { 55 $entry->setContent($html);
64 $args->setNewValue('content', $html);
65 }
66 } 56 }
67 57
68 // field preview picture has been updated 58 // update preview picture
69 if ($args->hasChangedField('previewPicture')) { 59 $previewPicture = $this->downloadPreviewImage($entry);
70 $previewPicture = $this->downloadPreviewImage($config, $entity); 60 if (false !== $previewPicture) {
61 $this->logger->debug('DownloadImagesSubscriber: update preview picture.');
71 62
72 if (false !== $previewPicture) { 63 $entry->setPreviewPicture($previewPicture);
73 $entity->setPreviewPicture($previewPicture);
74 }
75 } 64 }
65
66 $this->em->persist($entry);
67 $this->em->flush();
76 } 68 }
77 69
78 /** 70 /**
79 * When a new entry is saved. 71 * Remove images related to the entry.
80 * 72 *
81 * @param LifecycleEventArgs $args 73 * @param EntryDeletedEvent $event
82 */ 74 */
83 public function prePersist(LifecycleEventArgs $args) 75 public function onEntryDeleted(EntryDeletedEvent $event)
84 { 76 {
85 $entity = $args->getEntity(); 77 if (!$this->enabled) {
86 78 $this->logger->debug('DownloadImagesSubscriber: disabled.');
87 if (!$entity instanceof Entry) {
88 return;
89 }
90
91 $config = new $this->configClass();
92 $config->setEntityManager($args->getEntityManager());
93 79
94 if (!$config->get('download_images_enabled')) {
95 return; 80 return;
96 } 81 }
97 82
98 // update all images inside the html 83 $this->downloadImages->removeImages($event->getEntry()->getId());
99 $html = $this->downloadImages($config, $entity);
100 if (false !== $html) {
101 $entity->setContent($html);
102 }
103
104 // update preview picture
105 $previewPicture = $this->downloadPreviewImage($config, $entity);
106 if (false !== $previewPicture) {
107 $entity->setPreviewPicture($previewPicture);
108 }
109 } 84 }
110 85
111 /** 86 /**
@@ -113,16 +88,14 @@ class DownloadImagesSubscriber implements EventSubscriber
113 * 88 *
114 * @todo If we want to add async download, it should be done in that method 89 * @todo If we want to add async download, it should be done in that method
115 * 90 *
116 * @param Config $config 91 * @param Entry $entry
117 * @param Entry $entry
118 * 92 *
119 * @return string|false False in case of async 93 * @return string|false False in case of async
120 */ 94 */
121 public function downloadImages(Config $config, Entry $entry) 95 private function downloadImages(Entry $entry)
122 { 96 {
123 $this->downloadImages->setWallabagUrl($config->get('wallabag_url'));
124
125 return $this->downloadImages->processHtml( 97 return $this->downloadImages->processHtml(
98 $entry->getId(),
126 $entry->getContent(), 99 $entry->getContent(),
127 $entry->getUrl() 100 $entry->getUrl()
128 ); 101 );
@@ -133,16 +106,14 @@ class DownloadImagesSubscriber implements EventSubscriber
133 * 106 *
134 * @todo If we want to add async download, it should be done in that method 107 * @todo If we want to add async download, it should be done in that method
135 * 108 *
136 * @param Config $config 109 * @param Entry $entry
137 * @param Entry $entry
138 * 110 *
139 * @return string|false False in case of async 111 * @return string|false False in case of async
140 */ 112 */
141 public function downloadPreviewImage(Config $config, Entry $entry) 113 private function downloadPreviewImage(Entry $entry)
142 { 114 {
143 $this->downloadImages->setWallabagUrl($config->get('wallabag_url'));
144
145 return $this->downloadImages->processSingleImage( 115 return $this->downloadImages->processSingleImage(
116 $entry->getId(),
146 $entry->getPreviewPicture(), 117 $entry->getPreviewPicture(),
147 $entry->getUrl() 118 $entry->getUrl()
148 ); 119 );