]>
Commit | Line | Data |
---|---|---|
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 | // field content has been updated | |
56 | if ($args->hasChangedField('content')) { | |
57 | $html = $this->downloadImages($config, $entity); | |
58 | ||
59 | if (false !== $html) { | |
60 | $args->setNewValue('content', $html); | |
61 | } | |
62 | } | |
63 | ||
64 | // field preview picture has been updated | |
65 | if ($args->hasChangedField('previewPicture')) { | |
66 | $previewPicture = $this->downloadPreviewImage($config, $entity); | |
67 | ||
68 | if (false !== $previewPicture) { | |
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); | |
92 | if (false !== $html) { | |
93 | $entity->setContent($html); | |
94 | } | |
95 | ||
96 | // update preview picture | |
97 | $previewPicture = $this->downloadPreviewImage($config, $entity); | |
98 | if (false !== $previewPicture) { | |
99 | $entity->setPreviewPicture($previewPicture); | |
100 | } | |
101 | } | |
102 | ||
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 | */ | |
111 | public function downloadImages(Config $config, Entry $entry) | |
112 | { | |
113 | $this->downloadImages->setWallabagUrl($config->get('wallabag_url')); | |
114 | ||
115 | // if ($config->get('download_images_with_rabbitmq')) { | |
116 | ||
117 | // } else if ($config->get('download_images_with_redis')) { | |
118 | ||
119 | // } | |
120 | ||
121 | return $this->downloadImages->processHtml( | |
122 | $entry->getContent(), | |
123 | $entry->getUrl() | |
124 | ); | |
125 | } | |
126 | ||
127 | /** | |
128 | * Download the preview picture. | |
129 | * | |
130 | * @param Config $config | |
131 | * @param Entry $entry | |
132 | * | |
133 | * @return string|false False in case of async | |
134 | */ | |
135 | public function downloadPreviewImage(Config $config, Entry $entry) | |
136 | { | |
137 | $this->downloadImages->setWallabagUrl($config->get('wallabag_url')); | |
138 | ||
139 | // if ($config->get('download_images_with_rabbitmq')) { | |
140 | ||
141 | // } else if ($config->get('download_images_with_redis')) { | |
142 | ||
143 | // } | |
144 | ||
145 | return $this->downloadImages->processSingleImage( | |
146 | $entry->getPreviewPicture(), | |
147 | $entry->getUrl() | |
148 | ); | |
149 | } | |
150 | } |