]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/CoreBundle/Event/Subscriber/DownloadImagesSubscriber.php
Add translations & migration
[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 $config = new $this->configClass();
53 $config->setEntityManager($args->getEntityManager());
54
55 if (!$config->get('download_images_enabled')) {
56 return;
57 }
58
59 // field content has been updated
60 if ($args->hasChangedField('content')) {
61 $html = $this->downloadImages($config, $entity);
62
63 if (false !== $html) {
64 $args->setNewValue('content', $html);
65 }
66 }
67
68 // field preview picture has been updated
69 if ($args->hasChangedField('previewPicture')) {
70 $previewPicture = $this->downloadPreviewImage($config, $entity);
71
72 if (false !== $previewPicture) {
73 $entity->setPreviewPicture($previewPicture);
74 }
75 }
76 }
77
78 /**
79 * When a new entry is saved.
80 *
81 * @param LifecycleEventArgs $args
82 */
83 public function prePersist(LifecycleEventArgs $args)
84 {
85 $entity = $args->getEntity();
86
87 if (!$entity instanceof Entry) {
88 return;
89 }
90
91 $config = new $this->configClass();
92 $config->setEntityManager($args->getEntityManager());
93
94 if (!$config->get('download_images_enabled')) {
95 return;
96 }
97
98 // update all images inside the html
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 }
110
111 /**
112 * Download all images from the html.
113 *
114 * @todo If we want to add async download, it should be done in that method
115 *
116 * @param Config $config
117 * @param Entry $entry
118 *
119 * @return string|false False in case of async
120 */
121 public function downloadImages(Config $config, Entry $entry)
122 {
123 $this->downloadImages->setWallabagUrl($config->get('wallabag_url'));
124
125 return $this->downloadImages->processHtml(
126 $entry->getContent(),
127 $entry->getUrl()
128 );
129 }
130
131 /**
132 * Download the preview picture.
133 *
134 * @todo If we want to add async download, it should be done in that method
135 *
136 * @param Config $config
137 * @param Entry $entry
138 *
139 * @return string|false False in case of async
140 */
141 public function downloadPreviewImage(Config $config, Entry $entry)
142 {
143 $this->downloadImages->setWallabagUrl($config->get('wallabag_url'));
144
145 return $this->downloadImages->processSingleImage(
146 $entry->getPreviewPicture(),
147 $entry->getUrl()
148 );
149 }
150 }