<?php declare(strict_types=1);
namespace Nd\Srs\Subscriber;
use Nd\Srs\Service\PointsCommunicator;
use Shopware\Core\Framework\Struct\ArrayStruct;
use Shopware\Storefront\Page\Account\Overview\AccountOverviewPageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class AccountPageSubscriber implements EventSubscriberInterface
{
private PointsCommunicator $pointsCommunicator;
public function __construct(PointsCommunicator $pointsCommunicator)
{
$this->pointsCommunicator = $pointsCommunicator;
}
public static function getSubscribedEvents(): array
{
return [
AccountOverviewPageLoadedEvent::class => ['onAccountOverviewPageLoadedEvent', 1000]
];
}
public function onAccountOverviewPageLoadedEvent($event)
{
$customer = $event->getPage()->getCustomer();
if (!$customer) {
return;
}
$points = $this->pointsCommunicator->retrievePoints($customer, $event->getContext());
$page = $event->getPage();
$page->addExtension('srs_points', new ArrayStruct(['points' => $points]));
}
}