3 namespace Wallabag\ImportBundle\Import
;
5 use Psr\Log\LoggerInterface
;
6 use Psr\Log\NullLogger
;
7 use Doctrine\ORM\EntityManager
;
8 use Wallabag\CoreBundle\Helper\ContentProxy
;
9 use Wallabag\CoreBundle\Entity\Entry
;
10 use Wallabag\UserBundle\Entity\User
;
11 use OldSound\RabbitMqBundle\RabbitMq\Producer
;
13 abstract class AbstractImport
implements ImportInterface
17 protected $contentProxy;
20 protected $markAsRead;
22 public function __construct(EntityManager
$em, ContentProxy
$contentProxy)
25 $this->logger
= new NullLogger();
26 $this->contentProxy
= $contentProxy;
29 public function setLogger(LoggerInterface
$logger)
31 $this->logger
= $logger;
35 * Set RabbitMQ Producer to send each entry to a queue.
36 * This method should be called when user has enabled RabbitMQ.
38 * @param Producer $producer
40 public function setRabbitmqProducer(Producer
$producer)
42 $this->producer
= $producer;
47 * Could the current *connected* user or one retrieve by the consumer.
51 public function setUser(User
$user)
57 * Set whether articles must be all marked as read.
59 * @param bool $markAsRead
61 public function setMarkAsRead($markAsRead)
63 $this->markAsRead
= $markAsRead;
69 * Get whether articles must be all marked as read.
71 public function getMarkAsRead()
73 return $this->markAsRead
;
77 * Fetch content from the ContentProxy (using graby).
78 * If it fails return false instead of the updated entry.
80 * @param Entry $entry Entry to update
81 * @param string $url Url to grab content for
82 * @param array $content An array with AT LEAST keys title, html, url, language & content_type to skip the fetchContent from the url
86 protected function fetchContent(Entry
$entry, $url, array $content = [])
89 return $this->contentProxy
->updateEntry($entry, $url, $content);
90 } catch (\Exception
$e) {
96 * Parse and insert all given entries.
100 protected function parseEntries($entries)
104 foreach ($entries as $importedEntry) {
105 $entry = $this->parseEntry($importedEntry);
107 if (null === $entry) {
111 // flush every 20 entries
112 if (($i %
20) === 0) {
114 $this->em
->clear($entry);
123 * Parse entries and send them to the queue.
124 * It should just be a simple loop on all item, no call to the database should be done
125 * to speedup queuing.
127 * Faster parse entries for Producer.
128 * We don't care to make check at this time. They'll be done by the consumer.
130 * @param array $entries
132 protected function parseEntriesForProducer(array $entries)
134 foreach ($entries as $importedEntry) {
135 // set userId for the producer (it won't know which user is connected)
136 $importedEntry['userId'] = $this->user
->getId();
138 if ($this->markAsRead
) {
139 $importedEntry = $this->setEntryAsRead($importedEntry);
142 ++
$this->importedEntries
;
144 $this->producer
->publish(json_encode($importedEntry));
151 * @param array $importedEntry
155 abstract public function parseEntry(array $importedEntry);
158 * Set current imported entry to archived / read.
159 * Implementation is different accross all imports.
161 * @param array $importedEntry
165 abstract protected function setEntryAsRead(array $importedEntry);