namespace Wallabag\ImportBundle\Import;
+use OldSound\RabbitMqBundle\RabbitMq\Producer;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Doctrine\ORM\EntityManager;
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;
}
/**
{
$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
}
$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 || $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);
}
}
$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])) {
);
}
+ $pocketEntry['url'] = $url;
+ $pocketEntry['userId'] = $this->user->getId();
+
$this->em->persist($entry);
++$this->importedEntries;
if (($i % 20) === 0) {
$this->em->flush();
}
+
++$i;
}
$this->em->flush();
+
+ if ($this->rabbitMQ) {
+ foreach ($entries as $entry) {
+ $this->producer->publish(serialize($entry));
+ }
+ }
}
}