namespace Wallabag\ImportBundle\Import;
-use GuzzleHttp\Client;
-use GuzzleHttp\Exception\RequestException;
+use Http\Client\Common\HttpMethodsClient;
+use Http\Client\Common\Plugin\ErrorPlugin;
+use Http\Client\Common\PluginClient;
+use Http\Client\Exception\RequestException;
+use Http\Client\HttpClient;
+use Http\Discovery\MessageFactoryDiscovery;
+use Http\Message\MessageFactory;
+use Psr\Http\Message\ResponseInterface;
use Wallabag\CoreBundle\Entity\Entry;
class PocketImport extends AbstractImport
{
+ const NB_ELEMENTS = 5000;
+ /**
+ * @var HttpMethodsClient
+ */
private $client;
private $accessToken;
- const NB_ELEMENTS = 5000;
-
/**
* Only used for test purpose.
*
*/
public function getRequestToken($redirectUri)
{
- $request = $this->client->createRequest('POST', 'https://getpocket.com/v3/oauth/request',
- [
- 'body' => json_encode([
- 'consumer_key' => $this->user->getConfig()->getPocketConsumerKey(),
- 'redirect_uri' => $redirectUri,
- ]),
- ]
- );
-
try {
- $response = $this->client->send($request);
+ $response = $this->client->post('https://getpocket.com/v3/oauth/request', [], json_encode([
+ 'consumer_key' => $this->user->getConfig()->getPocketConsumerKey(),
+ 'redirect_uri' => $redirectUri,
+ ]));
} catch (RequestException $e) {
$this->logger->error(sprintf('PocketImport: Failed to request token: %s', $e->getMessage()), ['exception' => $e]);
return false;
}
- return $response->json()['code'];
+ return $this->jsonDecode($response)['code'];
}
/**
*/
public function authorize($code)
{
- $request = $this->client->createRequest('POST', 'https://getpocket.com/v3/oauth/authorize',
- [
- 'body' => json_encode([
- 'consumer_key' => $this->user->getConfig()->getPocketConsumerKey(),
- 'code' => $code,
- ]),
- ]
- );
-
try {
- $response = $this->client->send($request);
+ $response = $this->client->post('https://getpocket.com/v3/oauth/authorize', [], json_encode([
+ 'consumer_key' => $this->user->getConfig()->getPocketConsumerKey(),
+ 'code' => $code,
+ ]));
} catch (RequestException $e) {
$this->logger->error(sprintf('PocketImport: Failed to authorize client: %s', $e->getMessage()), ['exception' => $e]);
return false;
}
- $this->accessToken = $response->json()['access_token'];
+ $this->accessToken = $this->jsonDecode($response)['access_token'];
return true;
}
{
static $run = 0;
- $request = $this->client->createRequest('POST', 'https://getpocket.com/v3/get',
- [
- 'body' => json_encode([
- 'consumer_key' => $this->user->getConfig()->getPocketConsumerKey(),
- 'access_token' => $this->accessToken,
- 'detailType' => 'complete',
- 'state' => 'all',
- 'sort' => 'newest',
- 'count' => self::NB_ELEMENTS,
- 'offset' => $offset,
- ]),
- ]
- );
-
try {
- $response = $this->client->send($request);
+ $response = $this->client->post('https://getpocket.com/v3/get', [], json_encode([
+ 'consumer_key' => $this->user->getConfig()->getPocketConsumerKey(),
+ 'access_token' => $this->accessToken,
+ 'detailType' => 'complete',
+ 'state' => 'all',
+ 'sort' => 'newest',
+ 'count' => self::NB_ELEMENTS,
+ 'offset' => $offset,
+ ]));
} catch (RequestException $e) {
$this->logger->error(sprintf('PocketImport: Failed to import: %s', $e->getMessage()), ['exception' => $e]);
return false;
}
- $entries = $response->json();
+ $entries = $this->jsonDecode($response);
if ($this->producer) {
$this->parseEntriesForProducer($entries['list']);
// - first call get 5k offset 0
// - second call get 5k offset 5k
// - and so on
- if (count($entries['list']) === self::NB_ELEMENTS) {
+ if (self::NB_ELEMENTS === \count($entries['list'])) {
++$run;
return $this->import(self::NB_ELEMENTS * $run);
}
/**
- * Set the Guzzle client.
- *
- * @param Client $client
+ * Set the Http client.
*/
- public function setClient(Client $client)
+ public function setClient(HttpClient $client, MessageFactory $messageFactory = null)
{
- $this->client = $client;
+ $this->client = new HttpMethodsClient(new PluginClient($client, [new ErrorPlugin()]), $messageFactory ?: MessageFactoryDiscovery::find());
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function validateEntry(array $importedEntry)
+ {
+ if (empty($importedEntry['resolved_url']) && empty($importedEntry['given_url'])) {
+ return false;
+ }
+
+ return true;
}
/**
*/
public function parseEntry(array $importedEntry)
{
- $url = isset($importedEntry['resolved_url']) && $importedEntry['resolved_url'] != '' ? $importedEntry['resolved_url'] : $importedEntry['given_url'];
+ $url = isset($importedEntry['resolved_url']) && '' !== $importedEntry['resolved_url'] ? $importedEntry['resolved_url'] : $importedEntry['given_url'];
$existingEntry = $this->em
->getRepository('WallabagCoreBundle:Entry')
$this->fetchContent($entry, $url);
// 0, 1, 2 - 1 if the item is archived - 2 if the item should be deleted
- $entry->setArchived($importedEntry['status'] == 1 || $this->markAsRead);
+ $entry->updateArchived(1 === (int) $importedEntry['status'] || $this->markAsRead);
- // 0 or 1 - 1 If the item is starred
- $entry->setStarred($importedEntry['favorite'] == 1);
+ // 0 or 1 - 1 if the item is starred
+ $entry->setStarred(1 === (int) $importedEntry['favorite']);
$title = 'Untitled';
- if (isset($importedEntry['resolved_title']) && $importedEntry['resolved_title'] != '') {
+ if (isset($importedEntry['resolved_title']) && '' !== $importedEntry['resolved_title']) {
$title = $importedEntry['resolved_title'];
- } elseif (isset($importedEntry['given_title']) && $importedEntry['given_title'] != '') {
+ } elseif (isset($importedEntry['given_title']) && '' !== $importedEntry['given_title']) {
$title = $importedEntry['given_title'];
}
return $importedEntry;
}
+
+ protected function jsonDecode(ResponseInterface $response)
+ {
+ $data = json_decode((string) $response->getBody(), true);
+
+ if (JSON_ERROR_NONE !== json_last_error()) {
+ throw new \InvalidArgumentException('Unable to parse JSON data: ' . json_last_error_msg());
+ }
+
+ return $data;
+ }
}