<?php
namespace App\Auction\EventSubscriber;
use App\Auction\Event\AuctionAttendEvent;
use App\Auction\Event\AuctionEvents;
use App\Profile\Model\Profile;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Mercure\HubInterface;
use Symfony\Component\Mercure\Update;
class AuctionAttendeesSubscriber implements EventSubscriberInterface
{
public function __construct(
protected HubInterface $hub,
protected \Twig\Environment $twig
) {
}
public static function getSubscribedEvents()
{
return [
AuctionEvents::ATTEND => 'updateAuctionAttendees',
AuctionEvents::UNATTEND => 'updateAuctionAttendees',
];
}
public function updateAuctionAttendees(AuctionAttendEvent $event)
{
$attendees = $event->auction->getAttendees();
$attendeeProfiles = [];
foreach ($attendees as $attendee) {
$attendeeProfiles[] = Profile::getById($attendee->getObjectId());
}
$sortedAttendeeProfile = [];
foreach ($attendeeProfiles as $profile) {
$key = explode('\\', $profile->getPrimaryProfile());
$key = end($key);
$key = str_replace('Profile', '', $key);
$key = mb_strtolower($key);
$sortedAttendeeProfile[$key][] = $profile;
usort($sortedAttendeeProfile[$key], fn ($a, $b) => strcmp($a->getName(), $b->getName()));
}
$html = $this->twig->render('network/auction/event/attend.stream.twig', ['event' => $event->auction, 'attendees' => $sortedAttendeeProfile]);
$this->hub->publish(new Update($event->getTopic(), $html));
}
}