2016-09-11 18:23:17 +00:00
|
|
|
<?php
|
|
|
|
|
2023-12-31 09:03:14 +00:00
|
|
|
namespace Wallabag\CoreBundle\Consumer;
|
2016-09-11 18:23:17 +00:00
|
|
|
|
2022-04-30 18:12:47 +00:00
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
2016-09-11 18:23:17 +00:00
|
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
use Psr\Log\NullLogger;
|
2016-11-02 06:10:23 +00:00
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
2017-07-01 07:52:38 +00:00
|
|
|
use Wallabag\CoreBundle\Entity\Entry;
|
|
|
|
use Wallabag\CoreBundle\Entity\Tag;
|
2016-11-02 06:10:23 +00:00
|
|
|
use Wallabag\CoreBundle\Event\EntrySavedEvent;
|
2023-12-31 17:21:09 +00:00
|
|
|
use Wallabag\CoreBundle\Import\AbstractImport;
|
2023-12-30 22:38:22 +00:00
|
|
|
use Wallabag\CoreBundle\Repository\UserRepository;
|
2016-09-11 18:23:17 +00:00
|
|
|
|
|
|
|
abstract class AbstractConsumer
|
|
|
|
{
|
|
|
|
protected $em;
|
|
|
|
protected $userRepository;
|
|
|
|
protected $import;
|
2016-11-03 17:01:25 +00:00
|
|
|
protected $eventDispatcher;
|
2016-09-11 18:23:17 +00:00
|
|
|
protected $logger;
|
|
|
|
|
2022-04-30 18:12:47 +00:00
|
|
|
public function __construct(EntityManagerInterface $em, UserRepository $userRepository, AbstractImport $import, EventDispatcherInterface $eventDispatcher, LoggerInterface $logger = null)
|
2016-09-11 18:23:17 +00:00
|
|
|
{
|
|
|
|
$this->em = $em;
|
|
|
|
$this->userRepository = $userRepository;
|
|
|
|
$this->import = $import;
|
2016-11-02 06:10:23 +00:00
|
|
|
$this->eventDispatcher = $eventDispatcher;
|
2016-09-11 18:23:17 +00:00
|
|
|
$this->logger = $logger ?: new NullLogger();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle a message and save it.
|
|
|
|
*
|
|
|
|
* @param string $body Message from the queue (in json)
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
protected function handleMessage($body)
|
|
|
|
{
|
|
|
|
$storedEntry = json_decode($body, true);
|
|
|
|
|
|
|
|
$user = $this->userRepository->find($storedEntry['userId']);
|
|
|
|
|
|
|
|
// no user? Drop message
|
|
|
|
if (null === $user) {
|
|
|
|
$this->logger->warning('Unable to retrieve user', ['entry' => $storedEntry]);
|
|
|
|
|
2017-02-20 08:16:02 +00:00
|
|
|
// return true to skip message
|
|
|
|
return true;
|
2016-09-11 18:23:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->import->setUser($user);
|
|
|
|
|
2018-12-18 12:14:42 +00:00
|
|
|
if (false === $this->import->validateEntry($storedEntry)) {
|
|
|
|
$this->logger->warning('Entry is invalid', ['entry' => $storedEntry]);
|
|
|
|
|
|
|
|
// return true to skip message
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-09-11 18:23:17 +00:00
|
|
|
$entry = $this->import->parseEntry($storedEntry);
|
|
|
|
|
|
|
|
if (null === $entry) {
|
2016-10-20 19:17:03 +00:00
|
|
|
$this->logger->warning('Entry already exists', ['entry' => $storedEntry]);
|
2016-09-11 18:23:17 +00:00
|
|
|
|
2016-10-20 19:17:03 +00:00
|
|
|
// return true to skip message
|
|
|
|
return true;
|
2016-09-11 18:23:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$this->em->flush();
|
|
|
|
|
2016-11-02 06:10:23 +00:00
|
|
|
// entry saved, dispatch event about it!
|
2022-12-15 20:47:31 +00:00
|
|
|
$this->eventDispatcher->dispatch(new EntrySavedEvent($entry), EntrySavedEvent::NAME);
|
2016-11-02 06:10:23 +00:00
|
|
|
|
2016-09-11 18:23:17 +00:00
|
|
|
// clear only affected entities
|
|
|
|
$this->em->clear(Entry::class);
|
|
|
|
$this->em->clear(Tag::class);
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
$this->logger->warning('Unable to save entry', ['entry' => $storedEntry, 'exception' => $e]);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-07-01 07:52:38 +00:00
|
|
|
$this->logger->info('Content with url imported! (' . $entry->getUrl() . ')');
|
2016-09-11 18:23:17 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|