src/Controller/Ecommerce/GiftCheckoutController.php line 115

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Ecommerce;
  3. use App\Assigner\Ecommerce\Interfaces\CartShippingAssignerInterface;
  4. use App\Controller\Ecommerce\Traits\ExtractsExtraTypesFromGift;
  5. use App\Entity\Ecommerce\AddressBillable;
  6. use App\Entity\Ecommerce\Gift;
  7. use App\Enum\Ecommerce\ShippingMethodsEnum;
  8. use App\Factory\Ecommerce\GiftFormFactory;
  9. use App\Factory\Ecommerce\Interfaces\GiftFormFactoryInterface;
  10. use App\Form\Ecommerce\GiftType;
  11. use App\Processor\Ecommerce\Interfaces\GiftHasExtraProcessorInterface;
  12. use App\Repository\Ecommerce\Interfaces\GiftRepositoryInterface;
  13. use App\Service\Ecommerce\GiftToCartService;
  14. use App\Util\Interfaces\UrlGeneratorAwareInterface;
  15. use App\Util\Traits\UrlGeneratorAwareTrait;
  16. use Symfony\Component\HttpFoundation\RedirectResponse;
  17. use Symfony\Component\HttpFoundation\Request;
  18. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  19. use WAM\Bundle\CoreBundle\Controller\Traits\RendererControllerTrait;
  20. use WAM\Bundle\CoreBundle\Util\Interfaces\RendererAwareInterface;
  21. /**
  22.  * @author Gerard Rico <grico@wearemarketing.com>
  23.  */
  24. class GiftCheckoutController implements RendererAwareInterfaceUrlGeneratorAwareInterface
  25. {
  26.     use RendererControllerTrait;
  27.     use UrlGeneratorAwareTrait;
  28.     use ExtractsExtraTypesFromGift;
  29.     private GiftRepositoryInterface $giftRepository;
  30.     private GiftFormFactory $giftFormFactory;
  31.     private GiftHasExtraProcessorInterface $giftHasExtraProcessor;
  32.     private GiftToCartService $giftToCartService;
  33.     private CartShippingAssignerInterface $cartShippingAssigner;
  34.     public function __construct(
  35.         GiftRepositoryInterface $giftRepository,
  36.         GiftFormFactory $giftFormFactory,
  37.         GiftHasExtraProcessorInterface $giftHasExtraProcessor,
  38.         GiftToCartService $giftToCartService,
  39.         CartShippingAssignerInterface $cartShippingAssigner
  40.     ) {
  41.         $this->giftRepository $giftRepository;
  42.         $this->giftFormFactory $giftFormFactory;
  43.         $this->giftHasExtraProcessor $giftHasExtraProcessor;
  44.         $this->giftToCartService $giftToCartService;
  45.         $this->cartShippingAssigner $cartShippingAssigner;
  46.     }
  47.     public function __invoke(Request $requestint $id)
  48.     {
  49.         /** @var Gift $gift */
  50.         $gift $this->giftRepository->getById($id);
  51.         if (is_null($gift)) {
  52.             throw new NotFoundHttpException(sprintf('Gift not found'));
  53.         }
  54.         $form $this->giftFormFactory->createForm($gift);
  55.         $extraTypes $this->giftHasExtraProcessor->getExtraTypesByGift($gift);
  56.         $extraTypesDescription $this->extractExtraTypes($gift);
  57.         $form->handleRequest($request);
  58.         if ($form->isSubmitted() and $form->isValid()) {
  59.             $data $form->getData();
  60.             $shippingMethod $data[GiftType::SHIPPING_METHOD_FIELD_NAME];
  61.             //todo: Fix this no sense behavior
  62.             if ($shippingMethod == ShippingMethodsEnum::menorca()
  63.                 || $shippingMethod == ShippingMethodsEnum::shipping()
  64.                 || $shippingMethod == ShippingMethodsEnum::spain()
  65.                 || $shippingMethod == ShippingMethodsEnum::asturias()
  66.                 || $shippingMethod == ShippingMethodsEnum::express()
  67.                 || $shippingMethod == ShippingMethodsEnum::standard()
  68.             ) {
  69.                 $shippingMethod ShippingMethodsEnum::shipping();
  70.             }
  71.             /** @var AddressBillable $addressBillable */
  72.             $addressBillable $data['billing'];
  73.             /** @todo: check why addres billable line 2 is required */
  74.             if (!$addressBillable->getAddressLine2()) {
  75.                 $addressBillable->setAddressLine2("-");
  76.             }
  77.             $addressShippable $data[$shippingMethod instanceof ShippingMethodsEnum $shippingMethod->raw() : $shippingMethod];
  78.             $cart $this->giftToCartService->addToCart($form$addressBillable);
  79.             $this->cartShippingAssigner->assign($cart$addressShippable);
  80.             // redirect to redsys.
  81.             return new RedirectResponse($this->generateUrl('paymentsuite_redsys_execute', [
  82.                 '_locale' => $request->getLocale()
  83.             ]));
  84.         }
  85.         return $this->render(
  86.             'form/gift.html.twig',
  87.             [
  88.                 'form' => $form->createView(),
  89.                 'gift' => $gift,
  90.                 'extraTypes' => $extraTypes,
  91.                 'extraTypesDescription' => $extraTypesDescription,
  92.             ]
  93.         );
  94.     }
  95. }