]> git.immae.eu Git - github/wallabag/wallabag.git/blobdiff - src/Wallabag/ImportBundle/Import/PocketImport.php
Add test for RabbitMQ
[github/wallabag/wallabag.git] / src / Wallabag / ImportBundle / Import / PocketImport.php
index dd0ddd72567d315fdba93f261a1569264eb06b16..d76a3a08fa20b7bbe91d8db4e9f41081af64d240 100644 (file)
@@ -14,9 +14,9 @@ class PocketImport extends AbstractImport
 {
     private $client;
     private $consumerKey;
-    private $skippedEntries = 0;
-    private $importedEntries = 0;
-    protected $accessToken;
+    private $accessToken;
+
+    const NB_ELEMENTS = 5000;
 
     public function __construct(EntityManager $em, ContentProxy $contentProxy, Config $craueConfig)
     {
@@ -26,6 +26,16 @@ class PocketImport extends AbstractImport
         $this->logger = new NullLogger();
     }
 
+    /**
+     * Only used for test purpose.
+     *
+     * @return string
+     */
+    public function getAccessToken()
+    {
+        return $this->accessToken;
+    }
+
     /**
      * {@inheritdoc}
      */
@@ -114,8 +124,10 @@ class PocketImport extends AbstractImport
     /**
      * {@inheritdoc}
      */
-    public function import()
+    public function import($offset = 0)
     {
+        static $run = 0;
+
         $request = $this->client->createRequest('POST', 'https://getpocket.com/v3/get',
             [
                 'body' => json_encode([
@@ -123,7 +135,9 @@ class PocketImport extends AbstractImport
                     'access_token' => $this->accessToken,
                     'detailType' => 'complete',
                     'state' => 'all',
-                    'sort' => 'oldest',
+                    'sort' => 'newest',
+                    'count' => self::NB_ELEMENTS,
+                    'offset' => $offset,
                 ]),
             ]
         );
@@ -140,11 +154,20 @@ class PocketImport extends AbstractImport
 
         if ($this->producer) {
             $this->parseEntriesForProducer($entries['list']);
-
-            return true;
+        } else {
+            $this->parseEntries($entries['list']);
         }
 
-        $this->parseEntries($entries['list']);
+        // if we retrieve exactly the amount of items requested it means we can get more
+        // re-call import and offset item by the amount previous received:
+        //  - first call get 5k offset 0
+        //  - second call get 5k offset 5k
+        //  - and so on
+        if (count($entries['list']) === self::NB_ELEMENTS) {
+            ++$run;
+
+            return $this->import(self::NB_ELEMENTS * $run);
+        }
 
         return true;
     }
@@ -170,6 +193,11 @@ class PocketImport extends AbstractImport
         $this->client = $client;
     }
 
+    /**
+     * {@inheritdoc}
+     *
+     * @see https://getpocket.com/developer/docs/v3/retrieve
+     */
     public function parseEntry(array $importedEntry)
     {
         $url = isset($importedEntry['resolved_url']) && $importedEntry['resolved_url'] != '' ? $importedEntry['resolved_url'] : $importedEntry['given_url'];
@@ -233,24 +261,12 @@ class PocketImport extends AbstractImport
     }
 
     /**
-     * Faster parse entries for Producer.
-     * We don't care to make check at this time. They'll be done by the consumer.
-     *
-     * @param array $entries
+     * {@inheritdoc}
      */
-    public function parseEntriesForProducer($entries)
+    protected function setEntryAsRead(array $importedEntry)
     {
-        foreach ($entries as $importedEntry) {
-            // set userId for the producer (it won't know which user is connected)
-            $importedEntry['userId'] = $this->user->getId();
-
-            if ($this->markAsRead) {
-                $importedEntry['status'] = 1;
-            }
+        $importedEntry['status'] = '1';
 
-            ++$this->importedEntries;
-
-            $this->producer->publish(json_encode($importedEntry));
-        }
+        return $importedEntry;
     }
 }