aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/CoreBundle/Controller/EntryController.php
diff options
context:
space:
mode:
authorJeremy Benoist <jeremy.benoist@gmail.com>2016-09-17 07:40:56 +0200
committerJeremy Benoist <jeremy.benoist@gmail.com>2016-09-17 07:40:56 +0200
commit59b97fae996d8307b9d957d210d46200f6d206bf (patch)
tree9481859d10fafed91067ac7736480e91cd6eb4bb /src/Wallabag/CoreBundle/Controller/EntryController.php
parentfbb319f064e6336a3b44bda12cdc51c93c51f379 (diff)
downloadwallabag-59b97fae996d8307b9d957d210d46200f6d206bf.tar.gz
wallabag-59b97fae996d8307b9d957d210d46200f6d206bf.tar.zst
wallabag-59b97fae996d8307b9d957d210d46200f6d206bf.zip
Avoid losing entry when fetching fail
Instead of just say “Failed to save entry” we’ll save the entry at all cost and try to fetch content. If fetching content failed, the entry will still be saved at least, but without content.
Diffstat (limited to 'src/Wallabag/CoreBundle/Controller/EntryController.php')
-rw-r--r--src/Wallabag/CoreBundle/Controller/EntryController.php50
1 files changed, 29 insertions, 21 deletions
diff --git a/src/Wallabag/CoreBundle/Controller/EntryController.php b/src/Wallabag/CoreBundle/Controller/EntryController.php
index 624576b5..40111af0 100644
--- a/src/Wallabag/CoreBundle/Controller/EntryController.php
+++ b/src/Wallabag/CoreBundle/Controller/EntryController.php
@@ -17,26 +17,35 @@ use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;
17class EntryController extends Controller 17class EntryController extends Controller
18{ 18{
19 /** 19 /**
20 * @param Entry $entry 20 * Fetch content and update entry.
21 * In case it fails, entry will return to avod loosing the data.
22 *
23 * @param Entry $entry
24 * @param string $prefixMessage Should be the translation key: entry_saved or entry_reloaded
25 *
26 * @return Entry
21 */ 27 */
22 private function updateEntry(Entry $entry) 28 private function updateEntry(Entry $entry, $prefixMessage = 'entry_saved')
23 { 29 {
30 // put default title in case of fetching content failed
31 $entry->setTitle('No title found');
32
33 $message = 'flashes.entry.notice.'.$prefixMessage;
34
24 try { 35 try {
25 $entry = $this->get('wallabag_core.content_proxy')->updateEntry($entry, $entry->getUrl()); 36 $entry = $this->get('wallabag_core.content_proxy')->updateEntry($entry, $entry->getUrl());
26
27 $em = $this->getDoctrine()->getManager();
28 $em->persist($entry);
29 $em->flush();
30 } catch (\Exception $e) { 37 } catch (\Exception $e) {
31 $this->get('logger')->error('Error while saving an entry', [ 38 $this->get('logger')->error('Error while saving an entry', [
32 'exception' => $e, 39 'exception' => $e,
33 'entry' => $entry, 40 'entry' => $entry,
34 ]); 41 ]);
35 42
36 return false; 43 $message = 'flashes.entry.notice.'.$prefixMessage.'_failed';
37 } 44 }
38 45
39 return true; 46 $this->get('session')->getFlashBag()->add('notice', $message);
47
48 return $entry;
40 } 49 }
41 50
42 /** 51 /**
@@ -66,12 +75,11 @@ class EntryController extends Controller
66 return $this->redirect($this->generateUrl('view', ['id' => $existingEntry->getId()])); 75 return $this->redirect($this->generateUrl('view', ['id' => $existingEntry->getId()]));
67 } 76 }
68 77
69 $message = 'flashes.entry.notice.entry_saved'; 78 $this->updateEntry($entry);
70 if (false === $this->updateEntry($entry)) {
71 $message = 'flashes.entry.notice.entry_saved_failed';
72 }
73 79
74 $this->get('session')->getFlashBag()->add('notice', $message); 80 $em = $this->getDoctrine()->getManager();
81 $em->persist($entry);
82 $em->flush();
75 83
76 return $this->redirect($this->generateUrl('homepage')); 84 return $this->redirect($this->generateUrl('homepage'));
77 } 85 }
@@ -95,6 +103,10 @@ class EntryController extends Controller
95 103
96 if (false === $this->checkIfEntryAlreadyExists($entry)) { 104 if (false === $this->checkIfEntryAlreadyExists($entry)) {
97 $this->updateEntry($entry); 105 $this->updateEntry($entry);
106
107 $em = $this->getDoctrine()->getManager();
108 $em->persist($entry);
109 $em->flush();
98 } 110 }
99 111
100 return $this->redirect($this->generateUrl('homepage')); 112 return $this->redirect($this->generateUrl('homepage'));
@@ -316,15 +328,11 @@ class EntryController extends Controller
316 { 328 {
317 $this->checkUserAction($entry); 329 $this->checkUserAction($entry);
318 330
319 $message = 'flashes.entry.notice.entry_reloaded'; 331 $this->updateEntry($entry, 'entry_reloaded');
320 if (false === $this->updateEntry($entry)) {
321 $message = 'flashes.entry.notice.entry_reload_failed';
322 }
323 332
324 $this->get('session')->getFlashBag()->add( 333 $em = $this->getDoctrine()->getManager();
325 'notice', 334 $em->persist($entry);
326 $message 335 $em->flush();
327 );
328 336
329 return $this->redirect($this->generateUrl('view', ['id' => $entry->getId()])); 337 return $this->redirect($this->generateUrl('view', ['id' => $entry->getId()]));
330 } 338 }