From: Jérôme Benoit Date: Thu, 28 Jun 2018 09:14:10 +0000 (+0200) Subject: Fix error reporting. X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=b607789316c3ff7c7e01b9ad4ade2bc72ff61780;p=Project_proches_de_moi-server.git Fix error reporting. Signed-off-by: Jérôme Benoit --- diff --git a/src/Controller/PersonController.php b/src/Controller/PersonController.php index f2e73c1..0fb8985 100644 --- a/src/Controller/PersonController.php +++ b/src/Controller/PersonController.php @@ -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)); + } + }