<?php
namespace CioBudget\Subscriber;
use CioBudget\Service\SessionService;
use Shopware\Core\Checkout\Customer\Event\CustomerLoginEvent;
use Shopware\Core\Checkout\Customer\Event\CustomerLogoutEvent;
use Shopware\Core\System\SalesChannel\Context\SalesChannelContextPersister;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class RegisterBudgetInSession implements EventSubscriberInterface
{
private SessionService $sessionService;
private SalesChannelContextPersister $salesChannelContextPersister;
public function setSessionService(SessionService $sessionService, SalesChannelContextPersister $salesChannelContextPersister)
{
$this->sessionService = $sessionService;
$this->salesChannelContextPersister = $salesChannelContextPersister;
}
public static function getSubscribedEvents(): array
{
return [
CustomerLoginEvent::class => 'onLogin',
CustomerLogoutEvent::class => 'onLogout'
];
}
public function onLogin(CustomerLoginEvent $event)
{
$customer = $event->getCustomer();
$this->sessionService->setCustomerBudgetInSession($customer, $event->getSalesChannelContext());
}
public function onLogout(CustomerLogoutEvent $event)
{
$this->sessionService->removeOldActiveBudget();
}
}