]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/ImportBundle/Consumer/AbstractConsumer.php
Validate imported entry to avoid error on import
[github/wallabag/wallabag.git] / src / Wallabag / ImportBundle / Consumer / AbstractConsumer.php
CommitLineData
7d862f83
JB
1<?php
2
3namespace Wallabag\ImportBundle\Consumer;
4
5use Doctrine\ORM\EntityManager;
7d862f83
JB
6use Psr\Log\LoggerInterface;
7use Psr\Log\NullLogger;
7816eb62 8use Symfony\Component\EventDispatcher\EventDispatcherInterface;
f808b016
JB
9use Wallabag\CoreBundle\Entity\Entry;
10use Wallabag\CoreBundle\Entity\Tag;
7816eb62 11use Wallabag\CoreBundle\Event\EntrySavedEvent;
f808b016
JB
12use Wallabag\ImportBundle\Import\AbstractImport;
13use Wallabag\UserBundle\Repository\UserRepository;
7d862f83
JB
14
15abstract class AbstractConsumer
16{
17 protected $em;
18 protected $userRepository;
19 protected $import;
001cc716 20 protected $eventDispatcher;
7d862f83
JB
21 protected $logger;
22
7816eb62 23 public function __construct(EntityManager $em, UserRepository $userRepository, AbstractImport $import, EventDispatcherInterface $eventDispatcher, LoggerInterface $logger = null)
7d862f83
JB
24 {
25 $this->em = $em;
26 $this->userRepository = $userRepository;
27 $this->import = $import;
7816eb62 28 $this->eventDispatcher = $eventDispatcher;
7d862f83
JB
29 $this->logger = $logger ?: new NullLogger();
30 }
31
32 /**
33 * Handle a message and save it.
34 *
35 * @param string $body Message from the queue (in json)
36 *
37 * @return bool
38 */
39 protected function handleMessage($body)
40 {
41 $storedEntry = json_decode($body, true);
42
43 $user = $this->userRepository->find($storedEntry['userId']);
44
45 // no user? Drop message
46 if (null === $user) {
47 $this->logger->warning('Unable to retrieve user', ['entry' => $storedEntry]);
48
b45b6b67
NL
49 // return true to skip message
50 return true;
7d862f83
JB
51 }
52
53 $this->import->setUser($user);
54
9f8f188d
JB
55 if (false === $this->import->validateEntry($storedEntry)) {
56 $this->logger->warning('Entry is invalid', ['entry' => $storedEntry]);
57
58 // return true to skip message
59 return true;
60 }
61
7d862f83
JB
62 $entry = $this->import->parseEntry($storedEntry);
63
64 if (null === $entry) {
1e3d74a9 65 $this->logger->warning('Entry already exists', ['entry' => $storedEntry]);
7d862f83 66
1e3d74a9
JB
67 // return true to skip message
68 return true;
7d862f83
JB
69 }
70
71 try {
72 $this->em->flush();
73
7816eb62
JB
74 // entry saved, dispatch event about it!
75 $this->eventDispatcher->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry));
76
7d862f83
JB
77 // clear only affected entities
78 $this->em->clear(Entry::class);
79 $this->em->clear(Tag::class);
80 } catch (\Exception $e) {
81 $this->logger->warning('Unable to save entry', ['entry' => $storedEntry, 'exception' => $e]);
82
83 return false;
84 }
85
f808b016 86 $this->logger->info('Content with url imported! (' . $entry->getUrl() . ')');
7d862f83
JB
87
88 return true;
89 }
90}