Simplify the code.
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Thu, 28 Jun 2018 10:21:33 +0000 (12:21 +0200)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Thu, 28 Jun 2018 10:21:33 +0000 (12:21 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
src/Controller/PersonController.php

index 102b6980245824c6ca33201fe66ae29d641a3573..d4bd7ff374204dc15b14c68f3ec21e7f6d096bd3 100644 (file)
@@ -16,7 +16,7 @@ class PersonController extends Controller
 {
     /**
      * @Rest\Post(
-     *     path = "/api/person/inscription",
+     *     path = "/person/inscription",
      *     name = "create_person"
      * )
      * @Rest\View(StatusCode = Response::HTTP_CREATED)
@@ -29,7 +29,6 @@ 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])]);
     }
 
@@ -61,10 +60,8 @@ 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 $this->PersonNotFound($viewHandler);
+            return $this->PersonNotFound();
         }
 
         $person->setFirstName($request->get('firstname'));
@@ -74,7 +71,6 @@ 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])]);
     }
 
@@ -94,17 +90,11 @@ 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 $this->PersonLocalisationsNotFound($viewHandler);
+            return $this->PersonLocalisationsNotFound();
         }
 
-        $view = View::create($localisations);
-        $view->setFormat('json');
-
-        return $viewHandler->handle($view);
-
+        return $localisations;
     }
 
     /**
@@ -138,16 +128,11 @@ 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 $this->PersonNotFound($viewHandler);
+            return $this->PersonNotFound();
         }
 
-        $view = View::create($person);
-        $view->setFormat('json');
-
-        return $viewHandler->handle($view);
+        return $person;
    }
 
    /**
@@ -163,16 +148,11 @@ class PersonController extends Controller
         $em = $this->getDoctrine()->getManager();
         $person = $em->getRepository('App:Person')->findOneBy(['email' => $request->get('email')]);
 
-        $viewHandler = $this->get('fos_rest.view_handler');
-
         if (empty($person)) {
-            return $this->PersonNotFound($viewHandler);
+            return $this->PersonNotFound();
         }
 
-        $view = View::create($person);
-        $view->setFormat('json');
-
-        return $viewHandler->handle($view);
+        return $person;
   }
 
    /**
@@ -188,16 +168,11 @@ 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 $this->PersonNotFound($viewHandler);
+            return $this->PersonNotFound();
         }
 
-        $view = View::create($person->getFriends());
-        $view->setFormat('json');
-
-        return $viewHandler->handle($view);
+        return $person->getFriends();
   }
 
   /**
@@ -212,16 +187,10 @@ class PersonController extends Controller
        $em = $this->getDoctrine()->getManager();
        $persons = $em->getRepository('App:Person')->findAll();
 
-       $viewHandler = $this->get('fos_rest.view_handler');
-
        if (empty($persons)) {
-           return $this->PersonNotFound($viewHandler);
+           return $this->PersonNotFound();
        }
 
-       // $view = View::create($persons);
-       // $view->setFormat('json');
-       //
-       // return $viewHandler->handle($view);
        return $persons;
  }
 
@@ -238,24 +207,19 @@ class PersonController extends Controller
         $em = $this->getDoctrine()->getManager();
         $person = $em->getRepository('App:Person')->findOneBy(['email' => $request->get('email')]);
 
-        $viewHandler = $this->get('fos_rest.view_handler');
-
         if (empty($person)) {
-            return $this->PersonNotFound($viewHandler);
+            return $this->PersonNotFound();
         }
 
-        $view = View::create($person->getFriends());
-        $view->setFormat('json');
-
-        return $viewHandler->handle($view);
+        return $person->getFriends();
  }
 
- private function PersonNotFound($viewHandler) {
-     return $viewHandler->handle(View::create(['message' => 'Person not found'], Response::HTTP_NOT_FOUND));
+ private function PersonNotFound() {
+     return 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));
+ private function PersonLocalisationsNotFound() {
+     return View::create(['message' => 'Person localisations not found'], Response::HTTP_NOT_FOUND);
  }
 
 }