src/EventSubscriber/CommunityPostAlertSubscriber.php line 54

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