aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/ImportBundle/Consumer/RedisEntryConsumer.php
diff options
context:
space:
mode:
authorJeremy Benoist <jeremy.benoist@gmail.com>2016-09-11 20:23:17 +0200
committerJeremy Benoist <jeremy.benoist@gmail.com>2016-09-11 22:15:31 +0200
commit7d862f83b95d24b4f081d73ca7b0bdf4435ae008 (patch)
tree334383a1fdbd651247116cd54cb9d85e6300fc19 /src/Wallabag/ImportBundle/Consumer/RedisEntryConsumer.php
parentdc69e25f97c357fdfdff5225f4f65cc55a6770b0 (diff)
downloadwallabag-7d862f83b95d24b4f081d73ca7b0bdf4435ae008.tar.gz
wallabag-7d862f83b95d24b4f081d73ca7b0bdf4435ae008.tar.zst
wallabag-7d862f83b95d24b4f081d73ca7b0bdf4435ae008.zip
Re-facto EntryConsumer
Using an abstract method allow to share code but also can be used it we add a new broker in the future
Diffstat (limited to 'src/Wallabag/ImportBundle/Consumer/RedisEntryConsumer.php')
-rw-r--r--src/Wallabag/ImportBundle/Consumer/RedisEntryConsumer.php59
1 files changed, 2 insertions, 57 deletions
diff --git a/src/Wallabag/ImportBundle/Consumer/RedisEntryConsumer.php b/src/Wallabag/ImportBundle/Consumer/RedisEntryConsumer.php
index 38665b01..450b71ff 100644
--- a/src/Wallabag/ImportBundle/Consumer/RedisEntryConsumer.php
+++ b/src/Wallabag/ImportBundle/Consumer/RedisEntryConsumer.php
@@ -3,29 +3,9 @@
3namespace Wallabag\ImportBundle\Consumer; 3namespace Wallabag\ImportBundle\Consumer;
4 4
5use Simpleue\Job\Job; 5use Simpleue\Job\Job;
6use Doctrine\ORM\EntityManager;
7use Wallabag\ImportBundle\Import\AbstractImport;
8use Wallabag\UserBundle\Repository\UserRepository;
9use Wallabag\CoreBundle\Entity\Entry;
10use Wallabag\CoreBundle\Entity\Tag;
11use Psr\Log\LoggerInterface;
12use Psr\Log\NullLogger;
13 6
14class RedisEntryConsumer implements Job 7class RedisEntryConsumer extends AbstractConsumer implements Job
15{ 8{
16 private $em;
17 private $userRepository;
18 private $import;
19 private $logger;
20
21 public function __construct(EntityManager $em, UserRepository $userRepository, AbstractImport $import, LoggerInterface $logger = null)
22 {
23 $this->em = $em;
24 $this->userRepository = $userRepository;
25 $this->import = $import;
26 $this->logger = $logger ?: new NullLogger();
27 }
28
29 /** 9 /**
30 * Handle one message by one message. 10 * Handle one message by one message.
31 * 11 *
@@ -35,42 +15,7 @@ class RedisEntryConsumer implements Job
35 */ 15 */
36 public function manage($job) 16 public function manage($job)
37 { 17 {
38 $storedEntry = json_decode($job, true); 18 return $this->handleMessage($job);
39
40 $user = $this->userRepository->find($storedEntry['userId']);
41
42 // no user? Drop message
43 if (null === $user) {
44 $this->logger->warning('Unable to retrieve user', ['entry' => $storedEntry]);
45
46 return false;
47 }
48
49 $this->import->setUser($user);
50
51 $entry = $this->import->parseEntry($storedEntry);
52
53 if (null === $entry) {
54 $this->logger->warning('Unable to parse entry', ['entry' => $storedEntry]);
55
56 return false;
57 }
58
59 try {
60 $this->em->flush();
61
62 // clear only affected entities
63 $this->em->clear(Entry::class);
64 $this->em->clear(Tag::class);
65 } catch (\Exception $e) {
66 $this->logger->warning('Unable to save entry', ['entry' => $storedEntry, 'exception' => $e]);
67
68 return false;
69 }
70
71 $this->logger->info('Content with url ('.$entry->getUrl().') imported !');
72
73 return true;
74 } 19 }
75 20
76 /** 21 /**