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