diff options
Diffstat (limited to 'src/Wallabag/ImportBundle/Consumer/AMPQEntryConsumer.php')
-rw-r--r-- | src/Wallabag/ImportBundle/Consumer/AMPQEntryConsumer.php | 57 |
1 files changed, 2 insertions, 55 deletions
diff --git a/src/Wallabag/ImportBundle/Consumer/AMPQEntryConsumer.php b/src/Wallabag/ImportBundle/Consumer/AMPQEntryConsumer.php index 39bb5375..d95a011d 100644 --- a/src/Wallabag/ImportBundle/Consumer/AMPQEntryConsumer.php +++ b/src/Wallabag/ImportBundle/Consumer/AMPQEntryConsumer.php | |||
@@ -2,69 +2,16 @@ | |||
2 | 2 | ||
3 | namespace Wallabag\ImportBundle\Consumer; | 3 | namespace Wallabag\ImportBundle\Consumer; |
4 | 4 | ||
5 | use Doctrine\ORM\EntityManager; | ||
6 | use OldSound\RabbitMqBundle\RabbitMq\ConsumerInterface; | 5 | use OldSound\RabbitMqBundle\RabbitMq\ConsumerInterface; |
7 | use PhpAmqpLib\Message\AMQPMessage; | 6 | use PhpAmqpLib\Message\AMQPMessage; |
8 | use Wallabag\ImportBundle\Import\AbstractImport; | ||
9 | use Wallabag\UserBundle\Repository\UserRepository; | ||
10 | use Wallabag\CoreBundle\Entity\Entry; | ||
11 | use Wallabag\CoreBundle\Entity\Tag; | ||
12 | use Psr\Log\LoggerInterface; | ||
13 | use Psr\Log\NullLogger; | ||
14 | 7 | ||
15 | class AMPQEntryConsumer implements ConsumerInterface | 8 | class AMPQEntryConsumer extends AbstractConsumer implements ConsumerInterface |
16 | { | 9 | { |
17 | private $em; | ||
18 | private $userRepository; | ||
19 | private $import; | ||
20 | private $logger; | ||
21 | |||
22 | public function __construct(EntityManager $em, UserRepository $userRepository, AbstractImport $import, LoggerInterface $logger = null) | ||
23 | { | ||
24 | $this->em = $em; | ||
25 | $this->userRepository = $userRepository; | ||
26 | $this->import = $import; | ||
27 | $this->logger = $logger ?: new NullLogger(); | ||
28 | } | ||
29 | |||
30 | /** | 10 | /** |
31 | * {@inheritdoc} | 11 | * {@inheritdoc} |
32 | */ | 12 | */ |
33 | public function execute(AMQPMessage $msg) | 13 | public function execute(AMQPMessage $msg) |
34 | { | 14 | { |
35 | $storedEntry = json_decode($msg->body, true); | 15 | return $this->handleMessage($msg->body); |
36 | |||
37 | $user = $this->userRepository->find($storedEntry['userId']); | ||
38 | |||
39 | // no user? Drop message | ||
40 | if (null === $user) { | ||
41 | $this->logger->warning('Unable to retrieve user', ['entry' => $storedEntry]); | ||
42 | |||
43 | return; | ||
44 | } | ||
45 | |||
46 | $this->import->setUser($user); | ||
47 | |||
48 | $entry = $this->import->parseEntry($storedEntry); | ||
49 | |||
50 | if (null === $entry) { | ||
51 | $this->logger->warning('Unable to parse entry', ['entry' => $storedEntry]); | ||
52 | |||
53 | return; | ||
54 | } | ||
55 | |||
56 | try { | ||
57 | $this->em->flush(); | ||
58 | |||
59 | // clear only affected entities | ||
60 | $this->em->clear(Entry::class); | ||
61 | $this->em->clear(Tag::class); | ||
62 | } catch (\Exception $e) { | ||
63 | $this->logger->warning('Unable to save entry', ['entry' => $storedEntry, 'exception' => $e]); | ||
64 | |||
65 | return; | ||
66 | } | ||
67 | |||
68 | $this->logger->info('Content with url ('.$entry->getUrl().') imported !'); | ||
69 | } | 16 | } |
70 | } | 17 | } |