<?php
declare(strict_types=1);
namespace Lnb\Shopware6\CheckoutCustomization\Decorator;
use Jlau\LoginAsCustomer\Controller\LoginAsCustomer;
use Shopware\Core\Checkout\Payment\PaymentMethodCollection;
use Shopware\Core\Checkout\Payment\SalesChannel\AbstractPaymentMethodRoute;
use Shopware\Core\Checkout\Payment\SalesChannel\PaymentMethodRouteResponse;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
use Shopware\Core\System\SalesChannel\Entity\SalesChannelRepositoryInterface;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
class PaymentMethodRouteDecorator extends AbstractPaymentMethodRoute
{
private SalesChannelRepositoryInterface $paymentMethodsRepository;
private AbstractPaymentMethodRoute $decorated;
private RequestStack $requestStack;
public function __construct(
RequestStack $requestStack,
SalesChannelRepositoryInterface $paymentMethodsRepository,
AbstractPaymentMethodRoute $decorated
) {
$this->requestStack = $requestStack;
$this->paymentMethodsRepository = $paymentMethodsRepository;
$this->decorated = $decorated;
}
public function getDecorated(): AbstractPaymentMethodRoute
{
return $this->decorated;
}
public function load(Request $request, SalesChannelContext $context, Criteria $criteria): PaymentMethodRouteResponse
{
if (!$this->isLoggedInAsCustomerSession()) {
return $this->decorated->load($request, $context, $criteria);
}
$criteria
->addFilter(new EqualsFilter('active', true))
->addSorting(new FieldSorting('position'))
->addAssociation('media');
$result = $this->paymentMethodsRepository->search($criteria, $context);
/** @var PaymentMethodCollection $paymentMethods */
$paymentMethods = $result->getEntities();
$result->assign([
'entities' => $paymentMethods,
'elements' => $paymentMethods,
'total' => $paymentMethods->count(),
]);
return new PaymentMethodRouteResponse($result);
}
private function isLoggedInAsCustomerSession(): bool
{
$request = $this->requestStack->getCurrentRequest();
if ($request === null) {
return false;
}
if (!$request->getSession()->has(LoginAsCustomer::SESSION_NAME)) {
return false;
}
$routeName = $request->attributes->get('_route');
if ($routeName !== 'frontend.account.edit-order.page' && $routeName !== 'frontend.account.edit-order.update-order') {
return false;
}
return true;
}
}