src/EventSubscriber/ContractSubscriber.php line 263

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use ApiPlatform\Core\EventListener\EventPriorities;
  4. use App\Entity\Contract;
  5. use App\Message\SendNewContractNotification;
  6. use App\Message\SendUpdateContractOperatorNotification;
  7. use App\Message\SendUpdateContractItineraryNotification;
  8. use App\Message\SendUpdateContractStatusNotification;
  9. use App\Message\SendUpdateContractStatusCompletedNotification;
  10. use App\Message\SendNewReportedIncidentNotification;
  11. use App\Message\SendUpdateContractDemurrageStatusPaidNotification;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. use Symfony\Component\HttpFoundation\Request;
  14. // use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  15. use Symfony\Component\HttpKernel\Event\ViewEvent;
  16. use Symfony\Component\HttpKernel\KernelEvents;
  17. use Symfony\Component\Messenger\MessageBusInterface;
  18. // use App\Entity\User;
  19. // use App\Service\NotificationService;
  20. use Doctrine\ORM\EntityManagerInterface;
  21. // use Psr\Log\LoggerInterface;
  22. use Doctrine\Common\Collections\Collection;
  23. use Doctrine\ORM\UnitOfWork;
  24. final class ContractSubscriber implements EventSubscriberInterface
  25. {
  26.     public function __construct(
  27.         // TokenStorageInterface $tokenStorage,
  28.         private readonly MessageBusInterface $messageBus,
  29.         // private readonly NotificationService $notification // quitar
  30.         private readonly EntityManagerInterface $entityManager,
  31.         // private readonly LoggerInterface $logger,   // ðŸ‘ˆ añadido
  32.     ) {}
  33.     public static function getSubscribedEvents(): array
  34.     {
  35.         return [
  36.             KernelEvents::VIEW => [
  37.                 ['detectPropertyChange'EventPriorities::PRE_WRITE],
  38.                 ['sendNewContractNotification'EventPriorities::POST_WRITE],
  39.                 ['sendUpdateContractOperatorNotification'EventPriorities::POST_WRITE],
  40.                 ['sendUpdateContractItineraryNotification'EventPriorities::POST_WRITE],
  41.                 ['sendUpdateContractStatusNotification'EventPriorities::POST_WRITE],
  42.                 ['sendUpdateContractStatusCompletedNotification'EventPriorities::POST_WRITE],
  43.                 ['sendNewReportedIncidentNotification'EventPriorities::POST_WRITE],
  44.                 ['sendUpdateContractDemurrageStatusPaidNotification'EventPriorities::POST_WRITE],
  45.             ],
  46.         ];
  47.     }
  48.     public function detectPropertyChange(ViewEvent $event): void
  49.     {
  50.         $contract $event->getControllerResult();
  51.         $method $event->getRequest()->getMethod();
  52.         if (!$contract instanceof Contract || Request::METHOD_PUT !== $method) {
  53.             return;
  54.         }
  55.         $uow $this->entityManager->getUnitOfWork();
  56.         $originalData $uow->getOriginalEntityData($contract);
  57.         $oldOperator $originalData['operator']?->getId() ?? null;
  58.         $newOperator $contract->getOperator()?->getId();
  59.         if ($oldOperator !== $newOperator) {
  60.             $event->getRequest()->attributes->set('operator_changed'true);
  61.         }
  62.         $oldPortCalls $originalData['portCalls'] ?? null;
  63.         $newPortCalls $contract->getPortCalls();
  64.         if ($oldPortCalls instanceof Collection && $newPortCalls instanceof Collection) {
  65.             if ($this->collectionsAreDifferent($oldPortCalls$newPortCalls$uow)) {
  66.                 $event->getRequest()->attributes->set('portCalls_changed'true);
  67.             }
  68.         }
  69.         $oldStatus $originalData['status'] ?? null;
  70.         $newStatus $contract->getStatus();
  71.         if ($oldStatus !== $newStatus) {
  72.             $event->getRequest()->attributes->set('status_changed'true);
  73.         }
  74.         if ($newStatus === 'OPERATIONS_COMPLETED') {
  75.             $event->getRequest()->attributes->set('statusCompleted_changed'true);
  76.         }
  77.         $oldOperationReports $originalData['operationReports'] ?? null;
  78.         $newOperationReports $contract->getOperationReports();
  79.         if ($oldOperationReports instanceof Collection && $newOperationReports instanceof Collection) {
  80.             if ($this->collectionsAreDifferent($oldOperationReports$newOperationReports$uow)) {
  81.                 $event->getRequest()->attributes->set('operationReports_changed'true);
  82.             }
  83.         }
  84.         $oldDemurrageStatus $originalData['demurrageStatus'] ?? null;
  85.         $newDemurrageStatus $contract->getDemurrageStatus();
  86.         if ($oldDemurrageStatus !== $newDemurrageStatus) {
  87.             $event->getRequest()->attributes->set('demurrageStatus_changed'true);
  88.         }
  89.         if ($newDemurrageStatus === 'PAID') {
  90.             $event->getRequest()->attributes->set('demurrageStatusPaid_changed'true);
  91.         }
  92.         // $this->logger->info('___DEBUG___', [
  93.         //     'oldOperator' => $oldOperator,
  94.         //     'newOperator' => $newOperator,
  95.         //     'different' => $oldOperator !== $newOperator
  96.         // ]);
  97.         // $this->logger->info('___DEBUG___', [
  98.         //     'oldPortCalls' => $oldPortCalls,
  99.         //     'newPortCalls' => $newPortCalls,
  100.         //     'different' => $this->collectionsAreDifferent($oldPortCalls, $newPortCalls, $uow)
  101.         // ]);
  102.         // $this->logger->info('___DEBUG___', [
  103.         //     'oldStatus' => $oldStatus,
  104.         //     'newStatus' => $newStatus,
  105.         //     'different' => $oldStatus !== $newStatus
  106.         // ]);
  107.         // $this->logger->info('___DEBUG___', [
  108.         //     'oldReportedIncidents' => $oldReportedIncidents,
  109.         //     'newReportedIncidents' => $newReportedIncidents,
  110.         //     'different' => $this->collectionsAreDifferent($oldReportedIncidents, $newReportedIncidents, $uow)
  111.         // ]);
  112.         // $this->logger->info('___DEBUG___', [
  113.         //     'oldDemurrageStatus' => $oldDemurrageStatus,
  114.         //     'newDemurrageStatus' => $newDemurrageStatus,
  115.         //     'different' => $oldDemurrageStatus !== $newDemurrageStatus
  116.         // ]);
  117.     }
  118.     public function sendNewContractNotification(ViewEvent $event): void
  119.     {
  120.         $contract $event->getControllerResult();
  121.         $method $event->getRequest()->getMethod();
  122.         if (!$contract instanceof Contract || Request::METHOD_POST !== $method) {
  123.             return;
  124.         }
  125.         // $token = $this->tokenStorage->getToken();
  126.         // if (!$token) {
  127.         //     return;
  128.         // }
  129.         // $user = $token->getUser();
  130.         // if (!$user instanceof User) {
  131.         //     return;
  132.         // }
  133.         $message = new SendNewContractNotification();
  134.         $message->setContractId($contract->getId());
  135.         $this->messageBus->dispatch($message);
  136.         // $this->notification->sendNewContractNotification($contract->getId());
  137.     }
  138.     
  139.     public function sendUpdateContractOperatorNotification(ViewEvent $event): void
  140.     {
  141.         $contract $event->getControllerResult();
  142.         $method $event->getRequest()->getMethod();
  143.         if (!$contract instanceof Contract || Request::METHOD_PUT !== $method) {
  144.             return;
  145.         }
  146.         // $token = $this->tokenStorage->getToken();
  147.         // if (!$token) {
  148.         //     return;
  149.         // }
  150.         // $user = $token->getUser();
  151.         // if (!$user instanceof User) {
  152.         //     return;
  153.         // }
  154.         // $this->logger->info('___DEBUG___', [
  155.         //     'operator_changed' => $event->getRequest()->attributes->get('operator_changed', false)
  156.         // ]);
  157.         if (!$event->getRequest()->attributes->get('operator_changed'false)) {
  158.             return;
  159.         }
  160.         $message = new SendUpdateContractOperatorNotification();
  161.         $message->setContractId($contract->getId());
  162.         $this->messageBus->dispatch($message);
  163.         // $this->notification->sendUpdateContractOperatorNotification($contract->getId());
  164.     }
  165.     public function sendUpdateContractItineraryNotification(ViewEvent $event): void
  166.     {
  167.         $contract $event->getControllerResult();
  168.         $method $event->getRequest()->getMethod();
  169.         if (!$contract instanceof Contract || Request::METHOD_PUT !== $method) {
  170.             return;
  171.         }
  172.         // $token = $this->tokenStorage->getToken();
  173.         // if (!$token) {
  174.         //     return;
  175.         // }
  176.         // $user = $token->getUser();
  177.         // if (!$user instanceof User) {
  178.         //     return;
  179.         // }
  180.         // $this->logger->info('___DEBUG___', [
  181.         //     'portCalls_changed' => $event->getRequest()->attributes->get('portCalls_changed', false)
  182.         // ]);
  183.         if (!$event->getRequest()->attributes->get('portCalls_changed'false)) {
  184.             return;
  185.         }
  186.         $message = new SendUpdateContractItineraryNotification();
  187.         $message->setContractId($contract->getId());
  188.         $this->messageBus->dispatch($message);
  189.         // $this->notification->sendUpdateContractItineraryNotification($contract->getId());
  190.     }
  191.     public function sendUpdateContractStatusNotification(ViewEvent $event): void
  192.     {
  193.         $contract $event->getControllerResult();
  194.         $method $event->getRequest()->getMethod();
  195.         if (!$contract instanceof Contract || Request::METHOD_PUT !== $method) {
  196.             return;
  197.         }
  198.         // $token = $this->tokenStorage->getToken();
  199.         // if (!$token) {
  200.         //     return;
  201.         // }
  202.         // $user = $token->getUser();
  203.         // if (!$user instanceof User) {
  204.         //     return;
  205.         // }
  206.         // $this->logger->info('___DEBUG___', [
  207.         //     'status_changed' => $event->getRequest()->attributes->get('status_changed', false)
  208.         // ]);
  209.         if (!$event->getRequest()->attributes->get('status_changed'false)) {
  210.             return;
  211.         }
  212.         $message = new SendUpdateContractStatusNotification;
  213.         $message->setContractId($contract->getId());
  214.         $this->messageBus->dispatch($message);
  215.         // $this->notification->sendUpdateContractStatusNotification($contract->getId());
  216.     }
  217.     public function sendUpdateContractStatusCompletedNotification(ViewEvent $event): void
  218.     {
  219.         $contract $event->getControllerResult();
  220.         $method $event->getRequest()->getMethod();
  221.         if (!$contract instanceof Contract || Request::METHOD_PUT !== $method) {
  222.             return;
  223.         }
  224.         // $token = $this->tokenStorage->getToken();
  225.         // if (!$token) {
  226.         //     return;
  227.         // }
  228.         // $user = $token->getUser();
  229.         // if (!$user instanceof User) {
  230.         //     return;
  231.         // }
  232.         // $this->logger->info('___DEBUG___', [
  233.         //     'statusCompleted_changed' => $event->getRequest()->attributes->get('statusCompleted_changed', false)
  234.         // ]);
  235.         if (!$event->getRequest()->attributes->get('statusCompleted_changed'false)) {
  236.             return;
  237.         }
  238.         $message = new SendUpdateContractStatusCompletedNotification;
  239.         $message->setContractId($contract->getId());
  240.         $this->messageBus->dispatch($message);
  241.         // $this->notification->sendUpdateContractStatusCompletedNotification($contract->getId());
  242.     }
  243.     public function sendNewReportedIncidentNotification(ViewEvent $event): void
  244.     {
  245.         $contract $event->getControllerResult();
  246.         $method $event->getRequest()->getMethod();
  247.         if (!$contract instanceof Contract || Request::METHOD_PUT !== $method) {
  248.             return;
  249.         }
  250.         // $token = $this->tokenStorage->getToken();
  251.         // if (!$token) {
  252.         //     return;
  253.         // }
  254.         // $user = $token->getUser();
  255.         // if (!$user instanceof User) {
  256.         //     return;
  257.         // }
  258.         // $this->logger->info('___DEBUG___', [
  259.         //     'reportedIncidents_changed' => $event->getRequest()->attributes->get('reportedIncidents_changed', false)
  260.         // ]);
  261.         if (!$event->getRequest()->attributes->get('operationReports_changed'false)) {
  262.             return;
  263.         }
  264.         $message = new SendNewReportedIncidentNotification();
  265.         $message->setContractId($contract->getId());
  266.         $this->messageBus->dispatch($message);
  267.         // $this->notification->sendNewReportedIncidentNotification($contract->getId());
  268.     }
  269.     public function sendUpdateContractDemurrageStatusPaidNotification(ViewEvent $event): void
  270.     {
  271.         $contract $event->getControllerResult();
  272.         $method $event->getRequest()->getMethod();
  273.         if (!$contract instanceof Contract || Request::METHOD_PUT !== $method) {
  274.             return;
  275.         }
  276.         // $token = $this->tokenStorage->getToken();
  277.         // if (!$token) {
  278.         //     return;
  279.         // }
  280.         // $user = $token->getUser();
  281.         // if (!$user instanceof User) {
  282.         //     return;
  283.         // }
  284.         // $this->logger->info('___DEBUG___', [
  285.         //     'demurrageStatusPaid_changed' => $event->getRequest()->attributes->get('demurrageStatusPaid_changed', false)
  286.         // ]);
  287.         if (!$event->getRequest()->attributes->get('demurrageStatusPaid_changed'false)) {
  288.             return;
  289.         }
  290.         $message = new SendUpdateContractDemurrageStatusPaidNotification;
  291.         $message->setContractId($contract->getId());
  292.         $this->messageBus->dispatch($message);
  293.         // $this->notification->sendUpdateContractDemurrageStatusPaidNotification($contract->getId());
  294.     }
  295.     private function collectionsAreDifferent(Collection $oldCollection $newUnitOfWork $uow): bool
  296.     {
  297.         $uow->computeChangeSets();
  298.         $oldIds array_map(fn($entity) => $entity->getId(), $old->toArray());
  299.         $newIds array_map(fn($entity) => $entity->getId(), $new->toArray());
  300.         sort($oldIds);
  301.         sort($newIds);
  302.         if ($oldIds !== $newIds) {
  303.             return true;
  304.         }
  305.         foreach ($new as $entity) {
  306.             $changes $uow->getEntityChangeSet($entity);
  307.             if (!empty($changes)) {
  308.                 return true;
  309.             }
  310.         }
  311.         return false;
  312.     }
  313. }