custom/plugins/EasyCreditRatenkauf/src/Subscriber/Redirector.php line 112

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * (c) NETZKOLLEKTIV GmbH <kontakt@netzkollektiv.com>
  5.  * For the full copyright and license information, please view the LICENSE
  6.  * file that was distributed with this source code.
  7.  */
  8. namespace Netzkollektiv\EasyCredit\Subscriber;
  9. use Symfony\Component\DependencyInjection\ContainerInterface;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Symfony\Component\HttpFoundation\RedirectResponse;
  12. use Symfony\Component\HttpFoundation\RequestStack;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  15. use Symfony\Component\HttpKernel\KernelEvents;
  16. use Shopware\Core\System\SalesChannel\Event\SalesChannelContextSwitchEvent;
  17. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
  18. use Netzkollektiv\EasyCredit\Api\Storage;
  19. use Netzkollektiv\EasyCredit\Helper\Payment as PaymentHelper;
  20. use Netzkollektiv\EasyCredit\Service\CheckoutService;
  21. use Netzkollektiv\EasyCredit\Helper\Quote as QuoteHelper;
  22. class Redirector implements EventSubscriberInterface
  23. {
  24.     private ContainerInterface $container;
  25.     private Request $request;
  26.     private PaymentHelper $paymentHelper;
  27.     private QuoteHelper $quoteHelper;
  28.     private CheckoutService $checkoutService;
  29.     private Storage $storage;
  30.     public function __construct(
  31.         ContainerInterface $container,
  32.         RequestStack $requestStack,
  33.         PaymentHelper $paymentHelper,
  34.         QuoteHelper $quoteHelper,
  35.         CheckoutService $checkoutService,
  36.         Storage $storage
  37.     ) {
  38.         $this->container $container;
  39.         $this->request $requestStack->getCurrentRequest();
  40.         $this->paymentHelper $paymentHelper;
  41.         $this->quoteHelper $quoteHelper;
  42.         $this->checkoutService $checkoutService;
  43.         $this->storage $storage;
  44.     }
  45.     public static function getSubscribedEvents(): array
  46.     {
  47.         return [
  48.             SalesChannelContextSwitchEvent::class => 'onSalesChannelContextSwitch',
  49.             CheckoutConfirmPageLoadedEvent::class => 'onCheckoutConfirmLoaded',
  50.             KernelEvents::RESPONSE => 'onKernelResponse',
  51.         ];
  52.     }
  53.     public function onSalesChannelContextSwitch(SalesChannelContextSwitchEvent $event): void
  54.     {
  55.         $salesChannelContext $event->getSalesChannelContext();
  56.         if (!$this->isRoute('frontend.checkout.configure'$this->request)) {
  57.             return;
  58.         }
  59.         if (
  60.             !$event->getRequestDataBag()->get('paymentMethodId')
  61.             || !$this->paymentHelper->isSelected($salesChannelContext$event->getRequestDataBag()->get('paymentMethodId'))
  62.         ) {
  63.             return;
  64.         }
  65.         if (
  66.             \version_compare($this->container->getParameter('kernel.shopware_version'), '6.4.0''>=')
  67.             && !$event->getRequestDataBag()->get('easycredit')
  68.         ) {
  69.             return;
  70.         }
  71.         $this->storage
  72.             ->set('express'false)
  73.             ->set('duration'$event->getRequestDataBag()->get('easycredit')->get('number-of-installments'))
  74.             ->set('init'true)
  75.             ->persist();
  76.     }
  77.     public function onCheckoutConfirmLoaded(CheckoutConfirmPageLoadedEvent $event): void
  78.     {
  79.         if (!$this->storage->get('init')) {
  80.             return;
  81.         }
  82.         $salesChannelContext $event->getSalesChannelContext();
  83.         $cart $event->getPage()->getCart();
  84.         $this->storage->set('init'false);
  85.         $this->storage->set('cartToken'$cart->getToken());
  86.         $this->checkoutService->startCheckout(
  87.             $salesChannelContext,
  88.             $this->quoteHelper->getQuote($salesChannelContext$cart)
  89.         );
  90.     }
  91.     public function onKernelResponse(ResponseEvent $event): void
  92.     {
  93.         if (
  94.             !$this->request->hasSession() ||
  95.             $this->request->attributes->get('_routeScope') === ['store-api']
  96.         ) {
  97.             return; // do not run in CLI & API
  98.         }
  99.         if ($redirectUrl $this->storage->get('redirect_url')) {
  100.             $event->setResponse(new RedirectResponse($redirectUrl));
  101.             $this->storage->set('redirect_url'null)->persist();
  102.         }
  103.     }
  104.     protected function isRoute($route$request)
  105.     {
  106.         $attributes = (isset($request->attributes)) ? $request->attributes null;
  107.         if (
  108.             $attributes === null
  109.             || $attributes->get('_route') !== $route
  110.         ) {
  111.             return false;
  112.         }
  113.         return true;
  114.     }
  115. }