src/Controller/Bikers/InscriptionController.php line 32

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. //namespace App\Controller\Extras;
  4. namespace BitBag\OpenMarketplace\Controller\Bikers;
  5. use BitBag\OpenMarketplace\Controller\MangoPay\MangoPayService;
  6. use Sylius\Bundle\ResourceBundle\Controller\ResourceController;
  7. use Sylius\Component\Resource\ResourceActions;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\HttpKernel\Exception\HttpException;
  11. use Doctrine\Persistence\ObjectManager;
  12. use FOS\RestBundle\View\View;
  13. use Sylius\Bundle\ResourceBundle\Event\ResourceControllerEvent;
  14. use Sylius\Component\Resource\Exception\DeleteHandlingException;
  15. use Sylius\Component\Resource\Exception\UpdateHandlingException;
  16. use Sylius\Component\Resource\Factory\FactoryInterface;
  17. use Sylius\Component\Resource\Metadata\MetadataInterface;
  18. use Sylius\Component\Resource\Model\ResourceInterface;
  19. use Sylius\Component\Resource\Repository\RepositoryInterface;
  20. use Symfony\Component\DependencyInjection\ContainerAwareTrait;
  21. use Symfony\Component\DependencyInjection\ContainerInterface;
  22. use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
  23. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  24. use Symfony\Component\Security\Core\Exception\AccessDeniedException;
  25. use Symfony\Component\HttpClient\HttpClient;
  26. class InscriptionController extends ResourceController
  27. {
  28.     public function createAction(Request $request): Response
  29.     {
  30.         $configuration $this->requestConfigurationFactory->create($this->metadata$request);
  31.         $this->isGrantedOr403($configurationResourceActions::CREATE);
  32.         $newResource $this->newResourceFactory->create($configuration$this->factory);
  33.         $form $this->resourceFormFactory->create($configuration$newResource);
  34.         $form->handleRequest($request);
  35.         if ($request->isMethod('POST') && $form->isSubmitted() && $form->isValid()) {
  36.             $formData $form->getData();
  37.             $mangoPay = new MangoPayService();
  38.             $user $mangoPay->createMangoUser(
  39.                 $formData->getFirstName(),
  40.                 $formData->getLastName(),
  41.                 $formData->getEmail(),
  42.                 $formData->getAddressNumber(),
  43.                 $formData->getAddressName(),
  44.                 $formData->getCity(),
  45.                 $formData->getCodePostal(),
  46.                 $formData->getRegion()
  47.             );
  48.             $wallet $mangoPay->createWallet($user->Id);
  49.             $newResource $form->getData();
  50.             $route=$request->attributes->get('_route');
  51.             $newResource->setSubscribedToNewsletter(true);
  52.             $newResource->setIsVendor(true);
  53.             $newResource->setWalletId(intval($wallet->Id));
  54.             $event $this->eventDispatcher->dispatchPreEvent(ResourceActions::CREATE$configuration$newResource);
  55.             if ($event->isStopped() && !$configuration->isHtmlRequest()) {
  56.                 throw new HttpException($event->getErrorCode(), $event->getMessage());
  57.             }
  58.             if ($event->isStopped()) {
  59.                 $this->flashHelper->addFlashFromEvent($configuration$event);
  60.                 $eventResponse $event->getResponse();
  61.                 if (null !== $eventResponse) {
  62.                     return $eventResponse;
  63.                 }
  64.                 return $this->redirectHandler->redirectToIndex($configuration$newResource);
  65.             }
  66.             if ($configuration->hasStateMachine()) {
  67.                 $stateMachine $this->getStateMachine();
  68.                 $stateMachine->apply($configuration$newResource);
  69.             }
  70.             $this->repository->add($newResource);
  71.             if ($configuration->isHtmlRequest()) {
  72.                 $this->flashHelper->addSuccessFlash($configurationResourceActions::CREATE$newResource);
  73.             }
  74.             $postEvent $this->eventDispatcher->dispatchPostEvent(ResourceActions::CREATE$configuration$newResource);
  75.             if (!$configuration->isHtmlRequest()) {
  76.                 return $this->createRestView($configuration$newResourceResponse::HTTP_CREATED);
  77.             }
  78.             $postEventResponse $postEvent->getResponse();
  79.             if (null !== $postEventResponse) {
  80.                 return $postEventResponse;
  81.             }
  82.             return $this->redirectHandler->redirectToResource($configuration$newResource);
  83.         }
  84.         if ($request->isMethod('POST') && $form->isSubmitted() && !$form->isValid()) {
  85.             $responseCode Response::HTTP_UNPROCESSABLE_ENTITY;
  86.         }
  87.         if (!$configuration->isHtmlRequest()) {
  88.             return $this->createRestView($configuration$formResponse::HTTP_BAD_REQUEST);
  89.         }
  90.         $initializeEvent $this->eventDispatcher->dispatchInitializeEvent(ResourceActions::CREATE$configuration$newResource);
  91.         $initializeEventResponse $initializeEvent->getResponse();
  92.         if (null !== $initializeEventResponse) {
  93.             return $initializeEventResponse;
  94.         }
  95. //        $TemplateName=$configuration->getTemplate(ResourceActions::CREATE . '.html');
  96.         $route=$request->attributes->get('_route');
  97.         $TemplateName="bundles/SyliusShopBundle/register.html.twig";
  98.         //var_dump($route,$configuration);exit;
  99.         //var_dump($route,$form,$configuration,$this->metadata,$newResource);
  100.         return $this->render($TemplateName, [
  101.             "Route"=>$route,
  102.             'configuration' => $configuration,
  103.             'metadata' => $this->metadata,
  104.             'resource' => $newResource,
  105.             $this->metadata->getName() => $newResource,
  106.             'form' => $form->createView(),
  107.         ], null$responseCode ?? Response::HTTP_OK);
  108.     }
  109. }