]>
Commit | Line | Data |
---|---|---|
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 Symfony\Component\Security\Core\User\UserInterface; | |
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 UserInterface $user | |
50 | */ | |
51 | public function setUser(UserInterface $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 one entry. | |
124 | * | |
125 | * @param array $importedEntry | |
126 | * | |
127 | * @return Entry | |
128 | */ | |
129 | abstract public function parseEntry(array $importedEntry); | |
130 | } |