X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=src%2FWallabag%2FImportBundle%2FConsumer%2FAbstractConsumer.php;h=e4bfbdf033807fca182b99343d6455d6361e451a;hb=9f8f188d928b47503d39348c5990379a572b570a;hp=2b85ad7672324b31f90a68d27da3022469841436;hpb=da18a4682f124b02278860d23ac1d59dee995277;p=github%2Fwallabag%2Fwallabag.git diff --git a/src/Wallabag/ImportBundle/Consumer/AbstractConsumer.php b/src/Wallabag/ImportBundle/Consumer/AbstractConsumer.php index 2b85ad76..e4bfbdf0 100644 --- a/src/Wallabag/ImportBundle/Consumer/AbstractConsumer.php +++ b/src/Wallabag/ImportBundle/Consumer/AbstractConsumer.php @@ -3,25 +3,29 @@ namespace Wallabag\ImportBundle\Consumer; use Doctrine\ORM\EntityManager; -use Wallabag\ImportBundle\Import\AbstractImport; -use Wallabag\UserBundle\Repository\UserRepository; -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\Entity\Entry; +use Wallabag\CoreBundle\Entity\Tag; +use Wallabag\CoreBundle\Event\EntrySavedEvent; +use Wallabag\ImportBundle\Import\AbstractImport; +use Wallabag\UserBundle\Repository\UserRepository; 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,22 +46,34 @@ 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); + if (false === $this->import->validateEntry($storedEntry)) { + $this->logger->warning('Entry is invalid', ['entry' => $storedEntry]); + + // return true to skip message + return true; + } + $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); @@ -67,7 +83,7 @@ abstract class AbstractConsumer return false; } - $this->logger->info('Content with url imported! ('.$entry->getUrl().')'); + $this->logger->info('Content with url imported! (' . $entry->getUrl() . ')'); return true; }