ONURESTController.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. namespace FTTHBundle\Controller;
  3. use FTTHBundle\Entity\ONU;
  4. use FTTHBundle\Form\ONUType;
  5. use FOS\RestBundle\Controller\Annotations\RouteResource;
  6. use FOS\RestBundle\Controller\Annotations\View;
  7. use FOS\RestBundle\Util\Codes;
  8. use FOS\RestBundle\View\View as FOSView;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use WebserviceBundle\Controller\RESTController;
  12. use FOS\RestBundle\Controller\Annotations\Patch;
  13. /**
  14. * ONU controller.
  15. * @RouteResource("ONU")
  16. */
  17. class ONURESTController extends RESTController
  18. {
  19. /**
  20. * @return string Retorna el nombre de la Entity de trabajo.
  21. */
  22. public function getRepository()
  23. {
  24. return 'FTTHBundle:ONU';
  25. }
  26. /**
  27. * @return string Retorna el tipo de la clase.
  28. */
  29. public function getFormEntityType()
  30. {
  31. return get_class(new ONUType());
  32. }
  33. // /**
  34. // * Create a ONU entity.
  35. // *
  36. // * @View(statusCode=201, serializerEnableMaxDepthChecks=true)
  37. // *
  38. // * @param Request $request
  39. // *
  40. // * @return Response
  41. // *
  42. // */
  43. // public function postAction(Request $request)
  44. // {
  45. // $entity = new ONU();
  46. // $form = $this->createForm(get_class(new ONUType()), $entity, array("method" => $request->getMethod()));
  47. // $this->removeExtraFields($request, $form);
  48. // $form->handleRequest($request);
  49. //
  50. // if ($form->isValid()) {
  51. // $em = $this->getDoctrine()->getManager();
  52. // $em->persist($entity);
  53. // $em->flush();
  54. //
  55. // return $entity;
  56. // }
  57. //
  58. // return FOSView::create(array('errors' => $form->getErrors()), Codes::HTTP_INTERNAL_SERVER_ERROR);
  59. // }
  60. //
  61. // /**
  62. // * Update a ONU entity.
  63. // *
  64. // * @View(serializerEnableMaxDepthChecks=true)
  65. // *
  66. // * @param Request $request
  67. // * @param $entity
  68. // *
  69. // * @return Response
  70. // */
  71. // public function putAction(Request $request, ONU $entity)
  72. // {
  73. // try {
  74. // $em = $this->getDoctrine()->getManager();
  75. // $request->setMethod('PATCH'); //Treat all PUTs as PATCH
  76. // $form = $this->createForm(get_class(new ONUType()), $entity, array("method" => $request->getMethod()));
  77. // $this->removeExtraFields($request, $form);
  78. // $form->handleRequest($request);
  79. // if ($form->isValid()) {
  80. // $em->flush();
  81. //
  82. // return $entity;
  83. // }
  84. //
  85. // return FOSView::create(array('errors' => $form->getErrors()), Codes::HTTP_INTERNAL_SERVER_ERROR);
  86. // } catch (\Exception $e) {
  87. // return FOSView::create($e->getMessage(), Codes::HTTP_INTERNAL_SERVER_ERROR);
  88. // }
  89. // }
  90. //
  91. // /**
  92. // * Partial Update to a ONU entity.
  93. // *
  94. // * @View(serializerEnableMaxDepthChecks=true)
  95. // *
  96. // * @param Request $request
  97. // * @param $entity
  98. // *
  99. // * @return Response
  100. // */
  101. // public function patchAction(Request $request, ONU $entity)
  102. // {
  103. // return $this->putAction($request, $entity);
  104. // }
  105. //
  106. // /**
  107. // * Delete a ONU entity.
  108. // *
  109. // * @View(statusCode=204)
  110. // *
  111. // * @param Request $request
  112. // * @param $entity
  113. // *
  114. // * @return Response
  115. // */
  116. // public function deleteAction(Request $request, ONU $entity)
  117. // {
  118. // try {
  119. // $em = $this->getDoctrine()->getManager();
  120. // $em->remove($entity);
  121. // $em->flush();
  122. //
  123. // return null;
  124. // } catch (\Exception $e) {
  125. // return FOSView::create($e->getMessage(), Codes::HTTP_INTERNAL_SERVER_ERROR);
  126. // }
  127. // }
  128. /**
  129. * PATCH Route annotation.
  130. * @Patch("/onus/apply/{id}/{workflow}/{transition}")
  131. * @View(statusCode=201, serializerEnableMaxDepthChecks=true)
  132. *
  133. * @param Request $request
  134. * @param $entity
  135. * @param $transition
  136. * @param $workflow
  137. *
  138. * @return Response
  139. */
  140. public function applyAction(Request $request, ONU $entity, String $workflow, String $transition)
  141. {
  142. try {
  143. $wr = $this->container->get("workflow.registry");
  144. $wf = $wr->get($entity, $workflow);
  145. $newState = $wf->apply($entity, $transition);
  146. $em = $this->container->get("doctrine.orm.entity_manager");
  147. $validator = $this->container->get('validator');
  148. $errors = $validator->validate($entity);
  149. if (count($errors) > 0) {
  150. $errorsString = (string) $errors;
  151. return FOSView::create($errorsString, Codes::HTTP_INTERNAL_SERVER_ERROR);
  152. }else{
  153. $entity->setAdministrativeState("suspend");
  154. $em->persist($entity);
  155. $em->flush($entity);
  156. return $entity;
  157. }
  158. } catch (\Exception $e) {
  159. return FOSView::create($e->getMessage(), Codes::HTTP_INTERNAL_SERVER_ERROR);
  160. }
  161. }
  162. }