aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/ImportBundle
diff options
context:
space:
mode:
authorNicolas LÅ“uillet <nicolas@loeuillet.org>2016-02-19 21:21:47 +0100
committerNicolas LÅ“uillet <nicolas@loeuillet.org>2016-02-19 21:21:47 +0100
commit0cde91cc6f42497844866d7631b8f28e024af965 (patch)
tree3cca40042f576a67672bde1236bd059b710f4f97 /src/Wallabag/ImportBundle
parenta8c6b8ee0cb4e77731df502a05479a8501806309 (diff)
parentc2656f96d4776c86b13d8a4c93a78ee7c4d3824c (diff)
downloadwallabag-0cde91cc6f42497844866d7631b8f28e024af965.tar.gz
wallabag-0cde91cc6f42497844866d7631b8f28e024af965.tar.zst
wallabag-0cde91cc6f42497844866d7631b8f28e024af965.zip
Merge pull request #1699 from wallabag/v2-assign-tag
Move assignTagsToEntry in ContentProxy helper
Diffstat (limited to 'src/Wallabag/ImportBundle')
-rw-r--r--src/Wallabag/ImportBundle/Import/PocketImport.php26
-rw-r--r--src/Wallabag/ImportBundle/Import/WallabagV1Import.php28
-rw-r--r--src/Wallabag/ImportBundle/Tests/Import/PocketImportTest.php18
3 files changed, 13 insertions, 59 deletions
diff --git a/src/Wallabag/ImportBundle/Import/PocketImport.php b/src/Wallabag/ImportBundle/Import/PocketImport.php
index 22932238..5dfd098c 100644
--- a/src/Wallabag/ImportBundle/Import/PocketImport.php
+++ b/src/Wallabag/ImportBundle/Import/PocketImport.php
@@ -9,7 +9,6 @@ use GuzzleHttp\Client;
9use GuzzleHttp\Exception\RequestException; 9use GuzzleHttp\Exception\RequestException;
10use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; 10use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
11use Wallabag\CoreBundle\Entity\Entry; 11use Wallabag\CoreBundle\Entity\Entry;
12use Wallabag\CoreBundle\Entity\Tag;
13use Wallabag\CoreBundle\Helper\ContentProxy; 12use Wallabag\CoreBundle\Helper\ContentProxy;
14use Craue\ConfigBundle\Util\Config; 13use Craue\ConfigBundle\Util\Config;
15 14
@@ -177,26 +176,6 @@ class PocketImport implements ImportInterface
177 $this->client = $client; 176 $this->client = $client;
178 } 177 }
179 178
180 private function assignTagsToEntry(Entry $entry, $tags)
181 {
182 foreach ($tags as $tag) {
183 $label = trim($tag['tag']);
184 $tagEntity = $this->em
185 ->getRepository('WallabagCoreBundle:Tag')
186 ->findOneByLabel($label);
187
188 if (is_object($tagEntity)) {
189 $entry->addTag($tagEntity);
190 } else {
191 $newTag = new Tag();
192 $newTag->setLabel($label);
193
194 $entry->addTag($newTag);
195 }
196 $this->em->flush();
197 }
198 }
199
200 /** 179 /**
201 * @see https://getpocket.com/developer/docs/v3/retrieve 180 * @see https://getpocket.com/developer/docs/v3/retrieve
202 * 181 *
@@ -246,7 +225,10 @@ class PocketImport implements ImportInterface
246 } 225 }
247 226
248 if (isset($pocketEntry['tags']) && !empty($pocketEntry['tags'])) { 227 if (isset($pocketEntry['tags']) && !empty($pocketEntry['tags'])) {
249 $this->assignTagsToEntry($entry, $pocketEntry['tags']); 228 $this->contentProxy->assignTagsToEntry(
229 $entry,
230 array_keys($pocketEntry['tags'])
231 );
250 } 232 }
251 233
252 $this->em->persist($entry); 234 $this->em->persist($entry);
diff --git a/src/Wallabag/ImportBundle/Import/WallabagV1Import.php b/src/Wallabag/ImportBundle/Import/WallabagV1Import.php
index bbac6eaf..05bdb401 100644
--- a/src/Wallabag/ImportBundle/Import/WallabagV1Import.php
+++ b/src/Wallabag/ImportBundle/Import/WallabagV1Import.php
@@ -6,7 +6,6 @@ use Psr\Log\LoggerInterface;
6use Psr\Log\NullLogger; 6use Psr\Log\NullLogger;
7use Doctrine\ORM\EntityManager; 7use Doctrine\ORM\EntityManager;
8use Wallabag\CoreBundle\Entity\Entry; 8use Wallabag\CoreBundle\Entity\Entry;
9use Wallabag\CoreBundle\Entity\Tag;
10use Wallabag\UserBundle\Entity\User; 9use Wallabag\UserBundle\Entity\User;
11use Wallabag\CoreBundle\Tools\Utils; 10use Wallabag\CoreBundle\Tools\Utils;
12use Wallabag\CoreBundle\Helper\ContentProxy; 11use Wallabag\CoreBundle\Helper\ContentProxy;
@@ -144,6 +143,7 @@ class WallabagV1Import implements ImportInterface
144 // @see ContentProxy->updateEntry 143 // @see ContentProxy->updateEntry
145 $entry = new Entry($this->user); 144 $entry = new Entry($this->user);
146 $entry->setUrl($importedEntry['url']); 145 $entry->setUrl($importedEntry['url']);
146
147 if (in_array($importedEntry['title'], $untitled)) { 147 if (in_array($importedEntry['title'], $untitled)) {
148 $entry = $this->contentProxy->updateEntry($entry, $importedEntry['url']); 148 $entry = $this->contentProxy->updateEntry($entry, $importedEntry['url']);
149 } else { 149 } else {
@@ -152,10 +152,14 @@ class WallabagV1Import implements ImportInterface
152 $entry->setReadingTime(Utils::getReadingTime($importedEntry['content'])); 152 $entry->setReadingTime(Utils::getReadingTime($importedEntry['content']));
153 $entry->setDomainName(parse_url($importedEntry['url'], PHP_URL_HOST)); 153 $entry->setDomainName(parse_url($importedEntry['url'], PHP_URL_HOST));
154 } 154 }
155
155 if (array_key_exists('tags', $importedEntry) && $importedEntry['tags'] != '') { 156 if (array_key_exists('tags', $importedEntry) && $importedEntry['tags'] != '') {
156 $tags = explode(',', $importedEntry['tags']); 157 $this->contentProxy->assignTagsToEntry(
157 $this->assignTagsToEntry($entry, $tags); 158 $entry,
159 $importedEntry['tags']
160 );
158 } 161 }
162
159 $entry->setArchived($importedEntry['is_read']); 163 $entry->setArchived($importedEntry['is_read']);
160 $entry->setStarred($importedEntry['is_fav']); 164 $entry->setStarred($importedEntry['is_fav']);
161 165
@@ -171,22 +175,4 @@ class WallabagV1Import implements ImportInterface
171 175
172 $this->em->flush(); 176 $this->em->flush();
173 } 177 }
174
175 private function assignTagsToEntry(Entry $entry, $tags)
176 {
177 foreach ($tags as $tag) {
178 $label = trim($tag);
179 $tagEntity = $this->em
180 ->getRepository('WallabagCoreBundle:Tag')
181 ->findOneByLabel($label);
182 if (is_object($tagEntity)) {
183 $entry->addTag($tagEntity);
184 } else {
185 $newTag = new Tag();
186 $newTag->setLabel($label);
187 $entry->addTag($newTag);
188 }
189 $this->em->flush();
190 }
191 }
192} 178}
diff --git a/src/Wallabag/ImportBundle/Tests/Import/PocketImportTest.php b/src/Wallabag/ImportBundle/Tests/Import/PocketImportTest.php
index 25359d56..f44786b1 100644
--- a/src/Wallabag/ImportBundle/Tests/Import/PocketImportTest.php
+++ b/src/Wallabag/ImportBundle/Tests/Import/PocketImportTest.php
@@ -260,24 +260,10 @@ class PocketImportTest extends \PHPUnit_Framework_TestCase
260 ->method('findByUrlAndUserId') 260 ->method('findByUrlAndUserId')
261 ->will($this->onConsecutiveCalls(false, true)); 261 ->will($this->onConsecutiveCalls(false, true));
262 262
263 $tag = $this->getMockBuilder('Wallabag\CoreBundle\Entity\Tag')
264 ->disableOriginalConstructor()
265 ->getMock();
266
267 $tagRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\TagRepository')
268 ->disableOriginalConstructor()
269 ->getMock();
270
271 $tagRepo->expects($this->exactly(2))
272 // the method `findOneByLabel` doesn't exist, EntityRepository will then call `_call` method
273 // to magically call the `findOneBy` with ['label' => 'foo']
274 ->method('__call')
275 ->will($this->onConsecutiveCalls(false, $tag));
276
277 $this->em 263 $this->em
278 ->expects($this->any()) 264 ->expects($this->exactly(2))
279 ->method('getRepository') 265 ->method('getRepository')
280 ->will($this->onConsecutiveCalls($entryRepo, $tagRepo, $tagRepo, $entryRepo)); 266 ->willReturn($entryRepo);
281 267
282 $entry = $this->getMockBuilder('Wallabag\CoreBundle\Entity\Entry') 268 $entry = $this->getMockBuilder('Wallabag\CoreBundle\Entity\Entry')
283 ->disableOriginalConstructor() 269 ->disableOriginalConstructor()