]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/ImportBundle/Import/ReadabilityImport.php
Some cleanup & refactor
[github/wallabag/wallabag.git] / src / Wallabag / ImportBundle / Import / ReadabilityImport.php
1 <?php
2
3 namespace Wallabag\ImportBundle\Import;
4
5 use Wallabag\CoreBundle\Entity\Entry;
6
7 class ReadabilityImport extends AbstractImport
8 {
9 private $skippedEntries = 0;
10 private $importedEntries = 0;
11 private $filepath;
12
13 /**
14 * {@inheritdoc}
15 */
16 public function getName()
17 {
18 return 'Readability';
19 }
20
21 /**
22 * {@inheritdoc}
23 */
24 public function getUrl()
25 {
26 return 'import_readability';
27 }
28
29 /**
30 * {@inheritdoc}
31 */
32 public function getDescription()
33 {
34 return 'import.readability.description';
35 }
36
37 /**
38 * Set file path to the json file.
39 *
40 * @param string $filepath
41 */
42 public function setFilepath($filepath)
43 {
44 $this->filepath = $filepath;
45
46 return $this;
47 }
48
49 /**
50 * {@inheritdoc}
51 */
52 public function getSummary()
53 {
54 return [
55 'skipped' => $this->skippedEntries,
56 'imported' => $this->importedEntries,
57 ];
58 }
59
60 /**
61 * {@inheritdoc}
62 */
63 public function import()
64 {
65 if (!$this->user) {
66 $this->logger->error('ReadabilityImport: user is not defined');
67
68 return false;
69 }
70
71 if (!file_exists($this->filepath) || !is_readable($this->filepath)) {
72 $this->logger->error('ReadabilityImport: unable to read file', ['filepath' => $this->filepath]);
73
74 return false;
75 }
76
77 $data = json_decode(file_get_contents($this->filepath), true);
78
79 if (empty($data) || empty($data['bookmarks'])) {
80 return false;
81 }
82
83 if ($this->producer) {
84 $this->parseEntriesForProducer($data['bookmarks']);
85
86 return true;
87 }
88
89 $this->parseEntries($data['bookmarks']);
90
91 return true;
92 }
93
94 public function parseEntry(array $importedEntry)
95 {
96 $existingEntry = $this->em
97 ->getRepository('WallabagCoreBundle:Entry')
98 ->findByUrlAndUserId($importedEntry['article__url'], $this->user->getId());
99
100 if (false !== $existingEntry) {
101 ++$this->skippedEntries;
102
103 return;
104 }
105
106 $data = [
107 'title' => $importedEntry['article__title'],
108 'url' => $importedEntry['article__url'],
109 'content_type' => '',
110 'language' => '',
111 'is_archived' => $importedEntry['archive'] || $this->markAsRead,
112 'is_starred' => $importedEntry['favorite'],
113 ];
114
115 $entry = $this->fetchContent(
116 new Entry($this->user),
117 $data['url'],
118 $data
119 );
120
121 // jump to next entry in case of problem while getting content
122 if (false === $entry) {
123 ++$this->skippedEntries;
124
125 return;
126 }
127
128 $entry->setArchived($data['is_archived']);
129 $entry->setStarred($data['is_starred']);
130
131 $this->em->persist($entry);
132 ++$this->importedEntries;
133
134 return $entry;
135 }
136
137 /**
138 * {@inheritdoc}
139 */
140 protected function setEntryAsRead(array $importedEntry)
141 {
142 $importedEntry['archive'] = 1;
143
144 return $importedEntry;
145 }
146 }