src/Controller/Customer/CustomerController.php line 79

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Customer;
  3. use App\Entity\Addressing\Country;
  4. use App\Entity\Addressing\County;
  5. use App\Entity\Customer\Customer;
  6. use App\Events\Customer\SubscriptionEvent;
  7. use App\Manager\Customer\ConfirmationCommercialNotificationManager;
  8. use App\Model\CustomerModel;
  9. use App\Service\AuthTokenService;
  10. use App\Service\CustomerService;
  11. use App\Service\ReCaptcha;
  12. use App\Validator\RegisterFormValidator;
  13. use FOS\RestBundle\View\View;
  14. use Sylius\Bundle\ResourceBundle\Controller\ResourceController;
  15. use Sylius\Bundle\ResourceBundle\Event\ResourceControllerEvent;
  16. use Sylius\Component\Resource\Exception\UpdateHandlingException;
  17. use Sylius\Component\Resource\ResourceActions;
  18. use Symfony\Component\EventDispatcher\GenericEvent;
  19. use Symfony\Component\HttpFoundation\Request;
  20. use Symfony\Component\HttpFoundation\Response;
  21. use Symfony\Component\HttpKernel\Exception\HttpException;
  22. use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
  23. use function Sentry\captureException;
  24. class CustomerController extends ResourceController
  25. {
  26.     public function manageCustomerAction(Request $request)
  27.     {
  28.         $customerId $request->get('customerId');
  29.         $customer $this->getDoctrine()->getRepository(Customer::class)->find($customerId);
  30.         return $this->render('@templates/AdminCustom/Customer/manage-customer.html.twig', [
  31.             'customer' => $customer
  32.         ]);
  33.     }
  34.     public function createAction(Request $request): Response
  35.     {
  36.         return $this->redirectToRoute('app_shop_customer_register');
  37.     }
  38.     public function disableCustomerAction(Request $request)
  39.     {
  40.         $customerModel = new CustomerModel($this->getDoctrine()->getManager());
  41.         $result $customerModel->disableCustomer($request$this->getUser());
  42.         $response json_encode('Error');
  43.         if ($result == -1) {
  44.             $response json_encode('Customer is already disabled');
  45.         } elseif ($result == 1) {
  46.             $response json_encode('Success');
  47.         }
  48.         return new Response($response);
  49.     }
  50.     public function restoreCustomerAction(Request $request)
  51.     {
  52.         $customerModel = new CustomerModel($this->getDoctrine()->getManager());
  53.         $result $customerModel->restoreCustomer($request);
  54.         $response json_encode('Error');
  55.         if ($result == -1) {
  56.             $response json_encode('Customer is already restored or is not disabled');
  57.         } elseif ($result == 1) {
  58.             $response json_encode('Success');
  59.         }
  60.         return new Response($response);
  61.     }
  62.     public function registerAction()
  63.     {
  64.         $user $this->getUser();
  65.         if ($user) {
  66.             $this->addFlash('already_logged_in''1');
  67.             return $this->redirectToRoute('sylius_shop_homepage');
  68.         }
  69.         return $this->render('@templates/Account/register.html.twig', [
  70.             'countries' => $this->getDoctrine()->getRepository(Country::class)->findAll(),
  71.         ]);
  72.     }
  73.     public function submitRegisterAction(Request $request)
  74.     {
  75.         $session $request->getSession();
  76.         $registerFormValidator = new RegisterFormValidator($this->getDoctrine()->getManager(), $this->container);
  77.         $params $request->request->all();
  78.         $reCaptcha = new ReCaptcha($_ENV['RECAPTCHA3_SECRET']);
  79.         if ($registerFormValidator->checkForm($request) && $reCaptcha->verify($params['g-recaptcha-response'], $request->getClientIp())) {
  80.             $customerService = new CustomerService($this->getDoctrine()->getManager(), $this->container->get('router'));
  81.            if ($customerService->registerNewCustomer($request)) {
  82.                $customer $this->getDoctrine()->getRepository(Customer::class)->findOneByEmail($params['customerEmail']);
  83.                $address $customerService->createNewAddressForCustomer($customer$params$this->getDoctrine()->getRepository(County::class));
  84.                if ($address) {
  85.                    $this->getDoctrine()->getManager()->persist($address);
  86.                    $this->getDoctrine()->getManager()->flush();
  87.                }
  88.                if (CustomerService::checkIfCustomerWantToSubscribe($request)) {
  89.                    try {
  90.                        $customerEvent = new GenericEvent($customer);
  91.                        $eventDispatcher $this->get('event_dispatcher');
  92.                        $eventDispatcher->dispatch($customerEvent'app.customer.post_register');
  93.                        $commercialNotificationService $this->container->get('app.service.customer.commercial_notification');
  94.                        $commercialNotificationService->addToQueueConfirmationEmailForCommercialNotificationForCustomer($customerConfirmationCommercialNotificationManager::EMAIL_TYPE_NEW_ACCOUNT_ID);
  95.                    } catch (\Throwable $exception) {
  96.                        captureException($exception);
  97.                    }
  98.                    $customerModel = new CustomerModel($this->getDoctrine()->getManager());
  99.                    $customerModel->subscribeCustomerToNewsletter($customer$request->getClientIp());
  100.                }
  101.                $token AuthTokenService::authenticateNewCustomer($customer);
  102.                $this->get('security.token_storage')->setToken($token);
  103.                $event = new InteractiveLoginEvent($request$token);
  104.                $dispatcher $this->get('event_dispatcher');
  105.                $dispatcher->dispatch($event'security.interactive_login');
  106.                $session->set('_sylius.cart.FASHION_WEB'180);
  107.                $session->save();
  108.                $response = array('success' => 1);
  109.            } else {
  110.                $response = array('fail' => 1);
  111.            }
  112.         } else {
  113.             if ($session->has('registerErrors')) {
  114.                 $response json_decode($session->get('registerErrors'));
  115.             } else {
  116.                 $response = array('captcha_error' => 1);
  117.             }
  118.         }
  119.         return new Response(json_encode($response));
  120.     }
  121.     /**
  122.      * @param Request $request
  123.      * @return Response
  124.      * METODA OVERRIDE DIN SYLIUS CONTROLLER
  125.      */
  126.     public function updateAction(Request $request): Response
  127.     {
  128.         $configuration $this->requestConfigurationFactory->create($this->metadata$request);
  129.         $this->isGrantedOr403($configurationResourceActions::UPDATE);
  130.         $resource $this->findOr404($configuration);
  131.         $form $this->resourceFormFactory->create($configuration$resource);
  132.         if (in_array($request->getMethod(), ['POST''PUT''PATCH'], true) && $form->handleRequest($request)->isValid()) {
  133.             $resource $form->getData();
  134.             $event $this->eventDispatcher->dispatchPreEvent(ResourceActions::UPDATE$configuration$resource);
  135.             $customer $this->findOr404($configuration);
  136.             $subscriptionEvent = new SubscriptionEvent($customer);
  137.             if ($form->has('subscribedToNewsletter')) {
  138.                 if ($form->get('subscribedToNewsletter')->getData()) {
  139.                     $this->get('event_dispatcher')->dispatch($subscriptionEventSubscriptionEvent::SUBSCRIBE_CUSTOMER);
  140.                 } else {
  141.                     $this->get('event_dispatcher')->dispatch($subscriptionEventSubscriptionEvent::UNSUBSCRIBE_CUSTOMER);
  142.                 }
  143.             }
  144.             if ($event->isStopped() && !$configuration->isHtmlRequest()) {
  145.                 throw new HttpException($event->getErrorCode(), $event->getMessage());
  146.             }
  147.             if ($event->isStopped()) {
  148.                 $this->flashHelper->addFlashFromEvent($configuration$event);
  149.                 $eventResponse $event->getResponse();
  150.                 if (null !== $eventResponse) {
  151.                     return $eventResponse;
  152.                 }
  153.                 return $this->redirectHandler->redirectToResource($configuration$resource);
  154.             }
  155.             try {
  156.                 $this->resourceUpdateHandler->handle($resource$configuration$this->manager);
  157.             } catch (UpdateHandlingException $exception) {
  158.                 if (!$configuration->isHtmlRequest()) {
  159.                     return $this->viewHandler->handle(
  160.                         $configuration,
  161.                         View::create($form$exception->getApiResponseCode())
  162.                     );
  163.                 }
  164.                 $this->flashHelper->addErrorFlash($configuration$exception->getFlash());
  165.                 return $this->redirectHandler->redirectToReferer($configuration);
  166.             }
  167.             if ($configuration->isHtmlRequest()) {
  168.                 $this->flashHelper->addSuccessFlash($configurationResourceActions::UPDATE$resource);
  169.             }
  170.             $postEvent $this->eventDispatcher->dispatchPostEvent(ResourceActions::UPDATE$configuration$resource);
  171.             if (!$configuration->isHtmlRequest()) {
  172.                 $view $configuration->getParameters()->get('return_content'false) ? View::create($resourceResponse::HTTP_OK) : View::create(nullResponse::HTTP_NO_CONTENT);
  173.                 return $this->viewHandler->handle($configuration$view);
  174.             }
  175.             $postEventResponse $postEvent->getResponse();
  176.             if (null !== $postEventResponse) {
  177.                 return $postEventResponse;
  178.             }
  179.             return $this->redirectHandler->redirectToResource($configuration$resource);
  180.         }
  181.         if (!$configuration->isHtmlRequest()) {
  182.             return $this->viewHandler->handle($configurationView::create($formResponse::HTTP_BAD_REQUEST));
  183.         }
  184.         $initializeEvent $this->eventDispatcher->dispatchInitializeEvent(ResourceActions::UPDATE$configuration$resource);
  185.         $initializeEventResponse $initializeEvent->getResponse();
  186.         if (null !== $initializeEventResponse) {
  187.             return $initializeEventResponse;
  188.         }
  189.         $view View::create()
  190.             ->setData([
  191.                 'configuration' => $configuration,
  192.                 'metadata' => $this->metadata,
  193.                 'resource' => $resource,
  194.                 $this->metadata->getName() => $resource,
  195.                 'form' => $form->createView(),
  196.             ])
  197.             ->setTemplate($configuration->getTemplate(ResourceActions::UPDATE '.html'))
  198.         ;
  199.         return $this->viewHandler->handle($configuration$view);
  200.     }
  201.     public function updateAccountAction(Request $request): Response
  202.     {
  203.         $customer CustomerService::retrieveCustomerFromShopUser($this->getUser());
  204.         return $this->render('@templates/Account/Customer/update.html.twig', [
  205.             'customerData' => CustomerService::retrieveCustomerDataForAccount($customer),
  206.             'translations' => CustomerService::retrieveTranslationsForCustomerAccount($this->container->get('translator')),
  207.         ]);
  208.     }
  209.     public function updateProfile(Request $request): Response
  210.     {
  211.         $customer CustomerService::retrieveCustomerFromShopUser($this->getUser());
  212.         return new Response(json_encode(CustomerService::updateCustomerData($request->request->all(), $customer$this->getDoctrine()->getManager(), $this->container->get('translator'))));
  213.     }
  214.     public function tryToFindCustomerData(Request $request)
  215.     {
  216.         $customerService = new CustomerService($this->getDoctrine()->getManager(), $this->container->get('router'));
  217.         return new Response(json_encode($customerService->tryToFindCustomerDataByEmailOrPhone($request->get('email'), $request->get('phone'))));
  218.     }
  219. }