src/Controller/Api/ProductController.php line 362

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Api;
  3. use App\Entity\Attribut;
  4. use App\Entity\AttributValue;
  5. use App\Entity\Category;
  6. use App\Entity\Creation;
  7. use App\Entity\FeatureFamily;
  8. use App\Entity\Folder;
  9. use App\Entity\Matrix;
  10. use App\Entity\Model;
  11. use App\Entity\ModelCategory;
  12. use App\Entity\Product;
  13. use App\Entity\ProductFeatureValue;
  14. use App\Entity\ProductMenuMaker;
  15. use App\Entity\ProductPrice;
  16. use App\Entity\ProductPriceMockup;
  17. use App\Service\ProductService;
  18. use App\Service\Rsa;
  19. use App\Service\SerializeService;
  20. use App\Service\Tools;
  21. use App\Service\TranslationService;
  22. use App\Repository\MatrixRepository;
  23. use App\Repository\ModelCategoryRepository;
  24. use Doctrine\Common\Collections\ArrayCollection;
  25. use Doctrine\ORM\EntityManagerInterface;
  26. use Exception;
  27. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  28. use Symfony\Component\HttpFoundation\JsonResponse;
  29. use Symfony\Component\HttpFoundation\Request;
  30. use Symfony\Component\Routing\Annotation\Route;
  31. use Symfony\Contracts\Translation\TranslatorInterface;
  32. /**
  33.  * Class AppController
  34.  * @package App\Controller\Api
  35.  * @Route(path="/api/product", name="api_product")
  36.  */
  37. class ProductController extends AbstractController
  38. {
  39.     private EntityManagerInterface $em;
  40.     private SerializeService $serializeService;
  41.     private TranslationService $translationService;
  42.     private ProductService $productService;
  43.     private Rsa $rsa;
  44.     private TranslatorInterface $translator;
  45.     public function __construct(
  46.         EntityManagerInterface $em,
  47.         SerializeService $serializeService,
  48.         TranslationService $translationService,
  49.         ProductService $productService,
  50.         Rsa $rsa,
  51.         TranslatorInterface $translator
  52.     ) {
  53.         $this->em $em;
  54.         $this->serializeService $serializeService;
  55.         $this->translationService $translationService;
  56.         $this->productService $productService;
  57.         $this->rsa $rsa;
  58.         $this->translator $translator;
  59.     }
  60.     /**
  61.      * @param Product $product
  62.      * @param Request $request
  63.      * @return JsonResponse
  64.      * @throws Exception
  65.      * @Route("/{product}", name="", requirements={"product"="\d+"})
  66.      */
  67.     public function find(Product $productRequest $request): JsonResponse
  68.     {
  69.         if (!$this->rsa->isValidToken($request->request->get('apiKey'))) {
  70.             return new JsonResponse([
  71.                 'success' => false,
  72.                 'message' => $this->translator->trans('global.invalidToken'),
  73.             ]);
  74.         }
  75.         if ($product->isCommandable()) {
  76.             return new JsonResponse([
  77.                 'success' => true,
  78.                 'product' => $this->serializeService->serializeProduct($product),
  79.             ]);
  80.         }
  81.         return new JsonResponse([
  82.             'success' => false,
  83.         ]);
  84.     }
  85.     /**
  86.      * @param Request $request
  87.      * @return JsonResponse
  88.      * @throws Exception
  89.      * @Route("/find-by-const-name", name="_findbyconstname")
  90.      */
  91.     public function findByConstName(Request $request): JsonResponse
  92.     {
  93.         if (!$this->rsa->isValidToken($request->request->get('apiKey'))) {
  94.             return new JsonResponse([
  95.                 'success' => false,
  96.                 'message' => $this->translator->trans('global.invalidToken'),
  97.             ]);
  98.         }
  99.         $data Tools::getRequestData($request);
  100.         /** @var Product $product */
  101.         $product $this->em->getRepository(Product::class)->findBy(array('constName' => $data['productConstName']));
  102.         if (!is_null($product)) {
  103.             if ($product[0]->isCommandable()) {
  104.                 return new JsonResponse([
  105.                     'success' => true,
  106.                     'product' => $this->serializeService->serializeProduct($product[0]),
  107.                 ]);
  108.             }
  109.         }
  110.         return new JsonResponse([
  111.             'success' => false,
  112.         ]);
  113.     }
  114.     /**
  115.      * @param Request $request
  116.      * @return JsonResponse
  117.      * @Route("/category", name="_category")
  118.      * @throws Exception
  119.      */
  120.     public function findProductCategories(Request $request): JsonResponse
  121.     {
  122.         if (!$this->rsa->isValidToken($request->request->get('apiKey'))) {
  123.             return new JsonResponse([
  124.                 'success' => false,
  125.                 'message' => $this->translator->trans('global.invalidToken'),
  126.             ]);
  127.         }
  128.         /** @var Category[] $productCategories */
  129.         $productCategories $this->em->getRepository(Category::class)->findBy([], ['position' => 'ASC']);
  130.         $data = [];
  131.         foreach ($productCategories as $productCategory) {
  132.             if ($productCategory->isActive()) {
  133.                 $products = [];
  134.                 /** @var Product $product */
  135.                 foreach ($productCategory->getProducts() as $product) {
  136.                     if ($product->isCommandable()) {
  137.                         $products[] = $this->serializeService->serializeProduct($product);
  138.                     }
  139.                 }
  140.                 if (count($products) > 0) {
  141.                     $data[] = [
  142.                         'id' => $productCategory->getId(),
  143.                         'name' => $this->translationService->translate($productCategory->getCategoryTranslations())->getName(),
  144.                         'position' => $productCategory->getId(),
  145.                         'active' => $productCategory->getId(),
  146.                         'products' => $products,
  147.                     ];
  148.                 }
  149.             }
  150.         }
  151.         return new JsonResponse([
  152.             'success' => true,
  153.             'productCategories' => $data,
  154.         ]);
  155.     }
  156.     /**
  157.      * @param Product $product
  158.      * @param Request $request
  159.      * @return JsonResponse
  160.      * @Route("/{product}/model-category", name="_model_category", requirements={"product"="\d+"})
  161.      */
  162.     public function findModelCategories(Product $productRequest $request): JsonResponse
  163.     {
  164.         if ($request->headers->get('Authorization')) {
  165.             $apiKey substr($request->headers->get('Authorization'), 6);
  166.         } else {
  167.             $apiKey $request->request->get('apiKey');
  168.         }
  169.         if (!$this->rsa->isValidToken($apiKey)) {
  170.             return new JsonResponse([
  171.                 'success' => false,
  172.                 'message' => $this->translator->trans('global.invalidToken'),
  173.             ]);
  174.         }
  175.         $data Tools::getRequestData($request);
  176.         $productMenuMaker null;
  177.         $matrixes = [];
  178.         if (array_key_exists('currentFeatureValueIds'$data)) {
  179.             $productFeatureValues = new ArrayCollection();
  180.             foreach ($data['currentFeatureValueIds'] as $currentFeatureValueId) {
  181.                 $productFeatureValues->add($this->em->getRepository(ProductFeatureValue::class)->find($currentFeatureValueId));
  182.             }
  183.             if ($productFeatureValues->count() > 0) {
  184.                 /** @var ProductMenuMaker $productMenuMaker */
  185.                 $productMenuMaker $this->em->getRepository(ProductMenuMaker::class)->findByProductFeatureValues($productFeatureValues);
  186.             }
  187.         }
  188.         $format $request->query->get('format''21x30-1v');
  189.         $modelCategories = [];
  190.         /** @var ModelCategory $modelCategory */
  191.         foreach ($product->getModelCategories() as $modelCategory) {
  192.             $models = [];
  193.             /** @var Model $model */
  194.             foreach ($modelCategory->getModels() as $model) {
  195.                 if ($model->isActive()) {
  196.                     $models[] = $this->serializeService->serializeModel($model$format);
  197.                 }
  198.             }
  199.             if (!is_null($productMenuMaker)) {
  200.                 /** @var Matrix $matrixActive */
  201.                 foreach ($productMenuMaker->getMatrixActives() as $matrixActive) {
  202.                     if (!is_null($matrixActive->getMatrixSet()) && $matrixActive->getMatrixSet()->getModelCategory()->getId() === $modelCategory->getId()) {
  203.                         $matrixes[] = $this->serializeService->serializeMatrix($matrixActive);
  204.                     }
  205.                 }
  206.             }
  207.             $modelCategories[] = [
  208.                 'id' => $modelCategory->getId(),
  209.                 'name' => $this->translationService->translate($modelCategory->getModelCategoryTranslations())->getName(),
  210.                 'reference' => $modelCategory->getReference(),
  211.                 'position' => $modelCategory->getPosition(),
  212.                 'models' => $models,
  213.                 'matrixes' => $matrixes,
  214.             ];
  215.         }
  216.         usort($modelCategories, function ($a$b) {
  217.             return $a['position'] <=> $b['position'];
  218.         });
  219.         return new JsonResponse([
  220.             'success' => true,
  221.             'modelCategories' => $modelCategories,
  222.         ]);
  223.     }
  224.     /**
  225.      * @param Product $product
  226.      * @param Request $request
  227.      * @return JsonResponse
  228.      * @Route("/{product}/model-category-without-model", name="_model_category_whithout_model", requirements={"product"="\d+"})
  229.      */
  230.     public function findModelCategoriesWithoutModels(Product $productRequest $request): JsonResponse
  231.     {
  232.         if (!$this->rsa->isValidToken($request->request->get('apiKey'))) {
  233.             return new JsonResponse([
  234.                 'success' => false,
  235.                 'message' => $this->translator->trans('global.invalidToken'),
  236.             ]);
  237.         }
  238.         $modelCategories = [];
  239.         /** @var ModelCategory $modelCategory */
  240.         foreach ($product->getModelCategories() as $modelCategory) {
  241.             $modelCategories[] = [
  242.                 'id' => $modelCategory->getId(),
  243.                 'name' => $this->translationService->translate($modelCategory->getModelCategoryTranslations())->getName(),
  244.                 'reference' => $modelCategory->getReference(),
  245.                 'position' => $modelCategory->getPosition(),
  246.             ];
  247.         }
  248.         return new JsonResponse([
  249.             'success' => true,
  250.             'modelCategories' => $modelCategories,
  251.         ]);
  252.     }
  253.     /**
  254.      * @param Product $product
  255.      * @param Request $request
  256.      * @return JsonResponse
  257.      * @Route("/{product}/models", name="_models", requirements={"product"="\d+"})
  258.      */
  259.     public function findModels(Product $productRequest $request): JsonResponse
  260.     {
  261.         if (!$this->rsa->isValidToken($request->request->get('apiKey'))) {
  262.             return new JsonResponse([
  263.                 'success' => false,
  264.                 'message' => $this->translator->trans('global.invalidToken'),
  265.             ]);
  266.         }
  267.         $data Tools::getRequestData($request);
  268.         $format $request->query->get('format''21x30-1v');
  269.         $productMenuMaker null;
  270.         $models = [];
  271.         if (array_key_exists('currentFeatureValueIds'$data)) {
  272.             $productFeatureValues = new ArrayCollection();
  273.             foreach ($data['currentFeatureValueIds'] as $currentFeatureValueId) {
  274.                 $productFeatureValues->add($this->em->getRepository(ProductFeatureValue::class)->find($currentFeatureValueId));
  275.             }
  276.             if ($productFeatureValues->count() > 0) {
  277.                 /** @var ProductMenuMaker $productMenuMaker */
  278.                 $productMenuMaker $this->em->getRepository(ProductMenuMaker::class)->findByProductFeatureValues($productFeatureValues);
  279.             }
  280.         }
  281.         /** @var ModelCategory $modelCategory */
  282.         foreach ($product->getModelCategories() as $modelCategory) {
  283.             /** @var Model $model */
  284.             foreach ($modelCategory->getModels() as $model) {
  285.                 if ($model->isActive()) {
  286.                     $models[] = $this->serializeService->serializeModel($model$format);
  287.                 }
  288.             }
  289.             if (!is_null($productMenuMaker)) {
  290.                 /** @var Matrix $matrixActive */
  291.                 foreach ($productMenuMaker->getMatrixActives() as $matrixActive) {
  292.                     if (!is_null($matrixActive->getMatrixSet()) && $matrixActive->getMatrixSet()->getModelCategory()->getId() === $modelCategory->getId()) {
  293.                         $matrix $this->serializeService->serializeMatrix($matrixActive);
  294.                         $matrix['category'] = $modelCategory->getId(); // Change to the model category id instead of the matrix one
  295.                         $models[] = $matrix;
  296.                     }
  297.                 }
  298.             }
  299.         }
  300.         usort($models, function ($a$b) {
  301.             return $a['position'] <=> $b['position'];
  302.         });
  303.         return new JsonResponse([
  304.             'success' => true,
  305.             'models' => $models,
  306.         ]);
  307.     }
  308.     /**
  309.      * @param Product $product
  310.      * @param Request $request
  311.      * @param ModelCategoryRepository $modelCategoryRepository
  312.      * @return JsonResponse
  313.      * @Route("/{product}/has-models", name="_has_models", requirements={"product"="\d+"})
  314.      */
  315.     public function hasModels(Product $productRequest $requestModelCategoryRepository $modelCategoryRepository): JsonResponse
  316.     {
  317.         if (!$this->rsa->isValidToken($request->request->get('apiKey'))) {
  318.             return new JsonResponse([
  319.                 'success' => false,
  320.                 'message' => $this->translator->trans('global.invalidToken'),
  321.             ]);
  322.         }
  323.         $hasModels $modelCategoryRepository->hasAtLeastOneModelInCategories($product);
  324.         return new JsonResponse([
  325.             'success' => true,
  326.             'hasModels' => $hasModels,
  327.         ]);
  328.     }
  329.     /**
  330.      * @param Product $product
  331.      * @param Request $request
  332.      * @return JsonResponse
  333.      * @Route("/{product}/feature", name="_feature", requirements={"product"="\d+"})
  334.      */
  335.     public function findFeatures(Product $productRequest $request): JsonResponse
  336.     {
  337.         if (!$this->rsa->isValidToken($request->request->get('apiKey'))) {
  338.             return new JsonResponse([
  339.                 'success' => false,
  340.                 'message' => $this->translator->trans('global.invalidToken'),
  341.             ]);
  342.         }
  343.         if (!$product->isCommandable()) {
  344.             return new JsonResponse([
  345.                 'success' => false,
  346.                 'url' => $this->generateUrl('app_index'),
  347.             ]);
  348.         }
  349.         $features = [];
  350.         foreach ($product->getProductFeatures() as $productFeature) {
  351.             $features[] = [
  352.                 'id' => $productFeature->getFeature()->getId(),
  353.                 'name' => $this->translationService->translate($productFeature->getFeature()->getFeatureTranslations())->getName(),
  354.                 'isCustomizable' => ($productFeature->getFeature()->getFeatureFamily()->getConstName() === FeatureFamily::CUSTOMIZABLE),
  355.                 'isPrintable' => ($productFeature->getFeature()->getFeatureFamily()->getConstName() === FeatureFamily::PRINTABLE),
  356.                 'isClassic' => ($productFeature->getFeature()->getFeatureFamily()->getConstName() === FeatureFamily::CLASSIC),
  357.                 'constName' => $productFeature->getFeature()->getConstName(),
  358.                 'printComConstName' => $productFeature->getFeature()->getPrintComConstName(),
  359.             ];
  360.         }
  361.         return new JsonResponse([
  362.             'success' => true,
  363.             'features' => $features,
  364.         ]);
  365.     }
  366.     /**
  367.      * @param Product $product
  368.      * @param Request $request
  369.      * @return JsonResponse
  370.      * @Route("/{product}/feature-value", name="_feature_value", requirements={"product"="\d+"})
  371.      * @throws Exception
  372.      */
  373.     public function findFeatureValues(Product $productRequest $request): JsonResponse
  374.     {
  375.         if (!$this->rsa->isValidToken($request->request->get('apiKey'))) {
  376.             return new JsonResponse([
  377.                 'success' => false,
  378.                 'message' => $this->translator->trans('global.invalidToken'),
  379.             ]);
  380.         }
  381.         if (!$product->isCommandable()) {
  382.             return new JsonResponse([
  383.                 'success' => false,
  384.                 'url' => $this->generateUrl('app_index'),
  385.             ]);
  386.         }
  387.         //On envoie les features values commandables (prix + menumaker)
  388.         $featureValues $this->productService->getProductFeatureValuesByProduct($product);
  389.         uasort($featureValues, function ($a$b) {
  390.             return $a['position'] - $b['position'];
  391.         });
  392.         return new JsonResponse([
  393.             'success' => true,
  394.             'featureValues' => $featureValues,
  395.         ]);
  396.     }
  397.     /**
  398.      * @param Product $product
  399.      * @param Request $request
  400.      * @return JsonResponse
  401.      * @Route("/{product}/matrix-category", name="_matrix_category", requirements={"product"="\d+"})
  402.      */
  403.     public function findMatrixCategories(Product $productRequest $request): JsonResponse
  404.     {
  405.         if (!$this->rsa->isValidToken($request->request->get('apiKey'))) {
  406.             return new JsonResponse([
  407.                 'success' => false,
  408.                 'message' => $this->translator->trans('global.invalidToken'),
  409.             ]);
  410.         }
  411.         $data Tools::getRequestData($request);
  412.         $productFeatureValues = new ArrayCollection();
  413.         foreach ($data['currentFeatureValueIds'] as $currentFeatureValueId) {
  414.             $productFeatureValues->add($this->em->getRepository(ProductFeatureValue::class)->find($currentFeatureValueId));
  415.         }
  416.         $matrixCategories = [];
  417.         if ($productFeatureValues->count() > 0) {
  418.             /** @var ProductMenuMaker $productMenuMaker */
  419.             $productMenuMaker $this->em->getRepository(ProductMenuMaker::class)->findByProductFeatureValues($productFeatureValues);
  420.             if (!is_null($productMenuMaker)) {
  421.                 /** @var Matrix $matrixActive */
  422.                 foreach ($productMenuMaker->getMatrixActives() as $matrixActive) {
  423.                     if (is_null($matrixActive->getMatrixSet())) {
  424.                         if (!isset($matrixCategories[$matrixActive->getMatrixCategory()->getId()])) {
  425.                             $matrixCategories[$matrixActive->getMatrixCategory()->getId()] = [
  426.                                 'id' => $matrixActive->getMatrixCategory()->getId(),
  427.                                 'reference' => $matrixActive->getMatrixCategory()->getReference(),
  428.                                 'name' => $this->translationService->translate($matrixActive->getMatrixCategory()->getMatrixCategoryTranslations())->getName(),
  429.                                 'matrixes' => [],
  430.                             ];
  431.                         }
  432.                         $matrixCategories[$matrixActive->getMatrixCategory()->getId()]['matrixes'][] = $this->serializeService->serializeMatrix($matrixActivefalse);
  433.                     }
  434.                 }
  435.                 $matrixCategories array_values($matrixCategories);
  436.             }
  437.         }
  438.         return new JsonResponse([
  439.             'success' => true,
  440.             'matrixCategories' => $matrixCategories,
  441.         ]);
  442.     }
  443.     /**
  444.      * @param Request $request
  445.      * @return JsonResponse
  446.      * @Route("/matrix-category-without-matrix", name="_matrix_category_without_matrix")
  447.      */
  448.     public function findMatrixCategoriesWithoutMatrices(Request $request): JsonResponse
  449.     {
  450.         if (!$this->rsa->isValidToken($request->request->get('apiKey'))) {
  451.             return new JsonResponse([
  452.                 'success' => false,
  453.                 'message' => $this->translator->trans('global.invalidToken'),
  454.             ]);
  455.         }
  456.         $data Tools::getRequestData($request);
  457.         $productFeatureValues = new ArrayCollection();
  458.         foreach ($data['currentFeatureValueIds'] as $currentFeatureValueId) {
  459.             $productFeatureValues->add($this->em->getRepository(ProductFeatureValue::class)->find($currentFeatureValueId));
  460.         }
  461.         $matrixCategories = [];
  462.         if ($productFeatureValues->count() > 0) {
  463.             /** @var ProductMenuMaker $productMenuMaker */
  464.             $productMenuMaker $this->em->getRepository(ProductMenuMaker::class)->findByProductFeatureValues($productFeatureValues);
  465.             if (!is_null($productMenuMaker)) {
  466.                 /** @var Matrix $matrixActive */
  467.                 foreach ($productMenuMaker->getMatrixActives() as $matrixActive) {
  468.                     if (is_null($matrixActive->getMatrixSet())) {
  469.                         $categoryId $matrixActive->getMatrixCategory()->getId();
  470.                         if (!isset($matrixCategories[$categoryId])) {
  471.                             $matrixCategories[$categoryId] = [
  472.                                 'id' => $categoryId,
  473.                                 'reference' => $matrixActive->getMatrixCategory()->getReference(),
  474.                                 'name' => $this->translationService->translate($matrixActive->getMatrixCategory()->getMatrixCategoryTranslations())->getName(),
  475.                             ];
  476.                         }
  477.                     }
  478.                 }
  479.                 $matrixCategories array_values($matrixCategories);
  480.             }
  481.         }
  482.         return new JsonResponse([
  483.             'success' => true,
  484.             'matrixCategories' => $matrixCategories,
  485.         ]);
  486.     }
  487.     /**
  488.      * @param Request $request
  489.      * @return JsonResponse
  490.      * @Route("/matrices", name="_matrices")
  491.      */
  492.     public function findMatrices(Request $request): JsonResponse
  493.     {
  494.         if (!$this->rsa->isValidToken($request->request->get('apiKey'))) {
  495.             return new JsonResponse([
  496.                 'success' => false,
  497.                 'message' => $this->translator->trans('global.invalidToken'),
  498.             ]);
  499.         }
  500.         $data Tools::getRequestData($request);
  501.         $productFeatureValues = new ArrayCollection();
  502.         foreach ($data['currentFeatureValueIds'] as $currentFeatureValueId) {
  503.             $productFeatureValues->add($this->em->getRepository(ProductFeatureValue::class)->find($currentFeatureValueId));
  504.         }
  505.         $matrices = [];
  506.         if ($productFeatureValues->count() > 0) {
  507.             /** @var ProductMenuMaker $productMenuMaker */
  508.             $productMenuMaker $this->em->getRepository(ProductMenuMaker::class)->findByProductFeatureValues($productFeatureValues);
  509.             if (!is_null($productMenuMaker)) {
  510.                 /** @var Matrix $matrixActive */
  511.                 foreach ($productMenuMaker->getMatrixActives() as $matrixActive) {
  512.                     if (is_null($matrixActive->getMatrixSet())) {
  513.                         $matrices[] = $this->serializeService->serializeMatrix($matrixActivefalse);
  514.                     }
  515.                 }
  516.                 $matrices array_values($matrices);
  517.             }
  518.         }
  519.         usort($matrices, function ($a$b) {
  520.             return $a['position'] <=> $b['position'];
  521.         });
  522.         return new JsonResponse([
  523.             'success' => true,
  524.             'matrices' => $matrices,
  525.         ]);
  526.     }
  527.     /**
  528.      * @param Request $request
  529.      * @param MatrixRepository $matrixRepository
  530.      * @return JsonResponse
  531.      * @Route("/has-matrices", name="_has_matrices")
  532.      */
  533.     public function hasMatrices(Request $requestMatrixRepository $matrixRepository): JsonResponse
  534.     {
  535.         if (!$this->rsa->isValidToken($request->request->get('apiKey'))) {
  536.             return new JsonResponse([
  537.                 'success' => false,
  538.                 'message' => $this->translator->trans('global.invalidToken'),
  539.             ]);
  540.         }
  541.         $data Tools::getRequestData($request);
  542.         $featureValueIds $data['currentFeatureValueIds'] ?? [];
  543.         $hasMatrices $matrixRepository->hasAtLeastOneMatrixWithoutSet($featureValueIds);
  544.         return new JsonResponse([
  545.             'success' => true,
  546.             'hasMatrices' => $hasMatrices,
  547.         ]);
  548.     }
  549.     /**
  550.      * @param Product $product
  551.      * @param Request $request
  552.      * @return JsonResponse
  553.      * @Route("/{product}/attribut", name="_attribut", requirements={"product"="\d+"})
  554.      */
  555.     public function findAttributs(Product $productRequest $request): JsonResponse
  556.     {
  557.         if (!$this->rsa->isValidToken($request->request->get('apiKey'))) {
  558.             return new JsonResponse([
  559.                 'success' => false,
  560.                 'message' => $this->translator->trans('global.invalidToken'),
  561.             ]);
  562.         }
  563.         if (!$product->isCommandable()) {
  564.             return new JsonResponse([
  565.                 'success' => false,
  566.                 'url' => $this->generateUrl('app_index'),
  567.             ]);
  568.         }
  569.         $data Tools::getRequestData($request);
  570.         $attributs = [];
  571.         /** @var Attribut $attribut */
  572.         foreach ($product->getAttributValids() as $attribut) {
  573.             $attributValues = [];
  574.             /** @var AttributValue $attributValue */
  575.             foreach ($attribut->getAttributValueActives() as $attributValue) {
  576.                 $attributValuePriceHt $attributValue->getPriceHt();
  577.                 if (isset($data['featureValueFormat'])) {
  578.                     if ($attributValue->getAttribut()->getConstName() === Attribut::MAKING) {
  579.                         /** @var ProductPriceMockup $productPriceMockup */
  580.                         $productPriceMockup $this->em->getRepository(ProductPriceMockup::class)->findOneBy([
  581.                             'product' => $product,
  582.                             'productFeatureValue' => $data['featureValueFormat'],
  583.                         ]);
  584.                         $attributValuePriceHt $productPriceMockup->getPriceHt();
  585.                     }
  586.                 }
  587.                 $attributValueSerialized $this->serializeService->serializeAttributValue($attributValue);
  588.                 $attributValueSerialized['priceHt'] = $attributValuePriceHt;
  589.                 $attributValues[] = $attributValueSerialized;
  590.             }
  591.             $attributSerialized $this->serializeService->serializeAttribut($attribut);
  592.             $attributSerialized['attributValues'] = $attributValues;
  593.             $attributs[] = $attributSerialized;
  594.         }
  595.         return new JsonResponse([
  596.             'success' => true,
  597.             'attributs' => $attributs,
  598.         ]);
  599.     }
  600.     /**
  601.      * @param Product $product
  602.      * @param Request $request
  603.      * @return JsonResponse
  604.      * @Route("/{product}/price", name="_price", requirements={"product"="\d+"})
  605.      * @throws Exception
  606.      */
  607.     public function findPrices(Product $productRequest $request): JsonResponse
  608.     {
  609.         if (!$this->rsa->isValidToken($request->request->get('apiKey'))) {
  610.             return new JsonResponse([
  611.                 'success' => false,
  612.                 'message' => $this->translator->trans('global.invalidToken'),
  613.             ]);
  614.         }
  615.         if (!$product->isCommandable()) {
  616.             return new JsonResponse([
  617.                 'success' => false,
  618.                 'url' => $this->generateUrl('app_index'),
  619.             ]);
  620.         }
  621.         $data Tools::getRequestData($request);
  622.         $quantities = [];
  623.         $isMockup = (array_key_exists('isMockup'$data) ? $data['isMockup'] : false);
  624.         if ($isMockup) {
  625.             if (array_key_exists('creation'$data)) {
  626.                 /** @var Creation $creation */
  627.                 $creation $this->em->getRepository(Creation::class)->find($data['creation']);
  628.                 if (!is_null($creation->getProductFeatureValueFormat())) {
  629.                     /** @var ProductPriceMockup $productPriceMockup */
  630.                     $productPriceMockup $this->em->getRepository(ProductPriceMockup::class)->findOneBy([
  631.                         'product' => $product,
  632.                         'productFeatureValue' => $creation->getProductFeatureValueFormat(),
  633.                     ]);
  634.                     if (!is_null($productPriceMockup)) {
  635.                         $quantities[] = $productPriceMockup->getPriceHt();
  636.                     } else {
  637.                         return new JsonResponse([
  638.                             'success' => false,
  639.                         ]);
  640.                     }
  641.                 } else {
  642.                     return new JsonResponse([
  643.                         'success' => false,
  644.                     ]);
  645.                 }
  646.             } else {
  647.                 $productFeatureValueFormat = (array_key_exists('productFeatureValueFormat'$data) ? $data['productFeatureValueFormat'] : null);
  648.                 if (!is_null($productFeatureValueFormat)) {
  649.                     /** @var ProductFeatureValue $productFeatureValueFormat */
  650.                     $productFeatureValueFormat $this->em->getRepository(ProductFeatureValue::class)->find($productFeatureValueFormat);
  651.                     /** @var ProductPriceMockup $productPriceMockup */
  652.                     $productPriceMockup $this->em->getRepository(ProductPriceMockup::class)->findOneBy([
  653.                         'product' => $product,
  654.                         'productFeatureValue' => $productFeatureValueFormat,
  655.                     ]);
  656.                     if (!is_null($productPriceMockup)) {
  657.                         $quantities[] = $productPriceMockup->getPriceHt();
  658.                     } else {
  659.                         return new JsonResponse([
  660.                             'success' => false,
  661.                         ]);
  662.                     }
  663.                 } else {
  664.                     return new JsonResponse([
  665.                         'success' => false,
  666.                     ]);
  667.                 }
  668.             }
  669.         } else {
  670.             $productFeatureValues = (array_key_exists('productFeatureValues'$data) ? $data['productFeatureValues'] : []);
  671.             $attributValues = (array_key_exists('attributValues'$data) ? $data['attributValues'] : []);
  672.             $creation null;
  673.             $isFormatVariable false;
  674.             $width 0;
  675.             $height 0;
  676.             if (array_key_exists('creation'$data)) {
  677.                 /** @var Creation $creation */
  678.                 $creation $this->em->getRepository(Creation::class)->find($data['creation']);
  679.                 $isFormatVariable $creation->hasFormatVariable();
  680.                 $width $creation->getWidth();
  681.                 $height $creation->getHeight();
  682.             }
  683.             // On récupère les productPrice en fonctions des featuresValues
  684.             $productPrices $this->productService->getProductPricesByFeatureValues($data['allProductFeatureValues'], $productFeatureValues$product);
  685.             foreach ($productPrices as $productPrice) {
  686.                 // Calcul du prix des attributs
  687.                 $attributValuePriceHtTotal 0;
  688.                 if ($attributValues) {
  689.                     foreach ($attributValues as $value) {
  690.                         /** @var AttributValue $attributValue */
  691.                         $attributValue $this->em->getRepository(AttributValue::class)->find($value);
  692.                         if ($attributValue) {
  693.                             $attributValuePriceHt $attributValue->getPriceHt();
  694.                             if (!is_null($creation)) {
  695.                                 if ($attributValue->getAttribut()->getConstName() === Attribut::MAKING) {
  696.                                     /** @var ProductPriceMockup $productPriceMockup */
  697.                                     $productPriceMockup $this->em->getRepository(ProductPriceMockup::class)->findOneBy([
  698.                                         'product' => $product,
  699.                                         'productFeatureValue' => $creation->getProductFeatureValueFormat(),
  700.                                     ]);
  701.                                     $attributValuePriceHt $productPriceMockup->getPriceHt();
  702.                                 }
  703.                             }
  704.                             if ($attributValue->getAttribut()->isMultiplyByQuantity()) {
  705.                                 $attributValuePriceHtTotal += $attributValuePriceHt $productPrice['step'];
  706.                             } else {
  707.                                 $attributValuePriceHtTotal += $attributValuePriceHt;
  708.                             }
  709.                         }
  710.                     }
  711.                 }
  712.                 if ($isFormatVariable) {
  713.                     $squareMeter round(($width $height 10000), 2);
  714.                     $quantities[] = [
  715.                         'quantity' => $productPrice['step'],
  716.                         'priceHt' => (($productPrice['priceHt'] * $squareMeter) * $productPrice['step']) + $attributValuePriceHtTotal,
  717.                     ];
  718.                 } else {
  719.                     $quantities[] = [
  720.                         'quantity' => $productPrice['step'],
  721.                         'priceHt' => $productPrice['priceHt'] + $attributValuePriceHtTotal,
  722.                     ];
  723.                 }
  724.             }
  725.         }
  726.         return new JsonResponse([
  727.             'success' => true,
  728.             'prices' => $quantities,
  729.         ]);
  730.     }
  731.     /**
  732.      * @param Product $product
  733.      * @return JsonResponse
  734.      * @Route("/{product}/folder", name="_folder", requirements={"product"="\d+"})
  735.      */
  736.     public function findFolders(Product $product): JsonResponse
  737.     {
  738.         $folders = [];
  739.         /** @var Folder $folder */
  740.         foreach ($product->getFolders() as $folder) {
  741.             $folders[] = [
  742.                 'id' => $folder->getId(),
  743.                 'name' => $folder->getName(),
  744.             ];
  745.         }
  746.         return new JsonResponse([
  747.             'success' => true,
  748.             'folders' => $folders,
  749.         ]);
  750.     }
  751.     /**
  752.      * @param Product $product
  753.      * @param Request $request
  754.      * @return JsonResponse
  755.      * @throws Exception
  756.      * @Route("/{product}/associated-products", name="_associated_products", requirements={"product"="\d+"})
  757.      */
  758.     public function findAssociatedProducts(Product $productRequest $request): JsonResponse
  759.     {
  760.         if (!$this->rsa->isValidToken($request->request->get('apiKey'))) {
  761.             return new JsonResponse([
  762.                 'success' => false,
  763.                 'message' => $this->translator->trans('global.invalidToken'),
  764.             ]);
  765.         }
  766.         $associatedProducts $product->getAssociatedProducts();
  767.         $serializedProducts = [];
  768.         foreach ($associatedProducts as $associatedProduct) {
  769.             $serializedProducts[] = $this->serializeService->serializeProduct($associatedProduct->getAssociatedProduct());
  770.         }
  771.         return new JsonResponse([
  772.             'success' => true,
  773.             'products' => $serializedProducts,
  774.         ]);
  775.     }
  776. }