<?php
namespace App\Events;
use App\Entity\CommunityPost;
use App\Entity\InvoiceWater;
use App\Message\CommunityPostNotification;
use ReflectionException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Event\ViewEvent;
use ApiPlatform\Core\EventListener\EventPriorities;
use App\Entity\CommunityServiceResponse;
use App\Entity\UserVoteInterface;
use App\Services\DompdfService;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Serializer\Exception\ExceptionInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Serializer\SerializerInterface;
class CommunityPostAlertSubscriber implements EventSubscriberInterface
{
/**
* @var MessageBusInterface
*/
private $messageBus;
/**
* @var NormalizerInterface
*/
private $normalizer;
/**
* @var DompdfService
*/
private $dompdf;
public function __construct(
MessageBusInterface $messageBus,
NormalizerInterface $normalizer,
DompdfService $dompdf
) {
$this->messageBus = $messageBus;
$this->normalizer = $normalizer;
$this->dompdf = $dompdf;
}
/**
* @param ViewEvent $event
* @return void
* @throws ReflectionException
* @throws ExceptionInterface
*/
public function alert(ViewEvent $event)
{
$object = $event->getControllerResult();
$request = $event->getRequest();
if (!$object || is_array($object) || !$event->getRequest()->isMethod(Request::METHOD_POST)) {
return;
}
if ($object instanceof CommunityPost) {
if ($object->getPlp() !== null) {
$this->dompdf->createPdfPlp($object->getPlp());
}
if ($object->getType() === 'Solicitation') {
$urlCommunity = $request->request->get("urlCommunity");
$designId = $request->request->get('designId');
$urlSite = $request->request->get('urlSite');
$extData = ['urlCommunity' => $urlCommunity, 'designId' => $designId, 'urlSite' => $urlSite];
$this->messageBus->dispatch(new CommunityPostNotification($object->getId(), $extData));
} else {
$this->messageBus->dispatch(new CommunityPostNotification($object->getId()));
}
}
if ((new \ReflectionClass($object))->implementsInterface(UserVoteInterface::class)) {
$data["post"] = $object->getPost()->getId();
if (method_exists($object, 'getVotes')) {
$data['votes'] = $object->getVotes();
if(method_exists($object, 'getUserVote')) {
$data['votes']['userVote'] = $object->getUserVote();
}
} else {
$data["votes"] = [
"userVote" => $object->getIsFor(),
"agree" => $object->getPost()->getAgree(),
"disagree" => $object->getPost()->getDisagree(),
];
}
$event->setResponse(new JsonResponse($data));
}
if ($object instanceof CommunityServiceResponse) {
$data = $this->normalizer->normalize($object->getPost(), "json", [
"groups" => "community:post:read"
]);
$event->setResponse(new JsonResponse($data));
}
}
/**
* @return array<string, mixed>
*/
public static function getSubscribedEvents()
{
return [
KernelEvents::VIEW => ['alert', EventPriorities::POST_WRITE],
];
}
}