src/EventSubscriber/Doctrine/LetterSubscriber.php line 168

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber\Doctrine;
  3. use App\Enum\LetterStatusEnum;
  4. use App\Message\OCRIndexationMessage;
  5. use App\Service\LetterFilesService;
  6. use App\Service\SegmentAPI;
  7. use App\Traits\SentryNotifyTrait;
  8. use Doctrine\Common\EventSubscriber;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. use Doctrine\Persistence\Event\LifecycleEventArgs;
  11. use Evo\Infrastructure\MappingORM\Letter;
  12. use Evo\Infrastructure\MappingORM\Organization;
  13. use Symfony\Component\Messenger\MessageBusInterface;
  14. use Symfony\Contracts\HttpClient\HttpClientInterface;
  15. class LetterSubscriber implements EventSubscriber
  16. {
  17.     use SentryNotifyTrait;
  18.     private LetterFilesService $letterFilesService;
  19.     private SegmentAPI $segmentAPI;
  20.     private HttpClientInterface $client;
  21.     private EntityManagerInterface $em;
  22.     private MessageBusInterface $bus;
  23.     public function __construct(
  24.         LetterFilesService $letterFilesService,
  25.         SegmentAPI $segmentAPI,
  26.         HttpClientInterface $client,
  27.         EntityManagerInterface $em,
  28.         MessageBusInterface $bus
  29.     ) {
  30.         $this->letterFilesService $letterFilesService;
  31.         $this->segmentAPI $segmentAPI;
  32.         $this->client $client;
  33.         $this->em $em;
  34.         $this->bus $bus;
  35.     }
  36.     /**
  37.      * Returns an array of events this subscriber wants to listen to.
  38.      *
  39.      * @return string[]
  40.      */
  41.     public function getSubscribedEvents()
  42.     {
  43.         return [
  44.             'postPersist',
  45.             'postUpdate',
  46.             'preRemove',
  47.         ];
  48.     }
  49.     public function postPersist(LifecycleEventArgs $args)
  50.     {
  51.         $entity $args->getObject();
  52.         if (!$entity instanceof Letter) {
  53.             return;
  54.         }
  55.         $this->sendLetterToAws($entity$args);
  56.         if (LetterStatusEnum::TO_BE_SENT === $entity->getStatus()) {
  57.             $organization $entity->getOrganization();
  58.             if (null !== $organization) {
  59.                 $organization->setIsEmptybal(false);
  60.                 $args->getObjectManager()->persist($organization);
  61.                 $args->getObjectManager()->flush();
  62.             }
  63.         }
  64.         $organization $entity->getOrganization();
  65.         if ($organization && !empty($organization->getZapierHookUrl())) {
  66.             $params = [
  67.                 'type' => $entity->getType(),
  68.                 'organization' => $organization,
  69.                 'letterId' => $entity->getId(),
  70.             ];
  71.             foreach ($organization->getZapierHookUrl() as $zapierHookUrl) {
  72.                 if (isset($zapierHookUrl['letter']) && 'letter' === array_keys($zapierHookUrl)[0]) {
  73.                     $response $this->client->request(
  74.                         'POST',
  75.                         $zapierHookUrl['letter'],
  76.                         [
  77.                             'body' => $params,
  78.                         ]
  79.                     );
  80.                     if (410 === $response->getStatusCode()) {
  81.                         $this->sendSentryMessage('[ZAPIER] Unsubscribe Zap 410 returned');
  82.                         continue;
  83.                     }
  84.                 } elseif (isset($zapierHookUrl['letter_with_ar']) && 'letter_with_ar' === array_keys($zapierHookUrl)[0]) {
  85.                     $response $this->client->request(
  86.                         'POST',
  87.                         $zapierHookUrl['letter_with_ar'],
  88.                         [
  89.                             'body' => $params,
  90.                         ]
  91.                     );
  92.                     if (410 === $response->getStatusCode()) {
  93.                         $this->sendSentryMessage('[ZAPIER] Unsubscribe Zap 410 returned');
  94.                         continue;
  95.                     }
  96.                 } elseif (isset($zapierHookUrl['parcel']) && 'parcel' === array_keys($zapierHookUrl)[0]) {
  97.                     $response $this->client->request(
  98.                         'POST',
  99.                         $zapierHookUrl['parcel'],
  100.                         [
  101.                             'body' => $params,
  102.                         ]
  103.                     );
  104.                     if (410 === $response->getStatusCode()) {
  105.                         $this->sendSentryMessage('[ZAPIER] Unsubscribe Zap 410 returned');
  106.                         continue;
  107.                     }
  108.                 } elseif (isset($zapierHookUrl['bailiff_notice']) && 'bailiff_notice' === array_keys($zapierHookUrl)[0]) {
  109.                     $response $this->client->request(
  110.                         'POST',
  111.                         $zapierHookUrl['bailiff_notice'],
  112.                         [
  113.                             'body' => $params,
  114.                         ]
  115.                     );
  116.                     if (410 === $response->getStatusCode()) {
  117.                         $this->sendSentryMessage('[ZAPIER] Unsubscribe Zap 410 returned');
  118.                         continue;
  119.                     }
  120.                 }
  121.             }
  122.         }
  123.         $this->segmentAPI->trackNewLetter($entity);
  124.         if (null !== $entity->getContentPathAWS()) {
  125.             $this->bus->dispatch(OCRIndexationMessage::createWithLetterId($entity->getId()));
  126.         }
  127.     }
  128.     public function postUpdate(LifecycleEventArgs $args)
  129.     {
  130.         $entity $args->getObject();
  131.         if (!$entity instanceof Letter) {
  132.             return;
  133.         }
  134.         $this->sendLetterToAws($entity$args);
  135.         $uow $this->em->getUnitOfWork();
  136.         $uow->computeChangeSets();
  137.         $changeset $uow->getEntityChangeSet($entity);
  138.         if (isset($changeset['status']) && LetterStatusEnum::DESTROYED === $changeset['status'][1]) {
  139.             $this->segmentAPI->trackDestroyedLetter($entity);
  140.         }
  141.         if (LetterStatusEnum::TO_BE_SENT === $entity->getStatus()) {
  142.             $entity->getOrganization()->setIsEmptybal(false);
  143.             $args->getObjectManager()->persist($args->getObjectManager()->getRepository(Organization::class)->find($entity->getOrganization()->getId()));
  144.             $args->getObjectManager()->flush();
  145.         }
  146.         if (null !== $entity->getContentPathAWS() && $organization $entity->getOrganization()) {
  147.             $this->bus->dispatch(OCRIndexationMessage::createWithOrganizationIdAndLetterId($organization->getId(), $entity->getId()));
  148.         }
  149.     }
  150.     private function sendLetterToAws(Letter $entity$args)
  151.     {
  152.         $response $this->letterFilesService->sendToAWS($entity);
  153.         if (!empty($response)) {
  154.             if (isset($response['front'])) {
  155.                 $entity->setEnvelopeFrontPath(null);
  156.                 $entity->setEnvelopeFrontPathAWS($response['front']);
  157.             }
  158.             if (isset($response['back'])) {
  159.                 $entity->setEnvelopeBackPath(null);
  160.                 $entity->setEnvelopeBackPathAWS($response['back']);
  161.             }
  162.             if (isset($response['content'])) {
  163.                 $entity->setContentPath(null);
  164.                 $entity->setContentPathAWS($response['content']);
  165.             }
  166.             $args->getObjectManager()->persist($entity);
  167.             $args->getObjectManager()->flush();
  168.         }
  169.     }
  170.     public function preRemove(LifecycleEventArgs $args)
  171.     {
  172.         $entity $args->getObject();
  173.         if (!$entity instanceof Letter) {
  174.             return;
  175.         }
  176.         $this->segmentAPI->trackDeletedLetter($entity);
  177.     }
  178. }