]> git.immae.eu Git - github/wallabag/wallabag.git/blobdiff - src/Wallabag/ImportBundle/Import/PocketImport.php
Replace RabbitMQ injection with CraueConfiguration
[github/wallabag/wallabag.git] / src / Wallabag / ImportBundle / Import / PocketImport.php
index 22932238aeaa6c56f4982d90d5766f8e30f34ddc..7d1c0c617bbc78dc36a58d731e8252bc81925978 100644 (file)
@@ -2,6 +2,7 @@
 
 namespace Wallabag\ImportBundle\Import;
 
+use OldSound\RabbitMqBundle\RabbitMq\Producer;
 use Psr\Log\LoggerInterface;
 use Psr\Log\NullLogger;
 use Doctrine\ORM\EntityManager;
@@ -9,34 +10,30 @@ 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;
 
-class PocketImport implements ImportInterface
+class PocketImport extends AbstractImport
 {
     private $user;
-    private $em;
-    private $contentProxy;
-    private $logger;
     private $client;
     private $consumerKey;
     private $skippedEntries = 0;
     private $importedEntries = 0;
+    private $markAsRead;
     protected $accessToken;
+    private $producer;
+    private $rabbitMQ;
 
-    public function __construct(TokenStorageInterface $tokenStorage, EntityManager $em, ContentProxy $contentProxy, Config $craueConfig)
+    public function __construct(TokenStorageInterface $tokenStorage, EntityManager $em, ContentProxy $contentProxy, Config $craueConfig, Producer $producer)
     {
         $this->user = $tokenStorage->getToken()->getUser();
         $this->em = $em;
         $this->contentProxy = $contentProxy;
         $this->consumerKey = $craueConfig->get('pocket_consumer_key');
         $this->logger = new NullLogger();
-    }
-
-    public function setLogger(LoggerInterface $logger)
-    {
-        $this->logger = $logger;
+        $this->rabbitMQ = $craueConfig->get('rabbitmq');
+        $this->producer = $producer;
     }
 
     /**
@@ -60,7 +57,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';
     }
 
     /**
@@ -68,7 +65,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)
     {
@@ -124,6 +121,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}
      */
@@ -177,26 +194,6 @@ class PocketImport implements ImportInterface
         $this->client = $client;
     }
 
-    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
      *
@@ -206,7 +203,7 @@ class PocketImport implements ImportInterface
     {
         $i = 1;
 
-        foreach ($entries as $pocketEntry) {
+        foreach ($entries as &$pocketEntry) {
             $url = isset($pocketEntry['resolved_url']) && $pocketEntry['resolved_url'] != '' ? $pocketEntry['resolved_url'] : $pocketEntry['given_url'];
 
             $existingEntry = $this->em
@@ -219,14 +216,23 @@ class PocketImport implements ImportInterface
             }
 
             $entry = new Entry($this->user);
-            $entry = $this->contentProxy->updateEntry($entry, $url);
+
+            if (!$this->rabbitMQ) {
+                $entry = $this->fetchContent($entry, $url);
+
+                // jump to next entry in case of problem while getting content
+                if (false === $entry) {
+                    ++$this->skippedEntries;
+                    continue;
+                }
+            }
 
             // 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);
             }
@@ -239,6 +245,7 @@ class PocketImport implements ImportInterface
             }
 
             $entry->setTitle($title);
+            $entry->setUrl($url);
 
             // 0, 1, or 2 - 1 if the item has images in it - 2 if the item is an image
             if (isset($pocketEntry['has_image']) && $pocketEntry['has_image'] > 0 && isset($pocketEntry['images'][1])) {
@@ -246,9 +253,15 @@ class PocketImport implements ImportInterface
             }
 
             if (isset($pocketEntry['tags']) && !empty($pocketEntry['tags'])) {
-                $this->assignTagsToEntry($entry, $pocketEntry['tags']);
+                $this->contentProxy->assignTagsToEntry(
+                    $entry,
+                    array_keys($pocketEntry['tags'])
+                );
             }
 
+            $pocketEntry['url'] = $url;
+            $pocketEntry['userId'] = $this->user->getId();
+
             $this->em->persist($entry);
             ++$this->importedEntries;
 
@@ -256,9 +269,16 @@ class PocketImport implements ImportInterface
             if (($i % 20) === 0) {
                 $this->em->flush();
             }
+
             ++$i;
         }
 
         $this->em->flush();
+
+        if ($this->rabbitMQ) {
+            foreach ($entries as $entry) {
+                $this->producer->publish(serialize($entry));
+            }
+        }
     }
 }