<?php
namespace App\Controller\Ecommerce;
use App\Assigner\Ecommerce\Interfaces\CartShippingAssignerInterface;
use App\Controller\Ecommerce\Traits\ExtractsExtraTypesFromGift;
use App\Entity\Ecommerce\AddressBillable;
use App\Entity\Ecommerce\Gift;
use App\Enum\Ecommerce\ShippingMethodsEnum;
use App\Factory\Ecommerce\GiftFormFactory;
use App\Factory\Ecommerce\Interfaces\GiftFormFactoryInterface;
use App\Form\Ecommerce\GiftType;
use App\Processor\Ecommerce\Interfaces\GiftHasExtraProcessorInterface;
use App\Repository\Ecommerce\Interfaces\GiftRepositoryInterface;
use App\Service\Ecommerce\GiftToCartService;
use App\Util\Interfaces\UrlGeneratorAwareInterface;
use App\Util\Traits\UrlGeneratorAwareTrait;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use WAM\Bundle\CoreBundle\Controller\Traits\RendererControllerTrait;
use WAM\Bundle\CoreBundle\Util\Interfaces\RendererAwareInterface;
/**
* @author Gerard Rico <grico@wearemarketing.com>
*/
class GiftCheckoutController implements RendererAwareInterface, UrlGeneratorAwareInterface
{
use RendererControllerTrait;
use UrlGeneratorAwareTrait;
use ExtractsExtraTypesFromGift;
private GiftRepositoryInterface $giftRepository;
private GiftFormFactory $giftFormFactory;
private GiftHasExtraProcessorInterface $giftHasExtraProcessor;
private GiftToCartService $giftToCartService;
private CartShippingAssignerInterface $cartShippingAssigner;
public function __construct(
GiftRepositoryInterface $giftRepository,
GiftFormFactory $giftFormFactory,
GiftHasExtraProcessorInterface $giftHasExtraProcessor,
GiftToCartService $giftToCartService,
CartShippingAssignerInterface $cartShippingAssigner
) {
$this->giftRepository = $giftRepository;
$this->giftFormFactory = $giftFormFactory;
$this->giftHasExtraProcessor = $giftHasExtraProcessor;
$this->giftToCartService = $giftToCartService;
$this->cartShippingAssigner = $cartShippingAssigner;
}
public function __invoke(Request $request, int $id)
{
/** @var Gift $gift */
$gift = $this->giftRepository->getById($id);
if (is_null($gift)) {
throw new NotFoundHttpException(sprintf('Gift not found'));
}
$form = $this->giftFormFactory->createForm($gift);
$extraTypes = $this->giftHasExtraProcessor->getExtraTypesByGift($gift);
$extraTypesDescription = $this->extractExtraTypes($gift);
$form->handleRequest($request);
if ($form->isSubmitted() and $form->isValid()) {
$data = $form->getData();
$shippingMethod = $data[GiftType::SHIPPING_METHOD_FIELD_NAME];
//todo: Fix this no sense behavior
if ($shippingMethod == ShippingMethodsEnum::menorca()
|| $shippingMethod == ShippingMethodsEnum::shipping()
|| $shippingMethod == ShippingMethodsEnum::spain()
|| $shippingMethod == ShippingMethodsEnum::asturias()
|| $shippingMethod == ShippingMethodsEnum::express()
|| $shippingMethod == ShippingMethodsEnum::standard()
) {
$shippingMethod = ShippingMethodsEnum::shipping();
}
/** @var AddressBillable $addressBillable */
$addressBillable = $data['billing'];
/** @todo: check why addres billable line 2 is required */
if (!$addressBillable->getAddressLine2()) {
$addressBillable->setAddressLine2("-");
}
$addressShippable = $data[$shippingMethod instanceof ShippingMethodsEnum ? $shippingMethod->raw() : $shippingMethod];
$cart = $this->giftToCartService->addToCart($form, $addressBillable);
$this->cartShippingAssigner->assign($cart, $addressShippable);
// redirect to redsys.
return new RedirectResponse($this->generateUrl('paymentsuite_redsys_execute', [
'_locale' => $request->getLocale()
]));
}
return $this->render(
'form/gift.html.twig',
[
'form' => $form->createView(),
'gift' => $gift,
'extraTypes' => $extraTypes,
'extraTypesDescription' => $extraTypesDescription,
]
);
}
}