<?php declare(strict_types=1);
namespace Appflix\Studygood\Storefront\Subscriber;
use Appflix\Studygood\Core\Service\VideoCourseService;
use Shopware\Core\Framework\Routing\Event\SalesChannelContextResolvedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class SalesChannelContextSubscriber implements EventSubscriberInterface
{
private VideoCourseService $videoCourseService;
public function __construct(
VideoCourseService $videoCourseService
)
{
$this->videoCourseService = $videoCourseService;
}
public static function getSubscribedEvents(): array
{
return [
SalesChannelContextResolvedEvent::class => 'onSalesChannelContextResolved'
];
}
public function onSalesChannelContextResolved(SalesChannelContextResolvedEvent $event): void
{
$customer = $event->getSalesChannelContext()->getCustomer();
if (!$customer) {
return;
}
$this->videoCourseService->setContext($event->getSalesChannelContext()->getContext());
$esdSubscriptions = $this->videoCourseService->getEsdSubscriptions($customer, $event->getSalesChannelContext());
if (!$esdSubscriptions) {
return;
}
$customer->assign([
'esdSubscriptions' => $esdSubscriptions
]);
}
}