src/Controller/Api/AccountCreationController.php line 268

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Api;
  3. use App\Entity\Creation;
  4. use App\Entity\Feature;
  5. use App\Entity\Institution;
  6. use App\Entity\Model;
  7. use App\Entity\ProductFeatureValue;
  8. use App\Entity\ProductMenuMaker;
  9. use App\Entity\ProductPrice;
  10. use App\Entity\User;
  11. use App\Service\CreationService;
  12. use App\Service\InstitutionService;
  13. use App\Service\ProductService;
  14. use App\Service\Tools;
  15. use Doctrine\ORM\EntityManagerInterface;
  16. use Exception;
  17. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  18. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  19. use Symfony\Component\HttpFoundation\JsonResponse;
  20. use Symfony\Component\HttpFoundation\Request;
  21. use Symfony\Component\HttpKernel\Exception\HttpException;
  22. use Symfony\Component\HttpKernel\Exception\NotAcceptableHttpException;
  23. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  24. use Symfony\Component\Routing\Annotation\Route;
  25. use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
  26. use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
  27. use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
  28. use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
  29. /**
  30.  * Class AccountCreationController
  31.  * @package App\Controller\Api
  32.  * @Route(path="/api/account/creation", name="api_account_creation")
  33.  * @IsGranted("IS_AUTHENTICATED_FULLY")
  34.  */
  35. class AccountCreationController extends AbstractController
  36. {
  37.     private EntityManagerInterface $em;
  38.     private CreationService $creationService;
  39.     private InstitutionService $institutionService;
  40.     private ProductService $productService;
  41.     public function __construct(
  42.         EntityManagerInterface $em,
  43.         CreationService $creationService,
  44.         InstitutionService $institutionService,
  45.         ProductService $productService
  46.     ) {
  47.         $this->em $em;
  48.         $this->creationService $creationService;
  49.         $this->institutionService $institutionService;
  50.         $this->productService $productService;
  51.     }
  52.     /**
  53.      * @param Creation $creation
  54.      * @param $institutionId
  55.      * @param Request $request
  56.      * @return JsonResponse
  57.      * @Route("/{creation}/{institutionId}", name="find", requirements={"creation"="\d+"}, defaults={"institutionId"=null})
  58.      */
  59.     public function find(Creation $creation$institutionIdRequest $request): JsonResponse
  60.     {
  61.         /** @var User $user */
  62.         $user $this->getUser();
  63.         /** @var Institution|JsonResponse $institution */
  64.         $institution $this->institutionService->getInstitutionForUser($request$user);
  65.         // Check if a JSON error has been returned
  66.         if ($institution instanceof JsonResponse) {
  67.             return $institution;
  68.         }
  69.         if (!$institution->getCreations()->contains($creation)) {
  70.             throw new NotFoundHttpException("Creation not found");
  71.         }
  72.         $content $this->creationService->getContent($creation);
  73.         return new JsonResponse([
  74.             'success' => true,
  75.             'name' => $creation->getName(),
  76.             'content' => $content,
  77.         ]);
  78.     }
  79.     /**
  80.      * @param Creation $creation
  81.      * @param Request $request
  82.      * @return JsonResponse
  83.      * @Route("/save/{creation}", name="save", requirements={"creation"="\d+"})
  84.      */
  85.     public function save(Creation $creationRequest $request): JsonResponse
  86.     {
  87.         /** @var User $user */
  88.         $user $this->getUser();
  89.         /** @var Institution|JsonResponse $institution */
  90.         $institution $this->institutionService->getInstitutionForUser($request$user);
  91.         // Check if a JSON error has been returned
  92.         if ($institution instanceof JsonResponse) {
  93.             return $institution;
  94.         }
  95.         if (!$institution->getCreations()->contains($creation)) {
  96.             throw new NotFoundHttpException("Creation not found");
  97.         }
  98.         $data Tools::getRequestData($request);
  99.         $content $data['content'];
  100.         if (!$this->creationService->isValidData($content)) {
  101.             throw new NotAcceptableHttpException('Data parameter is invalid');
  102.         }
  103.         $creation->setContent($content);
  104.         if (!is_null($creation->getContent()['model'])) {
  105.             /** @var Model $model */
  106.             $model $this->em->getRepository(Model::class)->find($creation->getContent()['model']['id']);
  107.             $creation->setModel($model);
  108.         } else {
  109.             $creation->setModel(null);
  110.         }
  111.         $this->creationService->saveCreationHistory($creation$content);
  112.         $this->em->persist($creation);
  113.         $this->em->flush();
  114.         return new JsonResponse([
  115.             'success' => true,
  116.         ]);
  117.     }
  118.     /**
  119.      * @param Creation $creation
  120.      * @param Request $request
  121.      * @return JsonResponse
  122.      * @throws Exception
  123.      * @Route("/format/{creation}", name="format", requirements={"creation"="\d+"})
  124.      */
  125.     public function updateFormat(Creation $creationRequest $request): JsonResponse
  126.     {
  127.         /** @var User $user */
  128.         $user $this->getUser();
  129.         /** @var Institution|JsonResponse $institution */
  130.         $institution $this->institutionService->getInstitutionForUser($request$user);
  131.         // Check if a JSON error has been returned
  132.         if ($institution instanceof JsonResponse) {
  133.             return $institution;
  134.         }
  135.         if (!$institution->getCreations()->contains($creation)) {
  136.             throw new NotFoundHttpException("Creation not found");
  137.         }
  138.         $data Tools::getRequestData($request);
  139.         $ref $data['ref'];
  140.         // Modification du format de la création
  141.         /** @var ProductMenuMaker $productMenuMaker */
  142.         $productMenuMaker $this->em->getRepository(ProductMenuMaker::class)->findOneBy([
  143.             'reference' => $ref
  144.         ]);
  145.         foreach ($creation->getProductFeatureValues() as $creationProductFeatureValue) {
  146.             foreach ($productMenuMaker->getProductFeatureValues() as $productMenuMakerProductFeatureValue) {
  147.                 if($creationProductFeatureValue->getProductFeature()->getId() === $productMenuMakerProductFeatureValue->getProductFeature()->getId()) {
  148.                     $creation->removeProductFeatureValue($creationProductFeatureValue);
  149.                     $creation->addProductFeatureValue($productMenuMakerProductFeatureValue);
  150.                 }
  151.             }
  152.         }
  153.         $creation->setMatrix(null);
  154.         $this->em->persist($creation);
  155.         // Modification de productPrice si la création est en panier
  156.         foreach ($creation->getCartElements() as $cartElement) {
  157.             $productFeatureValues = [];
  158.             /** @var ProductFeatureValue $productFeatureValue */
  159.             foreach ($cartElement->getProductPrice()->getProductFeatureValuesSorted() as $productFeatureValue) {
  160.                 $productFeatureValues[$productFeatureValue->getProductFeature()->getId()] = $productFeatureValue->getPosition() . '~' $productFeatureValue->getId();
  161.             }
  162.             foreach ($creation->getProductFeatureValues() as $creationProductFeatureValue) {
  163.                 if(array_key_exists($creationProductFeatureValue->getProductFeature()->getId(), $productFeatureValues)) {
  164.                     $productFeatureValues[$creationProductFeatureValue->getProductFeature()->getId()] = $creationProductFeatureValue->getPosition() . '~' $creationProductFeatureValue->getId();
  165.                 }
  166.             }
  167.             $productPrice $this->productService->getOneProductPriceByFeatureValuesQuantity($productFeatureValues$cartElement->getProduct(), $cartElement->getQuantity());
  168.             /** @var ProductPrice $productPrice */
  169.             $productPrice $this->em->getRepository(ProductPrice::class)->findOneBy([
  170.                 'id' => $productPrice['id']
  171.             ]);
  172.             if (!$productPrice) {
  173.                 $this->em->remove($cartElement);
  174.             } else {
  175.                 $cartElement->setProductPrice($productPrice);
  176.                 $this->em->persist($cartElement);
  177.             }
  178.         }
  179.         $this->em->flush();
  180.         return new JsonResponse(true);
  181.     }
  182.     /**
  183.      * @param Creation $creation
  184.      * @param Request $request
  185.      * @return JsonResponse
  186.      * @Route("/save/{creation}/name", name="save_name", requirements={"creation"="\d+"})
  187.      */
  188.     public function saveName(Creation $creationRequest $request): JsonResponse
  189.     {
  190.         /** @var User $user */
  191.         $user $this->getUser();
  192.         /** @var Institution|JsonResponse $institution */
  193.         $institution $this->institutionService->getInstitutionForUser($request$user);
  194.         // Check if a JSON error has been returned
  195.         if ($institution instanceof JsonResponse) {
  196.             return $institution;
  197.         }
  198.         if (!$institution->getCreations()->contains($creation)) {
  199.             throw new NotFoundHttpException("Creation not found");
  200.         }
  201.         $data Tools::getRequestData($request);
  202.         $name $data['name'];
  203.         $creation->setName($name);
  204.         $this->em->persist($creation);
  205.         $this->em->flush();
  206.         return new JsonResponse([
  207.             'success' => true,
  208.             'name' => $name,
  209.         ]);
  210.     }
  211.     /**
  212.      * @param Creation $creation
  213.      * @param Request $request
  214.      * @return JsonResponse
  215.      * @throws ClientExceptionInterface
  216.      * @throws RedirectionExceptionInterface
  217.      * @throws ServerExceptionInterface
  218.      * @throws TransportExceptionInterface
  219.      * @Route("/save/{creation}/preview", name="save_preview", requirements={"creation"="\d+"})
  220.      */
  221.     public function savePreview(Creation $creationRequest $request): JsonResponse
  222.     {
  223.         /** @var User $user */
  224.         $user $this->getUser();
  225.         /** @var Institution|JsonResponse $institution */
  226.         $institution $this->institutionService->getInstitutionForUser($request$user);
  227.         // Check if a JSON error has been returned
  228.         if ($institution instanceof JsonResponse) {
  229.             return $institution;
  230.         }
  231.         if (!$institution->getCreations()->contains($creation)) {
  232.             throw new NotFoundHttpException("Creation not found");
  233.         }
  234.         $data Tools::getRequestData($request);
  235.         $data $data['data'];
  236.         try {
  237.             $this->creationService->savePreview($creation$data);
  238.         } catch (Exception $e) {
  239.             throw new HttpException($e->getCode(), $e->getMessage());
  240.         }
  241.         return new JsonResponse([
  242.             'success' => true,
  243.         ]);
  244.     }
  245.     /**
  246.      * @param Creation $creation
  247.      * @param Request $request
  248.      * @return JsonResponse
  249.      * @throws ClientExceptionInterface
  250.      * @throws RedirectionExceptionInterface
  251.      * @throws ServerExceptionInterface
  252.      * @throws TransportExceptionInterface
  253.      * @Route("/save/{creation}/documents", name="save_document", requirements={"creation"="\d+"})
  254.      */
  255.     public function saveDocuments(Creation $creationRequest $request): JsonResponse
  256.     {
  257.         /** @var User $user */
  258.         $user $this->getUser();
  259.         /** @var Institution|JsonResponse $institution */
  260.         $institution $this->institutionService->getInstitutionForUser($request$user);
  261.         // Check if a JSON error has been returned
  262.         if ($institution instanceof JsonResponse) {
  263.             return $institution;
  264.         }
  265.         if (!$institution->getCreations()->contains($creation)) {
  266.             throw new NotFoundHttpException("Creation not found");
  267.         }
  268.         $data Tools::getRequestData($request);
  269.         $nbDocuments $institution->getPdfmenuDocuments()->count();
  270.         try {
  271.             foreach ($data['documents'] as $document) {
  272.                 if (isset($document)) {
  273.                     $document['position'] += $nbDocuments;
  274.                     $this->creationService->saveDocument($creation$document);
  275.                 }
  276.             }
  277.         } catch (Exception $e) {
  278.             throw new HttpException($e->getCode(), $e->getMessage());
  279.         }
  280.         return new JsonResponse([
  281.             'success' => true,
  282.         ]);
  283.     }
  284. }