diff options
Diffstat (limited to 'src/Wallabag/ImportBundle/Import/AbstractImport.php')
-rw-r--r-- | src/Wallabag/ImportBundle/Import/AbstractImport.php | 24 |
1 files changed, 23 insertions, 1 deletions
diff --git a/src/Wallabag/ImportBundle/Import/AbstractImport.php b/src/Wallabag/ImportBundle/Import/AbstractImport.php index 764b390a..1d4a6e27 100644 --- a/src/Wallabag/ImportBundle/Import/AbstractImport.php +++ b/src/Wallabag/ImportBundle/Import/AbstractImport.php | |||
@@ -10,12 +10,15 @@ use Wallabag\CoreBundle\Entity\Entry; | |||
10 | use Wallabag\CoreBundle\Entity\Tag; | 10 | use Wallabag\CoreBundle\Entity\Tag; |
11 | use Wallabag\UserBundle\Entity\User; | 11 | use Wallabag\UserBundle\Entity\User; |
12 | use OldSound\RabbitMqBundle\RabbitMq\ProducerInterface; | 12 | use OldSound\RabbitMqBundle\RabbitMq\ProducerInterface; |
13 | use Symfony\Component\EventDispatcher\EventDispatcherInterface; | ||
14 | use Wallabag\CoreBundle\Event\EntrySavedEvent; | ||
13 | 15 | ||
14 | abstract class AbstractImport implements ImportInterface | 16 | abstract class AbstractImport implements ImportInterface |
15 | { | 17 | { |
16 | protected $em; | 18 | protected $em; |
17 | protected $logger; | 19 | protected $logger; |
18 | protected $contentProxy; | 20 | protected $contentProxy; |
21 | protected $eventDispatcher; | ||
19 | protected $producer; | 22 | protected $producer; |
20 | protected $user; | 23 | protected $user; |
21 | protected $markAsRead; | 24 | protected $markAsRead; |
@@ -23,11 +26,12 @@ abstract class AbstractImport implements ImportInterface | |||
23 | protected $importedEntries = 0; | 26 | protected $importedEntries = 0; |
24 | protected $queuedEntries = 0; | 27 | protected $queuedEntries = 0; |
25 | 28 | ||
26 | public function __construct(EntityManager $em, ContentProxy $contentProxy) | 29 | public function __construct(EntityManager $em, ContentProxy $contentProxy, EventDispatcherInterface $eventDispatcher) |
27 | { | 30 | { |
28 | $this->em = $em; | 31 | $this->em = $em; |
29 | $this->logger = new NullLogger(); | 32 | $this->logger = new NullLogger(); |
30 | $this->contentProxy = $contentProxy; | 33 | $this->contentProxy = $contentProxy; |
34 | $this->eventDispatcher = $eventDispatcher; | ||
31 | } | 35 | } |
32 | 36 | ||
33 | public function setLogger(LoggerInterface $logger) | 37 | public function setLogger(LoggerInterface $logger) |
@@ -104,6 +108,7 @@ abstract class AbstractImport implements ImportInterface | |||
104 | protected function parseEntries($entries) | 108 | protected function parseEntries($entries) |
105 | { | 109 | { |
106 | $i = 1; | 110 | $i = 1; |
111 | $entryToBeFlushed = []; | ||
107 | 112 | ||
108 | foreach ($entries as $importedEntry) { | 113 | foreach ($entries as $importedEntry) { |
109 | if ($this->markAsRead) { | 114 | if ($this->markAsRead) { |
@@ -116,10 +121,21 @@ abstract class AbstractImport implements ImportInterface | |||
116 | continue; | 121 | continue; |
117 | } | 122 | } |
118 | 123 | ||
124 | // store each entry to be flushed so we can trigger the entry.saved event for each of them | ||
125 | // entry.saved needs the entry to be persisted in db because it needs it id to generate | ||
126 | // images (at least) | ||
127 | $entryToBeFlushed[] = $entry; | ||
128 | |||
119 | // flush every 20 entries | 129 | // flush every 20 entries |
120 | if (($i % 20) === 0) { | 130 | if (($i % 20) === 0) { |
121 | $this->em->flush(); | 131 | $this->em->flush(); |
122 | 132 | ||
133 | foreach ($entryToBeFlushed as $entry) { | ||
134 | $this->eventDispatcher->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry)); | ||
135 | } | ||
136 | |||
137 | $entryToBeFlushed = []; | ||
138 | |||
123 | // clear only affected entities | 139 | // clear only affected entities |
124 | $this->em->clear(Entry::class); | 140 | $this->em->clear(Entry::class); |
125 | $this->em->clear(Tag::class); | 141 | $this->em->clear(Tag::class); |
@@ -128,6 +144,12 @@ abstract class AbstractImport implements ImportInterface | |||
128 | } | 144 | } |
129 | 145 | ||
130 | $this->em->flush(); | 146 | $this->em->flush(); |
147 | |||
148 | if (!empty($entryToBeFlushed)) { | ||
149 | foreach ($entryToBeFlushed as $entry) { | ||
150 | $this->eventDispatcher->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry)); | ||
151 | } | ||
152 | } | ||
131 | } | 153 | } |
132 | 154 | ||
133 | /** | 155 | /** |