SHARE
TWEET

Untitled

a guest Aug 26th, 2019 83 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. namespace LDMR\FormBundle\Controller;
  4.  
  5. use LDMR\CommonBundle\Controller\AbstractBaseController;
  6. use LDMR\FormBundle\Repository\LuluFormRepository;
  7. use Nelmio\ApiDocBundle\Annotation\ApiDoc;
  8. use \Exception;
  9.  
  10. class LuluFormController extends AbstractBaseController
  11. {
  12.  
  13.     /**
  14.      * {@inheritdoc}
  15.      *
  16.      * @ApiDoc(
  17.      *      section="LuluForm",
  18.      *      description="Get a filled form from unique form id  and lulu id",
  19.      *      statusCodes={
  20.      *          401="Returned when the User is not authenticated",
  21.      *          200="Returned when successful",
  22.      *          404="Returned when the form is not found",
  23.      *      },
  24.      * )
  25.      *
  26.      */
  27.     public function getAction(string $uniqueId, int $id)
  28.     {
  29.         // check logged employee exists
  30.         try {
  31.             $currentUserEmployee = $this->getEmployeeRepository()->getOneByCriteria(['userId' => $this->getUser()->getId()]);
  32.  
  33.             if (!$currentUserEmployee) {
  34.                 throw new Exception(sprintf('Employee %s not found', $this->getUser()->getId()));
  35.             }
  36.         }
  37.         catch(Exception $e) {
  38.             return $this->getClientErrorResponseBuilder()->forbidden();
  39.         }
  40.  
  41.         // check lulu exists
  42.         try {
  43.             $lulu = $this->getLuluRepository()->getOneByCriteria(['id' => $id]);
  44.  
  45.             if (!$lulu) {
  46.                 throw new Exception(sprintf('Lulu %s not found', $this->getUser()->getId()));
  47.             }
  48.         }
  49.         catch(Exception $e) {
  50.             return $this->getClientErrorResponseBuilder()->notFound();
  51.         }
  52.  
  53.         /** @var LuluFormRepository $luluFormRepo **/
  54.         $luluFormRepo = $this->getDoctrine()->getRepository('LDMRFormBundle:LuluForm');
  55.  
  56.         try {
  57.             $luluForm = $luluFormRepo->getFormattedLuluFormAnswers($uniqueId, $id);
  58.  
  59.             if (!$luluForm) {
  60.                 throw new Exception(sprintf('LuluForm %s not found', $uniqueId));
  61.             }
  62.         }
  63.         catch(Exception $e) {
  64.             return $this->getClientErrorResponseBuilder()->notFound();
  65.         }
  66.  
  67.         return $this->getSuccessResponseBuilder()->buildSingleObjectResponse(
  68.             $luluForm,
  69.             [
  70.                 'luluFormForm',
  71.                 'formDownwards',
  72.                 'topicDownwards',
  73.                 'questionLuluAnswers',
  74.             ]
  75.         );
  76.     }
  77.  
  78.     /**
  79.      * {@inheritdoc}
  80.      *
  81.      * @ApiDoc(
  82.      *      section="LuluForm",
  83.      *      description="Get the list of forms for a lulu",
  84.      *      statusCodes={
  85.      *          401="Returned when the User is not authenticated",
  86.      *          200="Returned when successful",
  87.      *          404="Returned when the form is not found"
  88.      *      },
  89.      * )
  90.      *
  91.      */
  92.     public function listAction(int $id)
  93.     {
  94.         // check logged employee exists
  95.         try {
  96.             $currentUserEmployee = $this->getEmployeeRepository()->getOneByCriteria(['userId' => $this->getUser()->getId()]);
  97.  
  98.             if (!$currentUserEmployee) {
  99.                 throw new Exception(sprintf('Employee %s not found', $this->getUser()->getId()));
  100.             }
  101.         }
  102.         catch(Exception $e) {
  103.             return $this->getClientErrorResponseBuilder()->forbidden();
  104.         }
  105.  
  106.         // check lulu exists
  107.         try {
  108.             $lulu = $this->getLuluRepository()->getOneByCriteria(['id' => $id]);
  109.  
  110.             if (!$lulu) {
  111.                 throw new Exception(sprintf('Lulu %s not found', $this->getUser()->getId()));
  112.             }
  113.         }
  114.         catch(Exception $e) {
  115.             return $this->getClientErrorResponseBuilder()->notFound();
  116.         }
  117.  
  118.         try {
  119.             /** @var LuluFormRepository $luluFormRepo **/
  120.             $luluFormRepo = $this->getDoctrine()->getRepository('LDMRFormBundle:LuluForm');
  121.             $forms        = $luluFormRepo->findObjectsBy(
  122.                 ['lulu' => $lulu],
  123.                 ['form', 'followupEmployee']
  124.             );
  125.         }
  126.         catch (Exception $e) {
  127.             $forms = [];
  128.         }
  129.  
  130.         return $this->getSuccessResponseBuilder()->buildPaginationListResponse(
  131.             $forms,
  132.             [
  133.                 'luluFormForm',
  134.                 'luluFormFollowupEmployee',
  135.             ]
  136.         );
  137.     }
  138. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top