<?php
namespace B2bSellersCore\Components\ExpressCheckout\Subscriber;
use B2bSellersCore\Components\B2bPlatform\Traits\B2bContextTrait;
use B2bSellersCore\Components\ExpressCheckout\SalesChannel\AbstractExpressCheckoutSettingRoute;
use LogicException;
use Shopware\Core\Framework\Adapter\Translation\AbstractTranslator;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Shopware\Storefront\Page\Checkout\Cart\CheckoutCartPageLoadedEvent;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Exception\SessionNotFoundException;
use Symfony\Component\HttpFoundation\Request;
class CheckoutSubscriber implements EventSubscriberInterface
{
use B2bContextTrait;
private EntityRepositoryInterface $expressCheckoutSettingRepository;
private ContainerInterface $container;
private AbstractTranslator $translator;
private SystemConfigService $configService;
private AbstractExpressCheckoutSettingRoute $expressCheckoutSettingRoute;
public function __construct(
EntityRepositoryInterface $expressCheckoutSettingRepository,
ContainerInterface $container,
AbstractTranslator $translator,
SystemConfigService $configService,
AbstractExpressCheckoutSettingRoute $expressCheckoutSettingRoute)
{
$this->expressCheckoutSettingRepository = $expressCheckoutSettingRepository;
$this->container = $container;
$this->translator = $translator;
$this->configService = $configService;
$this->expressCheckoutSettingRoute = $expressCheckoutSettingRoute;
}
public static function getSubscribedEvents(): array
{
return [
CheckoutCartPageLoadedEvent::class => 'checkExpressCheckoutSetting'
];
}
public function checkExpressCheckoutSetting($event): void
{
$customer = $event->getSalesChannelContext()->getCustomer();
if ($this->hasB2bPlatformContext($event->getSalesChannelContext()) && $customer) {
if (!$this->configService->get('B2bSellersCore.config.enableExpressCheckout', $event->getSalesChannelContext()->getSalesChannelId())) {
return;
}
$expressCheckoutSetting = $this->expressCheckoutSettingRoute->get(new Request(), $event->getSalesChannelContext())->getExpressCheckoutSetting();
if ($expressCheckoutSetting === null) {
$this->addFlash('info', $this->translator->trans('b2bPlatform.expressCheckout.infoMessage'));
}
if ($expressCheckoutSetting !== null && ($expressCheckoutSetting->getBillingAddress() === null || $expressCheckoutSetting->getShippingAddress() === null || $expressCheckoutSetting->getPaymentMethod() === null)) {
$this->addFlash('info', $this->translator->trans('b2bPlatform.expressCheckout.infoMessage'));
}
}
}
/**
* Adds a flash message to the current session for type.
*
* @throws LogicException
*/
protected function addFlash(string $type, $message): void
{
try {
$this->container->get('session')->getFlashBag()->add($type, $message);
} catch (SessionNotFoundException $e) {
throw new LogicException('You can not use the addFlash method if sessions are disabled. Enable them in "config/packages/framework.yaml".', 0, $e);
}
}
}