<?php
namespace App\Controller\Api;
use App\Entity\Creation;
use App\Entity\Feature;
use App\Entity\Institution;
use App\Entity\Model;
use App\Entity\ProductFeatureValue;
use App\Entity\ProductMenuMaker;
use App\Entity\ProductPrice;
use App\Entity\User;
use App\Service\CreationService;
use App\Service\InstitutionService;
use App\Service\ProductService;
use App\Service\Tools;
use Doctrine\ORM\EntityManagerInterface;
use Exception;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpKernel\Exception\NotAcceptableHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
/**
* Class AccountCreationController
* @package App\Controller\Api
* @Route(path="/api/account/creation", name="api_account_creation")
* @IsGranted("IS_AUTHENTICATED_FULLY")
*/
class AccountCreationController extends AbstractController
{
private EntityManagerInterface $em;
private CreationService $creationService;
private InstitutionService $institutionService;
private ProductService $productService;
public function __construct(
EntityManagerInterface $em,
CreationService $creationService,
InstitutionService $institutionService,
ProductService $productService
) {
$this->em = $em;
$this->creationService = $creationService;
$this->institutionService = $institutionService;
$this->productService = $productService;
}
/**
* @param Creation $creation
* @param $institutionId
* @param Request $request
* @return JsonResponse
* @Route("/{creation}/{institutionId}", name="find", requirements={"creation"="\d+"}, defaults={"institutionId"=null})
*/
public function find(Creation $creation, $institutionId, Request $request): JsonResponse
{
/** @var User $user */
$user = $this->getUser();
/** @var Institution|JsonResponse $institution */
$institution = $this->institutionService->getInstitutionForUser($request, $user);
// Check if a JSON error has been returned
if ($institution instanceof JsonResponse) {
return $institution;
}
if (!$institution->getCreations()->contains($creation)) {
throw new NotFoundHttpException("Creation not found");
}
$content = $this->creationService->getContent($creation);
return new JsonResponse([
'success' => true,
'name' => $creation->getName(),
'content' => $content,
]);
}
/**
* @param Creation $creation
* @param Request $request
* @return JsonResponse
* @Route("/save/{creation}", name="save", requirements={"creation"="\d+"})
*/
public function save(Creation $creation, Request $request): JsonResponse
{
/** @var User $user */
$user = $this->getUser();
/** @var Institution|JsonResponse $institution */
$institution = $this->institutionService->getInstitutionForUser($request, $user);
// Check if a JSON error has been returned
if ($institution instanceof JsonResponse) {
return $institution;
}
if (!$institution->getCreations()->contains($creation)) {
throw new NotFoundHttpException("Creation not found");
}
$data = Tools::getRequestData($request);
$content = $data['content'];
if (!$this->creationService->isValidData($content)) {
throw new NotAcceptableHttpException('Data parameter is invalid');
}
$creation->setContent($content);
if (!is_null($creation->getContent()['model'])) {
/** @var Model $model */
$model = $this->em->getRepository(Model::class)->find($creation->getContent()['model']['id']);
$creation->setModel($model);
} else {
$creation->setModel(null);
}
$this->creationService->saveCreationHistory($creation, $content);
$this->em->persist($creation);
$this->em->flush();
return new JsonResponse([
'success' => true,
]);
}
/**
* @param Creation $creation
* @param Request $request
* @return JsonResponse
* @throws Exception
* @Route("/format/{creation}", name="format", requirements={"creation"="\d+"})
*/
public function updateFormat(Creation $creation, Request $request): JsonResponse
{
/** @var User $user */
$user = $this->getUser();
/** @var Institution|JsonResponse $institution */
$institution = $this->institutionService->getInstitutionForUser($request, $user);
// Check if a JSON error has been returned
if ($institution instanceof JsonResponse) {
return $institution;
}
if (!$institution->getCreations()->contains($creation)) {
throw new NotFoundHttpException("Creation not found");
}
$data = Tools::getRequestData($request);
$ref = $data['ref'];
// Modification du format de la création
/** @var ProductMenuMaker $productMenuMaker */
$productMenuMaker = $this->em->getRepository(ProductMenuMaker::class)->findOneBy([
'reference' => $ref
]);
foreach ($creation->getProductFeatureValues() as $creationProductFeatureValue) {
foreach ($productMenuMaker->getProductFeatureValues() as $productMenuMakerProductFeatureValue) {
if($creationProductFeatureValue->getProductFeature()->getId() === $productMenuMakerProductFeatureValue->getProductFeature()->getId()) {
$creation->removeProductFeatureValue($creationProductFeatureValue);
$creation->addProductFeatureValue($productMenuMakerProductFeatureValue);
}
}
}
$creation->setMatrix(null);
$this->em->persist($creation);
// Modification de productPrice si la création est en panier
foreach ($creation->getCartElements() as $cartElement) {
$productFeatureValues = [];
/** @var ProductFeatureValue $productFeatureValue */
foreach ($cartElement->getProductPrice()->getProductFeatureValuesSorted() as $productFeatureValue) {
$productFeatureValues[$productFeatureValue->getProductFeature()->getId()] = $productFeatureValue->getPosition() . '~' . $productFeatureValue->getId();
}
foreach ($creation->getProductFeatureValues() as $creationProductFeatureValue) {
if(array_key_exists($creationProductFeatureValue->getProductFeature()->getId(), $productFeatureValues)) {
$productFeatureValues[$creationProductFeatureValue->getProductFeature()->getId()] = $creationProductFeatureValue->getPosition() . '~' . $creationProductFeatureValue->getId();
}
}
$productPrice = $this->productService->getOneProductPriceByFeatureValuesQuantity($productFeatureValues, $cartElement->getProduct(), $cartElement->getQuantity());
/** @var ProductPrice $productPrice */
$productPrice = $this->em->getRepository(ProductPrice::class)->findOneBy([
'id' => $productPrice['id']
]);
if (!$productPrice) {
$this->em->remove($cartElement);
} else {
$cartElement->setProductPrice($productPrice);
$this->em->persist($cartElement);
}
}
$this->em->flush();
return new JsonResponse(true);
}
/**
* @param Creation $creation
* @param Request $request
* @return JsonResponse
* @Route("/save/{creation}/name", name="save_name", requirements={"creation"="\d+"})
*/
public function saveName(Creation $creation, Request $request): JsonResponse
{
/** @var User $user */
$user = $this->getUser();
/** @var Institution|JsonResponse $institution */
$institution = $this->institutionService->getInstitutionForUser($request, $user);
// Check if a JSON error has been returned
if ($institution instanceof JsonResponse) {
return $institution;
}
if (!$institution->getCreations()->contains($creation)) {
throw new NotFoundHttpException("Creation not found");
}
$data = Tools::getRequestData($request);
$name = $data['name'];
$creation->setName($name);
$this->em->persist($creation);
$this->em->flush();
return new JsonResponse([
'success' => true,
'name' => $name,
]);
}
/**
* @param Creation $creation
* @param Request $request
* @return JsonResponse
* @throws ClientExceptionInterface
* @throws RedirectionExceptionInterface
* @throws ServerExceptionInterface
* @throws TransportExceptionInterface
* @Route("/save/{creation}/preview", name="save_preview", requirements={"creation"="\d+"})
*/
public function savePreview(Creation $creation, Request $request): JsonResponse
{
/** @var User $user */
$user = $this->getUser();
/** @var Institution|JsonResponse $institution */
$institution = $this->institutionService->getInstitutionForUser($request, $user);
// Check if a JSON error has been returned
if ($institution instanceof JsonResponse) {
return $institution;
}
if (!$institution->getCreations()->contains($creation)) {
throw new NotFoundHttpException("Creation not found");
}
$data = Tools::getRequestData($request);
$data = $data['data'];
try {
$this->creationService->savePreview($creation, $data);
} catch (Exception $e) {
throw new HttpException($e->getCode(), $e->getMessage());
}
return new JsonResponse([
'success' => true,
]);
}
/**
* @param Creation $creation
* @param Request $request
* @return JsonResponse
* @throws ClientExceptionInterface
* @throws RedirectionExceptionInterface
* @throws ServerExceptionInterface
* @throws TransportExceptionInterface
* @Route("/save/{creation}/documents", name="save_document", requirements={"creation"="\d+"})
*/
public function saveDocuments(Creation $creation, Request $request): JsonResponse
{
/** @var User $user */
$user = $this->getUser();
/** @var Institution|JsonResponse $institution */
$institution = $this->institutionService->getInstitutionForUser($request, $user);
// Check if a JSON error has been returned
if ($institution instanceof JsonResponse) {
return $institution;
}
if (!$institution->getCreations()->contains($creation)) {
throw new NotFoundHttpException("Creation not found");
}
$data = Tools::getRequestData($request);
$nbDocuments = $institution->getPdfmenuDocuments()->count();
try {
foreach ($data['documents'] as $document) {
if (isset($document)) {
$document['position'] += $nbDocuments;
$this->creationService->saveDocument($creation, $document);
}
}
} catch (Exception $e) {
throw new HttpException($e->getCode(), $e->getMessage());
}
return new JsonResponse([
'success' => true,
]);
}
}