]> git.immae.eu Git - github/wallabag/wallabag.git/blobdiff - src/Wallabag/ImportBundle/Import/PocketImport.php
Replaced favorite word/icon with star one
[github/wallabag/wallabag.git] / src / Wallabag / ImportBundle / Import / PocketImport.php
index 0463a7399804d1b367cc251dbf65768b268ae3fc..19b63f176cb8d78c1a00d003617d5ad3cffe699c 100644 (file)
@@ -9,7 +9,6 @@ use GuzzleHttp\Client;
 use GuzzleHttp\Exception\RequestException;
 use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
 use Wallabag\CoreBundle\Entity\Entry;
-use Wallabag\CoreBundle\Entity\Tag;
 use Wallabag\CoreBundle\Helper\ContentProxy;
 use Craue\ConfigBundle\Util\Config;
 
@@ -23,8 +22,8 @@ class PocketImport implements ImportInterface
     private $consumerKey;
     private $skippedEntries = 0;
     private $importedEntries = 0;
+    private $markAsRead;
     protected $accessToken;
-    private $translator;
 
     public function __construct(TokenStorageInterface $tokenStorage, EntityManager $em, ContentProxy $contentProxy, Config $craueConfig)
     {
@@ -61,7 +60,7 @@ class PocketImport implements ImportInterface
      */
     public function getDescription()
     {
-        return 'This importer will import all your Pocket data. Pocket doesn\'t allow us to retrieve content from their service, so the readable content of each article will be re-fetched by wallabag.';
+        return 'import.pocket.description';
     }
 
     /**
@@ -69,7 +68,7 @@ class PocketImport implements ImportInterface
      *
      * @param string $redirectUri Redirect url in case of error
      *
-     * @return string request_token for callback method
+     * @return string|false request_token for callback method
      */
     public function getRequestToken($redirectUri)
     {
@@ -125,6 +124,26 @@ class PocketImport implements ImportInterface
         return true;
     }
 
+    /**
+     * Set whether articles must be all marked as read.
+     *
+     * @param bool $markAsRead
+     */
+    public function setMarkAsRead($markAsRead)
+    {
+        $this->markAsRead = $markAsRead;
+
+        return $this;
+    }
+
+    /**
+     * Get whether articles must be all marked as read.
+     */
+    public function getMarkAsRead()
+    {
+        return $this->markAsRead;
+    }
+
     /**
      * {@inheritdoc}
      */
@@ -178,29 +197,6 @@ class PocketImport implements ImportInterface
         $this->client = $client;
     }
 
-    /**
-     * @todo move that in a more global place
-     */
-    private function assignTagsToEntry(Entry $entry, $tags)
-    {
-        foreach ($tags as $tag) {
-            $label = trim($tag['tag']);
-            $tagEntity = $this->em
-                ->getRepository('WallabagCoreBundle:Tag')
-                ->findOneByLabel($label);
-
-            if (is_object($tagEntity)) {
-                $entry->addTag($tagEntity);
-            } else {
-                $newTag = new Tag();
-                $newTag->setLabel($label);
-
-                $entry->addTag($newTag);
-            }
-            $this->em->flush();
-        }
-    }
-
     /**
      * @see https://getpocket.com/developer/docs/v3/retrieve
      *
@@ -226,11 +222,11 @@ class PocketImport implements ImportInterface
             $entry = $this->contentProxy->updateEntry($entry, $url);
 
             // 0, 1, 2 - 1 if the item is archived - 2 if the item should be deleted
-            if ($pocketEntry['status'] == 1) {
+            if ($pocketEntry['status'] == 1 || $this->markAsRead) {
                 $entry->setArchived(true);
             }
 
-            // 0 or 1 - 1 If the item is favorited
+            // 0 or 1 - 1 If the item is starred
             if ($pocketEntry['favorite'] == 1) {
                 $entry->setStarred(true);
             }
@@ -250,7 +246,10 @@ class PocketImport implements ImportInterface
             }
 
             if (isset($pocketEntry['tags']) && !empty($pocketEntry['tags'])) {
-                $this->assignTagsToEntry($entry, $pocketEntry['tags']);
+                $this->contentProxy->assignTagsToEntry(
+                    $entry,
+                    array_keys($pocketEntry['tags'])
+                );
             }
 
             $this->em->persist($entry);
@@ -259,6 +258,7 @@ class PocketImport implements ImportInterface
             // flush every 20 entries
             if (($i % 20) === 0) {
                 $this->em->flush();
+                $this->em->clear($entry);
             }
             ++$i;
         }