3 namespace Wallabag\ImportBundle\Consumer
;
5 use Doctrine\ORM\EntityManager
;
6 use Wallabag\ImportBundle\Import\AbstractImport
;
7 use Wallabag\UserBundle\Repository\UserRepository
;
8 use Wallabag\CoreBundle\Entity\Entry
;
9 use Wallabag\CoreBundle\Entity\Tag
;
10 use Psr\Log\LoggerInterface
;
11 use Psr\Log\NullLogger
;
13 abstract class AbstractConsumer
16 protected $userRepository;
20 public function __construct(EntityManager
$em, UserRepository
$userRepository, AbstractImport
$import, LoggerInterface
$logger = null)
23 $this->userRepository
= $userRepository;
24 $this->import
= $import;
25 $this->logger
= $logger ?: new NullLogger();
29 * Handle a message and save it.
31 * @param string $body Message from the queue (in json)
35 protected function handleMessage($body)
37 $storedEntry = json_decode($body, true);
39 $user = $this->userRepository
->find($storedEntry['userId']);
41 // no user? Drop message
43 $this->logger
->warning('Unable to retrieve user', ['entry' => $storedEntry]);
48 $this->import
->setUser($user);
50 $entry = $this->import
->parseEntry($storedEntry);
52 if (null === $entry) {
53 $this->logger
->warning('Unable to parse entry', ['entry' => $storedEntry]);
61 // clear only affected entities
62 $this->em
->clear(Entry
::class);
63 $this->em
->clear(Tag
::class);
64 } catch (\Exception
$e) {
65 $this->logger
->warning('Unable to save entry', ['entry' => $storedEntry, 'exception' => $e]);
70 $this->logger
->info('Content with url imported! ('.$entry->getUrl().')');