vendor/lnb/shopware6-checkout-customization/src/Decorator/PaymentMethodRouteDecorator.php line 75

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Lnb\Shopware6\CheckoutCustomization\Decorator;
  4. use Jlau\LoginAsCustomer\Controller\LoginAsCustomer;
  5. use Shopware\Core\Checkout\Payment\PaymentMethodCollection;
  6. use Shopware\Core\Checkout\Payment\SalesChannel\AbstractPaymentMethodRoute;
  7. use Shopware\Core\Checkout\Payment\SalesChannel\PaymentMethodRouteResponse;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
  11. use Shopware\Core\System\SalesChannel\Entity\SalesChannelRepositoryInterface;
  12. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\RequestStack;
  15. class PaymentMethodRouteDecorator extends AbstractPaymentMethodRoute
  16. {
  17.     private SalesChannelRepositoryInterface $paymentMethodsRepository;
  18.     private AbstractPaymentMethodRoute $decorated;
  19.     private RequestStack $requestStack;
  20.     public function __construct(
  21.         RequestStack $requestStack,
  22.         SalesChannelRepositoryInterface $paymentMethodsRepository,
  23.         AbstractPaymentMethodRoute $decorated
  24.     ) {
  25.         $this->requestStack $requestStack;
  26.         $this->paymentMethodsRepository $paymentMethodsRepository;
  27.         $this->decorated $decorated;
  28.     }
  29.     public function getDecorated(): AbstractPaymentMethodRoute
  30.     {
  31.         return $this->decorated;
  32.     }
  33.     public function load(Request $requestSalesChannelContext $contextCriteria $criteria): PaymentMethodRouteResponse
  34.     {
  35.         if (!$this->isLoggedInAsCustomerSession()) {
  36.             return $this->decorated->load($request$context$criteria);
  37.         }
  38.         $criteria
  39.             ->addFilter(new EqualsFilter('active'true))
  40.             ->addSorting(new FieldSorting('position'))
  41.             ->addAssociation('media');
  42.         $result $this->paymentMethodsRepository->search($criteria$context);
  43.         /** @var PaymentMethodCollection $paymentMethods */
  44.         $paymentMethods $result->getEntities();
  45.         $result->assign([
  46.             'entities' => $paymentMethods,
  47.             'elements' => $paymentMethods,
  48.             'total' => $paymentMethods->count(),
  49.         ]);
  50.         return new PaymentMethodRouteResponse($result);
  51.     }
  52.     private function isLoggedInAsCustomerSession(): bool
  53.     {
  54.         $request $this->requestStack->getCurrentRequest();
  55.         if ($request === null) {
  56.             return false;
  57.         }
  58.         if (!$request->getSession()->has(LoginAsCustomer::SESSION_NAME)) {
  59.             return false;
  60.         }
  61.         $routeName $request->attributes->get('_route');
  62.         if ($routeName !== 'frontend.account.edit-order.page' && $routeName !== 'frontend.account.edit-order.update-order') {
  63.             return false;
  64.         }
  65.         return true;
  66.     }
  67. }