]> 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 1a0b80ac589fd8eed9b3c363274640d7c5316171..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,19 +85,20 @@ 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'));
     }
 
     /**
-     * @param Request $request
-     *
      * @Route("/new", name="new")
      *
      * @return \Symfony\Component\HttpFoundation\Response
      */
-    public function addEntryAction(Request $request)
+    public function addEntryAction()
     {
         return $this->render('WallabagCoreBundle:Entry:new.html.twig');
     }
@@ -131,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(),
-        ));
+        ]);
     }
 
     /**
@@ -257,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,
-            )
+            ]
         );
     }
 
@@ -286,7 +286,7 @@ class EntryController extends Controller
 
         return $this->render(
             'WallabagCoreBundle:Entry:entry.html.twig',
-            array('entry' => $entry)
+            ['entry' => $entry]
         );
     }
 
@@ -314,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()]));
     }
 
     /**
@@ -394,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
         );
 
@@ -422,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());
+    }
 }