]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/CoreBundle/Event/Subscriber/DownloadImagesSubscriber.php
Use doctrine event to download images
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Event / Subscriber / DownloadImagesSubscriber.php
1 <?php
2
3 namespace Wallabag\CoreBundle\Event\Subscriber;
4
5 use Doctrine\Common\EventSubscriber;
6 use Doctrine\ORM\Event\LifecycleEventArgs;
7 use Psr\Log\LoggerInterface;
8 use Wallabag\CoreBundle\Helper\DownloadImages;
9 use Wallabag\CoreBundle\Entity\Entry;
10 use Doctrine\ORM\EntityManager;
11 use Craue\ConfigBundle\Util\Config;
12
13 class DownloadImagesSubscriber implements EventSubscriber
14 {
15 private $configClass;
16 private $downloadImages;
17 private $logger;
18
19 /**
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 {
25 $this->downloadImages = $downloadImages;
26 $this->configClass = $configClass;
27 $this->logger = $logger;
28 }
29
30 public function getSubscribedEvents()
31 {
32 return array(
33 'prePersist',
34 'preUpdate',
35 );
36 }
37
38 /**
39 * In case of an entry has been updated.
40 * We won't update the content field if it wasn't updated.
41 *
42 * @param LifecycleEventArgs $args
43 */
44 public function preUpdate(LifecycleEventArgs $args)
45 {
46 $entity = $args->getEntity();
47
48 if (!$entity instanceof Entry) {
49 return;
50 }
51
52 $em = $args->getEntityManager();
53
54 // field content has been updated
55 if ($args->hasChangedField('content')) {
56 $html = $this->downloadImages($em, $entity);
57
58 if (null !== $html) {
59 $args->setNewValue('content', $html);
60 }
61 }
62
63 // field preview picture has been updated
64 if ($args->hasChangedField('previewPicture')) {
65 $previewPicture = $this->downloadPreviewImage($em, $entity);
66
67 if (null !== $previewPicture) {
68 $entity->setPreviewPicture($previewPicture);
69 }
70 }
71 }
72
73 /**
74 * When a new entry is saved.
75 *
76 * @param LifecycleEventArgs $args
77 */
78 public function prePersist(LifecycleEventArgs $args)
79 {
80 $entity = $args->getEntity();
81
82 if (!$entity instanceof Entry) {
83 return;
84 }
85
86 $config = new $this->configClass();
87 $config->setEntityManager($args->getEntityManager());
88
89 // update all images inside the html
90 $html = $this->downloadImages($config, $entity);
91 if (null !== $html) {
92 $entity->setContent($html);
93 }
94
95 // update preview picture
96 $previewPicture = $this->downloadPreviewImage($config, $entity);
97 if (null !== $previewPicture) {
98 $entity->setPreviewPicture($previewPicture);
99 }
100 }
101
102 public function downloadImages(Config $config, Entry $entry)
103 {
104 // if ($config->get('download_images_with_rabbitmq')) {
105
106 // } else if ($config->get('download_images_with_redis')) {
107
108 // }
109
110 return $this->downloadImages->processHtml(
111 $entry->getContent(),
112 $entry->getUrl()
113 );
114 }
115
116 public function downloadPreviewImage(Config $config, Entry $entry)
117 {
118 // if ($config->get('download_images_with_rabbitmq')) {
119
120 // } else if ($config->get('download_images_with_redis')) {
121
122 // }
123
124 return $this->downloadImages->processSingleImage(
125 $entry->getPreviewPicture(),
126 $entry->getUrl()
127 );
128 }
129 }