src/Controller/Uniqskills/Cart/CartStepsController.php line 49

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Uniqskills\Cart;
  3. use App\Enum\GoogleAnalyticsEvents;
  4. use App\Enum\PortalHashes;
  5. use App\Event\Analitics\InitiateCheckoutEvent;
  6. use App\Form\Frontend\Uniqskills\Cart\CustomerType;
  7. use App\Utils\Api\ApiCartService;
  8. use App\Utils\Uniqskills\Cart\Actions\CartAddressService;
  9. use App\Utils\Uniqskills\Cart\Actions\CartDetailsService;
  10. use App\Utils\Uniqskills\Cart\Actions\CartPortalService;
  11. use App\Utils\Uniqskills\Cart\Actions\CartTermsService;
  12. use App\Utils\Uniqskills\GoogleAnalytics\GoogleAnalyticsEventService;
  13. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  14. use Symfony\Component\HttpFoundation\JsonResponse;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\HttpFoundation\Response;
  17. use Symfony\Component\Routing\Annotation\Route;
  18. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  19. /**
  20.  * @Route("/{_locale}/cart", name="app_uniqskills_cart_")))
  21.  */
  22. class CartStepsController extends AbstractController
  23. {
  24.     private $cartDetailsService;
  25.     private $cartAddressService;
  26.     private $cartPortalService;
  27.     private $gaService;
  28.     public function __construct(
  29.         CartDetailsService $cartDetailsService,
  30.         CartAddressService $cartAddressService,
  31.         CartTermsService $cartTermsService,
  32.         CartPortalService $cartPortalService,
  33.         GoogleAnalyticsEventService $gaService,
  34.         ApiCartService $apiCartService
  35.     ) {
  36.         $this->cartDetailsService $cartDetailsService;
  37.         $this->cartAddressService $cartAddressService;
  38.         $this->cartPortalService $cartPortalService;
  39.         $this->gaService $gaService;
  40.     }
  41.     /**
  42.      * @Route("", name="index", host="www.%us_url%")
  43.      */
  44.     public function index(): Response
  45.     {
  46.         if ($_ENV['US_PORTAL_SETTINGS'] === PortalHashes::UK_UNIQSKILLS)
  47.         {
  48.             return $this->redirectToRoute('uniqskills_homepage');
  49.         }
  50.         return $this->render('uniqskills/cartcms/index.html.twig');
  51.     }
  52.     /**
  53.      * @Route("/render-index-data", name="render_index_data")
  54.      */
  55.     public function indexData(Request $requestEventDispatcherInterface $eventDispatcher): JsonResponse
  56.     {
  57.         $cart $this->cartDetailsService->getCart($this->getUser());
  58.         $portalSettings $this->cartPortalService->getPortalSettings();
  59.         if ($cart['hash'])
  60.         {
  61.             $eventDispatcher->dispatch(new InitiateCheckoutEvent($cart['hash']), InitiateCheckoutEvent::CART_INITIATE_CHECKOUT);
  62.         }
  63.         return $this->json([
  64.             'cart' => $this->renderView('uniqskills/cartcms/index_content.html.twig', [
  65.                 'cart' => $cart,
  66.                 'portalSettings' => $portalSettings
  67.             ]),
  68.             'gaEvent' => $this->gaService->getEventContent($cartGoogleAnalyticsEvents::VIEW_CART)
  69.         ]);
  70.     }
  71.     /**
  72.      * @Route("/details", name="details")
  73.      */
  74.     public function details(Request $request): Response
  75.     {
  76.         if ($_ENV['US_PORTAL_SETTINGS'] === PortalHashes::UK_UNIQSKILLS)
  77.         {
  78.             return $this->redirectToRoute('uniqskills_homepage');
  79.         }
  80.         return $this->render('uniqskills/cartcms/details.html.twig');
  81.     }
  82.     /**
  83.      * @Route("/formularz", name="form")
  84.      */
  85.     public function form(Request $request): Response
  86.     {
  87.         if ($_ENV['US_PORTAL_SETTINGS'] === PortalHashes::UK_UNIQSKILLS)
  88.         {
  89.             return $this->redirectToRoute('uniqskills_homepage');
  90.         }
  91.         $portalSettings $this->cartPortalService->getPortalSettings();
  92.         $address        $this->cartAddressService->getAddress($this->getUser(), $request->request->all());
  93.         $cart           $this->cartDetailsService->getCart($this->getUser());
  94.         $positions      $this->cartPortalService->getPositionsForPortal($portalSettings);
  95.         $honorifics     $this->cartDetailsService->getHonorificsForPortal($portalSettings);
  96.         $form $this->createForm(CustomerType::class, $address, [
  97.             'position' => $positions,
  98.             'validation_groups' => false
  99.         ])->handleRequest($request);
  100.         if (empty($cart['products']))
  101.         {
  102.             return $this->json([]);
  103.         }
  104.         return $this->json([
  105.             'form' => $this->renderView('uniqskills/cartcms/form.html.twig', [
  106.                 'cart' => $cart,
  107.                 'address' => $address,
  108.                 'form' => $form->createView(),
  109.                 'hidePositions' => empty($positions) || $portalSettings->getHideFieldPosition() === true,
  110.                 'hideHonorifics' => empty($honorifics)
  111.             ]),
  112.             'gaEvent' => $this->gaService->getEventContent($cartGoogleAnalyticsEvents::BEGIN_CHECKOUT)
  113.         ]);
  114.     }
  115. }