diff options
Diffstat (limited to 'src/Wallabag/ImportBundle/Consumer/RedisEntryConsumer.php')
-rw-r--r-- | src/Wallabag/ImportBundle/Consumer/RedisEntryConsumer.php | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/src/Wallabag/ImportBundle/Consumer/RedisEntryConsumer.php b/src/Wallabag/ImportBundle/Consumer/RedisEntryConsumer.php new file mode 100644 index 00000000..38665b01 --- /dev/null +++ b/src/Wallabag/ImportBundle/Consumer/RedisEntryConsumer.php | |||
@@ -0,0 +1,84 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Wallabag\ImportBundle\Consumer; | ||
4 | |||
5 | use Simpleue\Job\Job; | ||
6 | use Doctrine\ORM\EntityManager; | ||
7 | use Wallabag\ImportBundle\Import\AbstractImport; | ||
8 | use Wallabag\UserBundle\Repository\UserRepository; | ||
9 | use Wallabag\CoreBundle\Entity\Entry; | ||
10 | use Wallabag\CoreBundle\Entity\Tag; | ||
11 | use Psr\Log\LoggerInterface; | ||
12 | use Psr\Log\NullLogger; | ||
13 | |||
14 | class RedisEntryConsumer implements Job | ||
15 | { | ||
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 | /** | ||
30 | * Handle one message by one message. | ||
31 | * | ||
32 | * @param string $job Content of the message (directly from Redis) | ||
33 | * | ||
34 | * @return bool | ||
35 | */ | ||
36 | public function manage($job) | ||
37 | { | ||
38 | $storedEntry = json_decode($job, true); | ||
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 | } | ||
75 | |||
76 | /** | ||
77 | * Should tell if the given job will kill the worker. | ||
78 | * We don't want to stop it :). | ||
79 | */ | ||
80 | public function isStopJob($job) | ||
81 | { | ||
82 | return false; | ||
83 | } | ||
84 | } | ||