]> git.immae.eu Git - github/wallabag/wallabag.git/blobdiff - src/Wallabag/ImportBundle/Consumer/AbstractConsumer.php
Import: we now skip messages when user is null
[github/wallabag/wallabag.git] / src / Wallabag / ImportBundle / Consumer / AbstractConsumer.php
index 2b85ad7672324b31f90a68d27da3022469841436..992ce1adba3a7a3ba526eb1a6eec876e4c2a9c1b 100644 (file)
@@ -9,19 +9,23 @@ use Wallabag\CoreBundle\Entity\Entry;
 use Wallabag\CoreBundle\Entity\Tag;
 use Psr\Log\LoggerInterface;
 use Psr\Log\NullLogger;
+use Symfony\Component\EventDispatcher\EventDispatcherInterface;
+use Wallabag\CoreBundle\Event\EntrySavedEvent;
 
 abstract class AbstractConsumer
 {
     protected $em;
     protected $userRepository;
     protected $import;
+    protected $eventDispatcher;
     protected $logger;
 
-    public function __construct(EntityManager $em, UserRepository $userRepository, AbstractImport $import, LoggerInterface $logger = null)
+    public function __construct(EntityManager $em, UserRepository $userRepository, AbstractImport $import, EventDispatcherInterface $eventDispatcher, LoggerInterface $logger = null)
     {
         $this->em = $em;
         $this->userRepository = $userRepository;
         $this->import = $import;
+        $this->eventDispatcher = $eventDispatcher;
         $this->logger = $logger ?: new NullLogger();
     }
 
@@ -42,7 +46,8 @@ abstract class AbstractConsumer
         if (null === $user) {
             $this->logger->warning('Unable to retrieve user', ['entry' => $storedEntry]);
 
-            return false;
+            // return true to skip message
+            return true;
         }
 
         $this->import->setUser($user);
@@ -50,14 +55,18 @@ abstract class AbstractConsumer
         $entry = $this->import->parseEntry($storedEntry);
 
         if (null === $entry) {
-            $this->logger->warning('Unable to parse entry', ['entry' => $storedEntry]);
+            $this->logger->warning('Entry already exists', ['entry' => $storedEntry]);
 
-            return false;
+            // return true to skip message
+            return true;
         }
 
         try {
             $this->em->flush();
 
+            // entry saved, dispatch event about it!
+            $this->eventDispatcher->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry));
+
             // clear only affected entities
             $this->em->clear(Entry::class);
             $this->em->clear(Tag::class);