|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Sylius\PayPalPlugin\Controller; |
| 6 | + |
| 7 | +use Doctrine\ORM\EntityManagerInterface; |
| 8 | +use Sylius\Bundle\OrderBundle\Controller\AddToCartCommandInterface; |
| 9 | +use Sylius\Bundle\OrderBundle\Factory\AddToCartCommandFactoryInterface; |
| 10 | +use Sylius\Bundle\ResourceBundle\Controller\NewResourceFactoryInterface; |
| 11 | +use Sylius\Bundle\ResourceBundle\Controller\RequestConfigurationFactoryInterface; |
| 12 | +use Sylius\Component\Order\Context\CartContextInterface; |
| 13 | +use Sylius\Component\Order\Model\OrderItemInterface; |
| 14 | +use Sylius\Component\Order\Modifier\OrderItemQuantityModifierInterface; |
| 15 | +use Sylius\Component\Order\Modifier\OrderModifierInterface; |
| 16 | +use Sylius\Component\Resource\Factory\FactoryInterface; |
| 17 | +use Sylius\Resource\Metadata\MetadataInterface; |
| 18 | +use Symfony\Component\Form\FormFactoryInterface; |
| 19 | +use Symfony\Component\HttpFoundation\RedirectResponse; |
| 20 | +use Symfony\Component\HttpFoundation\Request; |
| 21 | +use Symfony\Component\HttpFoundation\Response; |
| 22 | +use Symfony\Component\Routing\RouterInterface; |
| 23 | + |
| 24 | +final readonly class AddToCartAction |
| 25 | +{ |
| 26 | + public function __construct( |
| 27 | + private AddToCartCommandFactoryInterface $addToCartCommandFactory, |
| 28 | + private CartContextInterface $cartContext, |
| 29 | + private EntityManagerInterface $cartManager, |
| 30 | + private FactoryInterface $factory, |
| 31 | + private FormFactoryInterface $formFactory, |
| 32 | + private MetadataInterface $metadata, |
| 33 | + private NewResourceFactoryInterface $newResourceFactory, |
| 34 | + private OrderItemQuantityModifierInterface $quantityModifier, |
| 35 | + private OrderModifierInterface $orderModifier, |
| 36 | + private RequestConfigurationFactoryInterface $requestConfigurationFactory, |
| 37 | + private RouterInterface $router, |
| 38 | + ) { |
| 39 | + } |
| 40 | + |
| 41 | + public function __invoke(Request $request): Response |
| 42 | + { |
| 43 | + $cart = $this->cartContext->getCart(); |
| 44 | + $configuration = $this->requestConfigurationFactory->create($this->metadata, $request); |
| 45 | + |
| 46 | + /** @var OrderItemInterface $orderItem */ |
| 47 | + $orderItem = $this->newResourceFactory->create($configuration, $this->factory); |
| 48 | + |
| 49 | + $this->quantityModifier->modify($orderItem, 1); |
| 50 | + /** @var string $formType */ |
| 51 | + $formType = $configuration->getFormType(); |
| 52 | + |
| 53 | + $form = $this->formFactory->create( |
| 54 | + $formType, |
| 55 | + $this->addToCartCommandFactory->createWithCartAndCartItem($cart, $orderItem), |
| 56 | + $configuration->getFormOptions(), |
| 57 | + ); |
| 58 | + |
| 59 | + $form = $form->handleRequest($request); |
| 60 | + |
| 61 | + if ($form->isSubmitted() && !$form->isValid()) { |
| 62 | + return new RedirectResponse((string)$request->headers->get('referer')); |
| 63 | + } |
| 64 | + |
| 65 | + /** @var AddToCartCommandInterface $addToCartCommand */ |
| 66 | + $addToCartCommand = $form->getData(); |
| 67 | + |
| 68 | + $this->orderModifier->addToOrder($addToCartCommand->getCart(), $addToCartCommand->getCartItem()); |
| 69 | + |
| 70 | + $this->cartManager->persist($cart); |
| 71 | + $this->cartManager->flush(); |
| 72 | + |
| 73 | + return new RedirectResponse($this->router->generate('sylius_paypal_shop_create_paypal_order_from_cart', ['id' => $cart->getId()])); |
| 74 | + } |
| 75 | + |
| 76 | +} |
0 commit comments