]> git.immae.eu Git - github/wallabag/wallabag.git/blobdiff - src/Wallabag/CoreBundle/Controller/EntryController.php
Convert array + phpDoc
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Controller / EntryController.php
index fa6330314209c36872e1b1b9d2100ea1f05d074c..17b72bd1c56e632c8d5c4bc7bfdfd4a88f0064c0 100644 (file)
@@ -49,16 +49,15 @@ class EntryController extends Controller
         $form->handleRequest($request);
 
         if ($form->isValid()) {
-            // check for existing entry, if it exists, redirect to it with a message
-            $existingEntry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId($entry->getUrl(), $this->getUser()->getId());
+            $existingEntry = $this->checkIfEntryAlreadyExists($entry);
 
             if (false !== $existingEntry) {
                 $this->get('session')->getFlashBag()->add(
                     'notice',
-                    $this->get('translator')->trans('flashes.entry.notice.entry_already_saved', array('%date%' => $existingEntry->getCreatedAt()->format('d-m-Y')))
+                    $this->get('translator')->trans('flashes.entry.notice.entry_already_saved', ['%date%' => $existingEntry->getCreatedAt()->format('d-m-Y')])
                 );
 
-                return $this->redirect($this->generateUrl('view', array('id' => $existingEntry->getId())));
+                return $this->redirect($this->generateUrl('view', ['id' => $existingEntry->getId()]));
             }
 
             $this->updateEntry($entry);
@@ -70,9 +69,9 @@ class EntryController extends Controller
             return $this->redirect($this->generateUrl('homepage'));
         }
 
-        return $this->render('WallabagCoreBundle:Entry:new_form.html.twig', array(
+        return $this->render('WallabagCoreBundle:Entry:new_form.html.twig', [
             'form' => $form->createView(),
-        ));
+        ]);
     }
 
     /**
@@ -86,7 +85,10 @@ class EntryController extends Controller
     {
         $entry = new Entry($this->getUser());
         $entry->setUrl($request->get('url'));
-        $this->updateEntry($entry);
+
+        if (false === $this->checkIfEntryAlreadyExists($entry)) {
+            $this->updateEntry($entry);
+        }
 
         return $this->redirect($this->generateUrl('homepage'));
     }
@@ -129,12 +131,12 @@ class EntryController extends Controller
                 'flashes.entry.notice.entry_updated'
             );
 
-            return $this->redirect($this->generateUrl('view', array('id' => $entry->getId())));
+            return $this->redirect($this->generateUrl('view', ['id' => $entry->getId()]));
         }
 
-        return $this->render('WallabagCoreBundle:Entry:edit.html.twig', array(
+        return $this->render('WallabagCoreBundle:Entry:edit.html.twig', [
             'form' => $form->createView(),
-        ));
+        ]);
     }
 
     /**
@@ -255,17 +257,17 @@ class EntryController extends Controller
             $entries->setCurrentPage($page);
         } catch (OutOfRangeCurrentPageException $e) {
             if ($page > 1) {
-                return $this->redirect($this->generateUrl($type, array('page' => $entries->getNbPages())), 302);
+                return $this->redirect($this->generateUrl($type, ['page' => $entries->getNbPages()]), 302);
             }
         }
 
         return $this->render(
             'WallabagCoreBundle:Entry:entries.html.twig',
-            array(
+            [
                 'form' => $form->createView(),
                 'entries' => $entries,
                 'currentPage' => $page,
-            )
+            ]
         );
     }
 
@@ -284,7 +286,7 @@ class EntryController extends Controller
 
         return $this->render(
             'WallabagCoreBundle:Entry:entry.html.twig',
-            array('entry' => $entry)
+            ['entry' => $entry]
         );
     }
 
@@ -312,7 +314,7 @@ class EntryController extends Controller
             $message
         );
 
-        return $this->redirect($this->generateUrl('view', array('id' => $entry->getId())));
+        return $this->redirect($this->generateUrl('view', ['id' => $entry->getId()]));
     }
 
     /**
@@ -392,7 +394,7 @@ class EntryController extends Controller
         // to avoid redirecting to the deleted entry. Ugh.
         $url = $this->generateUrl(
             'view',
-            array('id' => $entry->getId()),
+            ['id' => $entry->getId()],
             UrlGeneratorInterface::ABSOLUTE_URL
         );
 
@@ -420,4 +422,16 @@ class EntryController extends Controller
             throw $this->createAccessDeniedException('You can not access this entry.');
         }
     }
+
+    /**
+     * Check for existing entry, if it exists, redirect to it with a message.
+     *
+     * @param Entry $entry
+     *
+     * @return Entry|bool
+     */
+    private function checkIfEntryAlreadyExists(Entry $entry)
+    {
+        return $this->get('wallabag_core.entry_repository')->findByUrlAndUserId($entry->getUrl(), $this->getUser()->getId());
+    }
 }