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;
21 protected $skippedEntries = 0;
22 protected $importedEntries = 0;
24 public function __construct(EntityManager
$em, ContentProxy
$contentProxy)
27 $this->logger
= new NullLogger();
28 $this->contentProxy
= $contentProxy;
31 public function setLogger(LoggerInterface
$logger)
33 $this->logger
= $logger;
37 * Set RabbitMQ Producer to send each entry to a queue.
38 * This method should be called when user has enabled RabbitMQ.
40 * @param Producer $producer
42 public function setRabbitmqProducer(Producer
$producer)
44 $this->producer
= $producer;
49 * Could the current *connected* user or one retrieve by the consumer.
53 public function setUser(User
$user)
59 * Set whether articles must be all marked as read.
61 * @param bool $markAsRead
63 public function setMarkAsRead($markAsRead)
65 $this->markAsRead
= $markAsRead;
71 * Get whether articles must be all marked as read.
73 public function getMarkAsRead()
75 return $this->markAsRead
;
79 * Fetch content from the ContentProxy (using graby).
80 * If it fails return false instead of the updated entry.
82 * @param Entry $entry Entry to update
83 * @param string $url Url to grab content for
84 * @param array $content An array with AT LEAST keys title, html, url, language & content_type to skip the fetchContent from the url
88 protected function fetchContent(Entry
$entry, $url, array $content = [])
91 return $this->contentProxy
->updateEntry($entry, $url, $content);
92 } catch (\Exception
$e) {
98 * Parse and insert all given entries.
102 protected function parseEntries($entries)
106 foreach ($entries as $importedEntry) {
107 $entry = $this->parseEntry($importedEntry);
109 if (null === $entry) {
113 // flush every 20 entries
114 if (($i %
20) === 0) {
116 $this->em
->clear($entry);
125 * Parse entries and send them to the queue.
126 * It should just be a simple loop on all item, no call to the database should be done
127 * to speedup queuing.
129 * Faster parse entries for Producer.
130 * We don't care to make check at this time. They'll be done by the consumer.
132 * @param array $entries
134 protected function parseEntriesForProducer(array $entries)
136 foreach ($entries as $importedEntry) {
137 // set userId for the producer (it won't know which user is connected)
138 $importedEntry['userId'] = $this->user
->getId();
140 if ($this->markAsRead
) {
141 $importedEntry = $this->setEntryAsRead($importedEntry);
144 ++
$this->importedEntries
;
146 $this->producer
->publish(json_encode($importedEntry));
153 * @param array $importedEntry
157 abstract public function parseEntry(array $importedEntry);
160 * Set current imported entry to archived / read.
161 * Implementation is different accross all imports.
163 * @param array $importedEntry
167 abstract protected function setEntryAsRead(array $importedEntry);