<?php declare(strict_types=1);
namespace TemplaidInvoices\Subscriber;
use CoeWishlistSw6\Core\Content\Wishlist\Aggregate\WishlistNoteEntity;
use CoeWishlistSw6\Storefront\Page\Wishlist\WishlistPageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use TemplaidInvoices\Core\Content\Wishlist\WishlistItemEntity;
use TemplaidInvoices\Service\WishlistService;
class PageSubscriber implements EventSubscriberInterface
{
private WishlistService $wishlistService;
public function __construct(
WishlistService $wishlistService
)
{
$this->wishlistService = $wishlistService;
}
/**
* @return array|string[]
*/
public static function getSubscribedEvents()
{
return [
WishlistPageLoadedEvent::class => 'onWishlistPageLoaded'
];
}
/**
* @param WishlistPageLoadedEvent $event
*/
public function onWishlistPageLoaded(WishlistPageLoadedEvent $event): void
{
if (!$event->getSalesChannelContext()->getCustomer()) {
return;
}
$sortedItems = $this->wishlistService->getSortedWishlistItems($event->getSalesChannelContext()->getCustomerId(), $event->getContext());
$sortedIds = array_values($sortedItems->map(function (WishlistItemEntity $item) {
return $item->getLineId();
}));
$sortedNotes = $event->getPage()->getActiveList()->getNotes();
$sortedNotes->sort(function (WishlistNoteEntity $a, WishlistNoteEntity $b) use ($sortedIds) {
$positionA = array_search($a->getId(), $sortedIds) ?: 0;
$positionB = array_search($b->getId(), $sortedIds) ?: 0;
return $positionA <=> $positionB;
});
}
}