diff options
author | Jeremy Benoist <jeremy.benoist@gmail.com> | 2016-09-04 21:49:21 +0200 |
---|---|---|
committer | Jeremy Benoist <jeremy.benoist@gmail.com> | 2016-09-11 21:58:31 +0200 |
commit | c98db1b653b5dc8b701422190b02d9fbf10c4e68 (patch) | |
tree | 5f02de371264143a4e81231ad16f605435cb4d22 /src/Wallabag/ImportBundle/Import/AbstractImport.php | |
parent | ef75e1220ebb76a8df019d946460ad612759f0bb (diff) | |
download | wallabag-c98db1b653b5dc8b701422190b02d9fbf10c4e68.tar.gz wallabag-c98db1b653b5dc8b701422190b02d9fbf10c4e68.tar.zst wallabag-c98db1b653b5dc8b701422190b02d9fbf10c4e68.zip |
Convert other imports to Rabbit
Diffstat (limited to 'src/Wallabag/ImportBundle/Import/AbstractImport.php')
-rw-r--r-- | src/Wallabag/ImportBundle/Import/AbstractImport.php | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/src/Wallabag/ImportBundle/Import/AbstractImport.php b/src/Wallabag/ImportBundle/Import/AbstractImport.php index 14377a35..5b9d65d7 100644 --- a/src/Wallabag/ImportBundle/Import/AbstractImport.php +++ b/src/Wallabag/ImportBundle/Import/AbstractImport.php | |||
@@ -7,12 +7,17 @@ use Psr\Log\NullLogger; | |||
7 | use Doctrine\ORM\EntityManager; | 7 | use Doctrine\ORM\EntityManager; |
8 | use Wallabag\CoreBundle\Helper\ContentProxy; | 8 | use Wallabag\CoreBundle\Helper\ContentProxy; |
9 | use Wallabag\CoreBundle\Entity\Entry; | 9 | use Wallabag\CoreBundle\Entity\Entry; |
10 | use Symfony\Component\Security\Core\User\UserInterface; | ||
11 | use OldSound\RabbitMqBundle\RabbitMq\Producer; | ||
10 | 12 | ||
11 | abstract class AbstractImport implements ImportInterface | 13 | abstract class AbstractImport implements ImportInterface |
12 | { | 14 | { |
13 | protected $em; | 15 | protected $em; |
14 | protected $logger; | 16 | protected $logger; |
15 | protected $contentProxy; | 17 | protected $contentProxy; |
18 | protected $producer; | ||
19 | protected $user; | ||
20 | protected $markAsRead; | ||
16 | 21 | ||
17 | public function __construct(EntityManager $em, ContentProxy $contentProxy) | 22 | public function __construct(EntityManager $em, ContentProxy $contentProxy) |
18 | { | 23 | { |
@@ -27,6 +32,48 @@ abstract class AbstractImport implements ImportInterface | |||
27 | } | 32 | } |
28 | 33 | ||
29 | /** | 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 | /** | ||
30 | * Fetch content from the ContentProxy (using graby). | 77 | * Fetch content from the ContentProxy (using graby). |
31 | * If it fails return false instead of the updated entry. | 78 | * If it fails return false instead of the updated entry. |
32 | * | 79 | * |
@@ -44,4 +91,40 @@ abstract class AbstractImport implements ImportInterface | |||
44 | return false; | 91 | return false; |
45 | } | 92 | } |
46 | } | 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); | ||
47 | } | 130 | } |