]>
Commit | Line | Data |
---|---|---|
ef75e122 JB |
1 | <?php |
2 | ||
b3437d58 | 3 | namespace Wallabag\ImportBundle\Consumer; |
ef75e122 | 4 | |
b3437d58 | 5 | use Simpleue\Job\Job; |
ef75e122 | 6 | use Doctrine\ORM\EntityManager; |
c98db1b6 | 7 | use Wallabag\ImportBundle\Import\AbstractImport; |
ef75e122 | 8 | use Wallabag\UserBundle\Repository\UserRepository; |
8664069e JB |
9 | use Wallabag\CoreBundle\Entity\Entry; |
10 | use Wallabag\CoreBundle\Entity\Tag; | |
ef75e122 JB |
11 | use Psr\Log\LoggerInterface; |
12 | use Psr\Log\NullLogger; | |
13 | ||
b3437d58 | 14 | class RedisEntryConsumer implements Job |
ef75e122 JB |
15 | { |
16 | private $em; | |
17 | private $userRepository; | |
c98db1b6 | 18 | private $import; |
ef75e122 JB |
19 | private $logger; |
20 | ||
c98db1b6 | 21 | public function __construct(EntityManager $em, UserRepository $userRepository, AbstractImport $import, LoggerInterface $logger = null) |
ef75e122 JB |
22 | { |
23 | $this->em = $em; | |
24 | $this->userRepository = $userRepository; | |
c98db1b6 | 25 | $this->import = $import; |
ef75e122 JB |
26 | $this->logger = $logger ?: new NullLogger(); |
27 | } | |
28 | ||
29 | /** | |
b3437d58 JB |
30 | * Handle one message by one message. |
31 | * | |
32 | * @param string $job Content of the message (directly from Redis) | |
33 | * | |
34 | * @return bool | |
ef75e122 | 35 | */ |
b3437d58 | 36 | public function manage($job) |
ef75e122 | 37 | { |
b3437d58 | 38 | $storedEntry = json_decode($job, true); |
ef75e122 JB |
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 | ||
b3437d58 | 46 | return false; |
ef75e122 JB |
47 | } |
48 | ||
c98db1b6 | 49 | $this->import->setUser($user); |
ef75e122 | 50 | |
c98db1b6 | 51 | $entry = $this->import->parseEntry($storedEntry); |
ef75e122 JB |
52 | |
53 | if (null === $entry) { | |
54 | $this->logger->warning('Unable to parse entry', ['entry' => $storedEntry]); | |
55 | ||
b3437d58 | 56 | return false; |
ef75e122 JB |
57 | } |
58 | ||
59 | try { | |
60 | $this->em->flush(); | |
8664069e JB |
61 | |
62 | // clear only affected entities | |
63 | $this->em->clear(Entry::class); | |
64 | $this->em->clear(Tag::class); | |
ef75e122 JB |
65 | } catch (\Exception $e) { |
66 | $this->logger->warning('Unable to save entry', ['entry' => $storedEntry, 'exception' => $e]); | |
67 | ||
b3437d58 | 68 | return false; |
ef75e122 | 69 | } |
b3437d58 JB |
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; | |
ef75e122 JB |
83 | } |
84 | } |