Fix error reporting.
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Thu, 28 Jun 2018 09:14:10 +0000 (11:14 +0200)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Thu, 28 Jun 2018 09:14:35 +0000 (11:14 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
src/Controller/PersonController.php

index f2e73c132454b5e4d3bf98fc763ab7f121b7fe0f..0fb898527414e14ff7c940e3744cd8ea7f2c1edb 100644 (file)
@@ -29,6 +29,7 @@ class PersonController extends Controller
         $em->persist($person);
         $em->flush();
 
+        //TODO: use ViewHandler
         return $this->view($person, Response::HTTP_CREATED, ['Location' => $this->generateUrl('show_person', ['id' => $person->getId(), UrlGeneratorInterface::ABSOLUTE_URL])]);
     }
 
@@ -60,8 +61,10 @@ class PersonController extends Controller
         $em = $this->getDoctrine()->getManager();
         $person = $em->getRepository('App:Person')->find($request->get('id'));
 
+        $viewHandler = $this->get('fos_rest.view_handler');
+
         if (empty($person)) {
-            return View::create(['message' => 'Person not found'], Response::HTTP_NOT_FOUND);
+            return $this->PersonNotFound($viewHandler);
         }
 
         $person->setFirstName($request->get('firstname'));
@@ -71,6 +74,7 @@ class PersonController extends Controller
         $em->merge($person);
         $em->flush();
 
+        //TODO: use ViewHandler
         return $this->view($person, Response::HTTP_CREATED, ['Location' => $this->generateUrl('show_person', ['id' => $person->getId(), UrlGeneratorInterface::ABSOLUTE_URL])]);
     }
 
@@ -90,14 +94,15 @@ class PersonController extends Controller
         $em = $this->getDoctrine()->getManager();
         $localisations = $em->getRepository('App:Localisation')->findBy(['person' => $request->get('id')]);
 
+        $viewHandler = $this->get('fos_rest.view_handler');
+
         if (empty($localisations)) {
-            return View::create(['message' => 'Person localisations not found'], Response::HTTP_NOT_FOUND);
+            return $this->PersonLocalisationsNotFound($viewHandler);
         }
 
         $view = View::create($localisations);
         $view->setFormat('json');
 
-        $viewHandler = $this->get('fos_rest.view_handler');
         return $viewHandler->handle($view);
 
     }
@@ -133,14 +138,15 @@ class PersonController extends Controller
         $em = $this->getDoctrine()->getManager();
         $person = $em->getRepository('App:Person')->find($request->get('id'));
 
+        $viewHandler = $this->get('fos_rest.view_handler');
+
         if (empty($person)) {
-            return View::create(['message' => 'Person not found'], Response::HTTP_NOT_FOUND);
+            return $this->PersonNotFound($viewHandler);
         }
 
         $view = View::create($person);
         $view->setFormat('json');
 
-        $viewHandler = $this->get('fos_rest.view_handler');
         return $viewHandler->handle($view);
    }
 
@@ -155,16 +161,17 @@ class PersonController extends Controller
   public function showPersonByEmail(Request $request)
   {
         $em = $this->getDoctrine()->getManager();
-        $person = $em->getRepository('App:Person')->find($request->get('email'));
+        $person = $em->getRepository('App:Person')->findOneBy(['email' => $request->get('email')]);
+
+        $viewHandler = $this->get('fos_rest.view_handler');
 
         if (empty($person)) {
-            return View::create(['message' => 'Person not found'], Response::HTTP_NOT_FOUND);
+            return $this->PersonNotFound($viewHandler);
         }
 
         $view = View::create($person);
         $view->setFormat('json');
 
-        $viewHandler = $this->get('fos_rest.view_handler');
         return $viewHandler->handle($view);
   }
 
@@ -181,14 +188,15 @@ class PersonController extends Controller
         $em = $this->getDoctrine()->getManager();
         $person = $em->getRepository('App:Person')->find($request->get('id'));
 
+        $viewHandler = $this->get('fos_rest.view_handler');
+
         if (empty($person)) {
-            return View::create(['message' => 'Person not found'], Response::HTTP_NOT_FOUND);
+            return $this->PersonNotFound($viewHandler);
         }
 
         $view = View::create($person->getFriends());
         $view->setFormat('json');
 
-        $viewHandler = $this->get('fos_rest.view_handler');
         return $viewHandler->handle($view);
   }
 
@@ -203,16 +211,26 @@ class PersonController extends Controller
  public function showPersonFriendsByEmail(Request $request)
  {
         $em = $this->getDoctrine()->getManager();
-        $person = $em->getRepository('App:Person')->find($request->get('email'));
+        $person = $em->getRepository('App:Person')->findOneBy(['email' => $request->get('email')]);
+
+        $viewHandler = $this->get('fos_rest.view_handler');
 
         if (empty($person)) {
-            return View::create(['message' => 'Person not found'], Response::HTTP_NOT_FOUND);
+            return $this->PersonNotFound($viewHandler);
         }
 
         $view = View::create($person->getFriends());
         $view->setFormat('json');
 
-        $viewHandler = $this->get('fos_rest.view_handler');
         return $viewHandler->handle($view);
  }
+
+ private function PersonNotFound($viewHandler) {
+     return $viewHandler->handle(View::create(['message' => 'Person not found'], Response::HTTP_NOT_FOUND));
+ }
+
+ private function PersonLocalisationsNotFound($viewHandler) {
+     return $viewHandler->handle(View::create(['message' => 'Person localisations not found'], Response::HTTP_NOT_FOUND));
+ }
+
 }