diff options
Diffstat (limited to 'src/Wallabag/ImportBundle/Consumer/AMPQEntryConsumer.php')
-rw-r--r-- | src/Wallabag/ImportBundle/Consumer/AMPQEntryConsumer.php | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/src/Wallabag/ImportBundle/Consumer/AMPQEntryConsumer.php b/src/Wallabag/ImportBundle/Consumer/AMPQEntryConsumer.php new file mode 100644 index 00000000..39bb5375 --- /dev/null +++ b/src/Wallabag/ImportBundle/Consumer/AMPQEntryConsumer.php | |||
@@ -0,0 +1,70 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Wallabag\ImportBundle\Consumer; | ||
4 | |||
5 | use Doctrine\ORM\EntityManager; | ||
6 | use OldSound\RabbitMqBundle\RabbitMq\ConsumerInterface; | ||
7 | 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 | |||
15 | class AMPQEntryConsumer implements ConsumerInterface | ||
16 | { | ||
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 | /** | ||
31 | * {@inheritdoc} | ||
32 | */ | ||
33 | public function execute(AMQPMessage $msg) | ||
34 | { | ||
35 | $storedEntry = json_decode($msg->body, true); | ||
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 | } | ||
70 | } | ||