]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/CoreBundle/Event/Subscriber/DownloadImagesSubscriber.php
Fixing tests
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Event / Subscriber / DownloadImagesSubscriber.php
CommitLineData
7f559418
JB
1<?php
2
3namespace Wallabag\CoreBundle\Event\Subscriber;
4
5use Doctrine\Common\EventSubscriber;
6use Doctrine\ORM\Event\LifecycleEventArgs;
7use Psr\Log\LoggerInterface;
8use Wallabag\CoreBundle\Helper\DownloadImages;
9use Wallabag\CoreBundle\Entity\Entry;
10use Doctrine\ORM\EntityManager;
11use Craue\ConfigBundle\Util\Config;
12
13class 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
48656e0e
JB
52 $config = new $this->configClass();
53 $config->setEntityManager($args->getEntityManager());
7f559418
JB
54
55 // field content has been updated
56 if ($args->hasChangedField('content')) {
48656e0e 57 $html = $this->downloadImages($config, $entity);
7f559418 58
48656e0e 59 if (false !== $html) {
7f559418
JB
60 $args->setNewValue('content', $html);
61 }
62 }
63
64 // field preview picture has been updated
65 if ($args->hasChangedField('previewPicture')) {
48656e0e 66 $previewPicture = $this->downloadPreviewImage($config, $entity);
7f559418 67
48656e0e 68 if (false !== $previewPicture) {
7f559418
JB
69 $entity->setPreviewPicture($previewPicture);
70 }
71 }
72 }
73
74 /**
75 * When a new entry is saved.
76 *
77 * @param LifecycleEventArgs $args
78 */
79 public function prePersist(LifecycleEventArgs $args)
80 {
81 $entity = $args->getEntity();
82
83 if (!$entity instanceof Entry) {
84 return;
85 }
86
87 $config = new $this->configClass();
88 $config->setEntityManager($args->getEntityManager());
89
90 // update all images inside the html
91 $html = $this->downloadImages($config, $entity);
48656e0e 92 if (false !== $html) {
7f559418
JB
93 $entity->setContent($html);
94 }
95
96 // update preview picture
97 $previewPicture = $this->downloadPreviewImage($config, $entity);
48656e0e 98 if (false !== $previewPicture) {
7f559418
JB
99 $entity->setPreviewPicture($previewPicture);
100 }
101 }
102
48656e0e
JB
103 /**
104 * Download all images from the html.
105 *
106 * @param Config $config
107 * @param Entry $entry
108 *
109 * @return string|false False in case of async
110 */
7f559418
JB
111 public function downloadImages(Config $config, Entry $entry)
112 {
113 // if ($config->get('download_images_with_rabbitmq')) {
114
115 // } else if ($config->get('download_images_with_redis')) {
116
117 // }
118
119 return $this->downloadImages->processHtml(
120 $entry->getContent(),
121 $entry->getUrl()
122 );
123 }
124
48656e0e
JB
125 /**
126 * Download the preview picture.
127 *
128 * @param Config $config
129 * @param Entry $entry
130 *
131 * @return string|false False in case of async
132 */
7f559418
JB
133 public function downloadPreviewImage(Config $config, Entry $entry)
134 {
135 // if ($config->get('download_images_with_rabbitmq')) {
136
137 // } else if ($config->get('download_images_with_redis')) {
138
139 // }
140
141 return $this->downloadImages->processSingleImage(
142 $entry->getPreviewPicture(),
143 $entry->getUrl()
144 );
145 }
146}