]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/ImportBundle/Import/PocketImport.php
service call
[github/wallabag/wallabag.git] / src / Wallabag / ImportBundle / Import / PocketImport.php
CommitLineData
ff7b031d
NL
1<?php
2
3namespace Wallabag\ImportBundle\Import;
4
5use Doctrine\ORM\EntityManager;
6use GuzzleHttp\Client;
7use Symfony\Component\HttpFoundation\Session\Session;
8use Wallabag\CoreBundle\Entity\Entry;
9use Wallabag\CoreBundle\Tools\Utils;
10
11class PocketImport implements ImportInterface
12{
13 private $user;
14 private $session;
15 private $em;
16 private $consumerKey;
17
18 public function __construct($tokenStorage, Session $session, EntityManager $em, $consumerKey)
19 {
20 $this->user = $tokenStorage->getToken()->getUser();
21 $this->session = $session;
22 $this->em = $em;
23 $this->consumerKey = $consumerKey;
24 }
25
26 /**
27 * Create a new Client.
28 *
29 * @return Client
30 */
31 private function createClient()
32 {
33 return new Client([
34 'defaults' => [
35 'headers' => [
36 'content-type' => 'application/json',
37 'X-Accept' => 'application/json',
38 ],
39 ],
40 ]);
41 }
42
43 /**
44 * @param $entries
45 */
46 private function parsePocketEntries($entries)
47 {
48 foreach ($entries as $entry) {
49 $newEntry = new Entry($this->user);
50 $newEntry->setUrl($entry['given_url']);
51 $newEntry->setTitle(isset($entry['resolved_title']) ? $entry['resolved_title'] : (isset($entry['given_title']) ? $entry['given_title'] : 'Untitled'));
52
53 if (isset($entry['excerpt'])) {
54 $newEntry->setContent($entry['excerpt']);
55 }
56
57 if (isset($entry['has_image']) && $entry['has_image'] > 0) {
58 $newEntry->setPreviewPicture($entry['image']['src']);
59 }
60
61 if (isset($entry['word_count'])) {
62 $newEntry->setReadingTime(Utils::convertWordsToMinutes($entry['word_count']));
63 }
64
65 $this->em->persist($newEntry);
66 }
67
68 $this->em->flush();
69 }
70
71 public function oAuthRequest($redirectUri, $callbackUri)
72 {
73 $client = $this->createClient();
74 $request = $client->createRequest('POST', 'https://getpocket.com/v3/oauth/request',
75 [
76 'body' => json_encode([
77 'consumer_key' => $this->consumerKey,
78 'redirect_uri' => $redirectUri,
79 ]),
80 ]
81 );
82
83 $response = $client->send($request);
84 $values = $response->json();
85
86 // store code in session for callback method
87 $this->session->set('pocketCode', $values['code']);
88
89 return 'https://getpocket.com/auth/authorize?request_token='.$values['code'].'&redirect_uri='.$callbackUri;
90 }
91
92 public function oAuthAuthorize()
93 {
94 $client = $this->createClient();
95
96 $request = $client->createRequest('POST', 'https://getpocket.com/v3/oauth/authorize',
97 [
98 'body' => json_encode([
99 'consumer_key' => $this->consumerKey,
100 'code' => $this->session->get('pocketCode'),
101 ]),
102 ]
103 );
104
105 $response = $client->send($request);
106
107 return $response->json()['access_token'];
108 }
109
110 public function import($accessToken)
111 {
112 $client = $this->createClient();
113
114 $request = $client->createRequest('POST', 'https://getpocket.com/v3/get',
115 [
116 'body' => json_encode([
117 'consumer_key' => $this->consumerKey,
118 'access_token' => $accessToken,
119 'detailType' => 'complete',
120 ]),
121 ]
122 );
123
124 $response = $client->send($request);
125 $entries = $response->json();
126
127 $this->parsePocketEntries($entries['list']);
128
129 $this->session->getFlashBag()->add(
130 'notice',
131 count($entries['list']).' entries imported'
132 );
133 }
134}