src/Events/CommunityPostAlertSubscriber.php line 57

Open in your IDE?
  1. <?php
  2. namespace App\Events;
  3. use App\Entity\CommunityPost;
  4. use App\Entity\InvoiceWater;
  5. use App\Message\CommunityPostNotification;
  6. use ReflectionException;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpKernel\KernelEvents;
  9. use Symfony\Component\HttpKernel\Event\ViewEvent;
  10. use ApiPlatform\Core\EventListener\EventPriorities;
  11. use App\Entity\CommunityServiceResponse;
  12. use App\Entity\UserVoteInterface;
  13. use App\Services\DompdfService;
  14. use Symfony\Component\Messenger\MessageBusInterface;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. use Symfony\Component\HttpFoundation\JsonResponse;
  17. use Symfony\Component\HttpFoundation\Response;
  18. use Symfony\Component\Serializer\Exception\ExceptionInterface;
  19. use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
  20. use Symfony\Component\Serializer\SerializerInterface;
  21. class CommunityPostAlertSubscriber implements EventSubscriberInterface
  22. {
  23.     /**
  24.      * @var MessageBusInterface
  25.      */
  26.     private $messageBus;
  27.     /**
  28.      * @var NormalizerInterface
  29.      */
  30.     private $normalizer;
  31.     /**
  32.      * @var DompdfService
  33.      */
  34.     private $dompdf;
  35.     public function __construct(
  36.         MessageBusInterface $messageBus,
  37.         NormalizerInterface $normalizer,
  38.         DompdfService $dompdf
  39.     ) {
  40.         $this->messageBus $messageBus;
  41.         $this->normalizer $normalizer;
  42.         $this->dompdf $dompdf;
  43.     }
  44.     /**
  45.      * @param ViewEvent $event
  46.      * @return void
  47.      * @throws ReflectionException
  48.      * @throws ExceptionInterface
  49.      */
  50.     public function alert(ViewEvent $event)
  51.     {
  52.         $object  $event->getControllerResult();
  53.         $request $event->getRequest();
  54.         if (!$object || is_array($object) || !$event->getRequest()->isMethod(Request::METHOD_POST)) {
  55.             return;
  56.         }
  57.         if ($object instanceof CommunityPost) {
  58.             if ($object->getPlp() !== null) {
  59.                 $this->dompdf->createPdfPlp($object->getPlp());
  60.             }
  61.             if ($object->getType() === 'Solicitation') {
  62.                 $urlCommunity $request->request->get("urlCommunity");
  63.                 $designId $request->request->get('designId');
  64.                 $urlSite $request->request->get('urlSite');
  65.                 $extData = ['urlCommunity' => $urlCommunity'designId' => $designId'urlSite' => $urlSite];
  66.                 $this->messageBus->dispatch(new CommunityPostNotification($object->getId(), $extData));
  67.             } else {
  68.                 $this->messageBus->dispatch(new CommunityPostNotification($object->getId()));
  69.             }
  70.         }
  71.         if ((new \ReflectionClass($object))->implementsInterface(UserVoteInterface::class)) {
  72.             $data["post"] = $object->getPost()->getId();
  73.             if (method_exists($object'getVotes')) {
  74.                 $data['votes'] = $object->getVotes();
  75.                 if(method_exists($object'getUserVote')) {
  76.                     $data['votes']['userVote'] = $object->getUserVote();
  77.                 }
  78.             } else {
  79.                 $data["votes"] = [
  80.                     "userVote" => $object->getIsFor(),
  81.                     "agree" => $object->getPost()->getAgree(),
  82.                     "disagree" => $object->getPost()->getDisagree(),
  83.                 ];
  84.             }
  85.             $event->setResponse(new JsonResponse($data));
  86.         }
  87.         if ($object instanceof CommunityServiceResponse) {
  88.             $data $this->normalizer->normalize($object->getPost(), "json", [
  89.                 "groups" => "community:post:read"
  90.             ]);
  91.             $event->setResponse(new JsonResponse($data));
  92.         }
  93.     }
  94.     /**
  95.      * @return array<string, mixed>
  96.      */
  97.     public static function getSubscribedEvents()
  98.     {
  99.         return [
  100.             KernelEvents::VIEW => ['alert'EventPriorities::POST_WRITE],
  101.         ];
  102.     }
  103. }