]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/ImportBundle/Import/AbstractImport.php
CS
[github/wallabag/wallabag.git] / src / Wallabag / ImportBundle / Import / AbstractImport.php
1 <?php
2
3 namespace Wallabag\ImportBundle\Import;
4
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;
12
13 abstract class AbstractImport implements ImportInterface
14 {
15 protected $em;
16 protected $logger;
17 protected $contentProxy;
18 protected $producer;
19 protected $user;
20 protected $markAsRead;
21 protected $skippedEntries = 0;
22 protected $importedEntries = 0;
23
24 public function __construct(EntityManager $em, ContentProxy $contentProxy)
25 {
26 $this->em = $em;
27 $this->logger = new NullLogger();
28 $this->contentProxy = $contentProxy;
29 }
30
31 public function setLogger(LoggerInterface $logger)
32 {
33 $this->logger = $logger;
34 }
35
36 /**
37 * Set RabbitMQ Producer to send each entry to a queue.
38 * This method should be called when user has enabled RabbitMQ.
39 *
40 * @param Producer $producer
41 */
42 public function setRabbitmqProducer(Producer $producer)
43 {
44 $this->producer = $producer;
45 }
46
47 /**
48 * Set current user.
49 * Could the current *connected* user or one retrieve by the consumer.
50 *
51 * @param User $user
52 */
53 public function setUser(User $user)
54 {
55 $this->user = $user;
56 }
57
58 /**
59 * Set whether articles must be all marked as read.
60 *
61 * @param bool $markAsRead
62 */
63 public function setMarkAsRead($markAsRead)
64 {
65 $this->markAsRead = $markAsRead;
66
67 return $this;
68 }
69
70 /**
71 * Get whether articles must be all marked as read.
72 */
73 public function getMarkAsRead()
74 {
75 return $this->markAsRead;
76 }
77
78 /**
79 * Fetch content from the ContentProxy (using graby).
80 * If it fails return false instead of the updated entry.
81 *
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
85 *
86 * @return Entry|false
87 */
88 protected function fetchContent(Entry $entry, $url, array $content = [])
89 {
90 try {
91 return $this->contentProxy->updateEntry($entry, $url, $content);
92 } catch (\Exception $e) {
93 return false;
94 }
95 }
96
97 /**
98 * Parse and insert all given entries.
99 *
100 * @param $entries
101 */
102 protected function parseEntries($entries)
103 {
104 $i = 1;
105
106 foreach ($entries as $importedEntry) {
107 $entry = $this->parseEntry($importedEntry);
108
109 if (null === $entry) {
110 continue;
111 }
112
113 // flush every 20 entries
114 if (($i % 20) === 0) {
115 $this->em->flush();
116 $this->em->clear($entry);
117 }
118 ++$i;
119 }
120
121 $this->em->flush();
122 }
123
124 /**
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.
128 *
129 * Faster parse entries for Producer.
130 * We don't care to make check at this time. They'll be done by the consumer.
131 *
132 * @param array $entries
133 */
134 protected function parseEntriesForProducer(array $entries)
135 {
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();
139
140 if ($this->markAsRead) {
141 $importedEntry = $this->setEntryAsRead($importedEntry);
142 }
143
144 ++$this->importedEntries;
145
146 $this->producer->publish(json_encode($importedEntry));
147 }
148 }
149
150 /**
151 * Parse one entry.
152 *
153 * @param array $importedEntry
154 *
155 * @return Entry
156 */
157 abstract public function parseEntry(array $importedEntry);
158
159 /**
160 * Set current imported entry to archived / read.
161 * Implementation is different accross all imports.
162 *
163 * @param array $importedEntry
164 *
165 * @return array
166 */
167 abstract protected function setEntryAsRead(array $importedEntry);
168 }