src/EventSubscriber/PropertySubscriber.php line 115

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use ApiPlatform\Core\EventListener\EventPriorities;
  4. use App\Entity\AbstractPeople;
  5. use App\Entity\Property;
  6. use App\Entity\Reference\ReferencePropertyDomain;
  7. use App\Entity\Reference\ReferencePropertyServiceType;
  8. use App\Entity\Reference\ReferencePropertyStatus;
  9. use App\Manager\Property\AssignPropertyRecommendationInterface;
  10. use App\Manager\Property\PropertyReferenceGeneratorInterface;
  11. use App\Message\GenerateBusinessIndicationMandateMessage;
  12. use Doctrine\ORM\EntityManagerInterface;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\HttpFoundation\Response;
  16. use Symfony\Component\HttpKernel\Event\ViewEvent;
  17. use Symfony\Component\HttpKernel\KernelEvents;
  18. use Symfony\Component\HttpKernel\Event\TerminateEvent;
  19. use Symfony\Component\Messenger\MessageBusInterface;
  20. use Psr\Log\LoggerInterface;
  21. class PropertySubscriber implements EventSubscriberInterface
  22. {
  23.     public const BUSINESS_INDICATION_ALLOWED_METHODS = [
  24.      Request::METHOD_POST,
  25.      Request::METHOD_PUT,
  26.      Request::METHOD_PATCH
  27.     ];
  28.     public const BUSINESS_INDICATION_ALLOWED_STATUS_CODE = [
  29.         Response::HTTP_CREATED
  30.     ];
  31.     private EntityManagerInterface $entityManager;
  32.     private PropertyReferenceGeneratorInterface $referenceGenerator;
  33.     private AssignPropertyRecommendationInterface $assignPropertyRecommendation;
  34.     private MessageBusInterface $bus;
  35.     private LoggerInterface $logger;
  36.     public function __construct(
  37.         EntityManagerInterface $entityManager,
  38.         PropertyReferenceGeneratorInterface $referenceGenerator,
  39.         AssignPropertyRecommendationInterface $assignPropertyRecommendation,
  40.         MessageBusInterface $bus,
  41.         LoggerInterface $logger
  42.     ) {
  43.         $this->entityManager $entityManager;
  44.         $this->referenceGenerator $referenceGenerator;
  45.         $this->assignPropertyRecommendation $assignPropertyRecommendation;
  46.         $this->bus $bus;
  47.         $this->logger $logger;
  48.     }
  49.     /**
  50.      * @param ViewEvent $event
  51.      */
  52.     public function postUpdate(ViewEvent $event): void
  53.     {
  54.         $property $event->getControllerResult();
  55.         $method $event->getRequest()->getMethod();
  56.         if (!$property instanceof Property || Request::METHOD_PUT !== $method) {
  57.             return;
  58.         } elseif (
  59.             $property->getReferencePropertyStatus()->getName() ===
  60.             ReferencePropertyStatus::REFERENCE_PROPERTY_STATUS_INVALIDE
  61.         ) {
  62.             $this->assignPropertyRecommendation->assignRecommendation($property);
  63.             $this->entityManager->flush();
  64.         }
  65.         if ($property->getVMandate()) {
  66.             $mandate $property->getVMandate()[0];
  67.             $mandate->setMaximumPrice($property->getMinimalPrice());
  68.             $this->entityManager->flush();
  69.         }
  70.         $hasUpdatedArea false;
  71.         foreach ($property->getMandates() as $mandate) {
  72.             if ($mandate->getMinimalArea() !== $property->getMinimalArea()) {
  73.                 $hasUpdatedArea true;
  74.                 $mandate->setMinimalArea($property->getMinimalArea());
  75.                 $this->entityManager->persist($mandate);
  76.             }
  77.             if ($mandate->getMaximalArea() !== $property->getMaximalArea()) {
  78.                 $hasUpdatedArea true;
  79.                 $mandate->setMaximalArea($property->getMaximalArea());
  80.                 $this->entityManager->persist($mandate);
  81.             }
  82.         }
  83.         if ($hasUpdatedArea) {
  84.             $this->entityManager->flush();
  85.         }
  86.     }
  87.     public function generateKey($event): void
  88.     {
  89.         $property $event->getControllerResult();
  90.         $method $event->getRequest()->getMethod();
  91.         if (!$property instanceof Property || Request::METHOD_POST !== $method) {
  92.             return;
  93.         }
  94.         $this->referenceGenerator->generate($property);
  95.     }
  96.     public function handleBusinessIndicationProcess(TerminateEvent $event): void
  97.     {
  98.         $entity $event->getRequest()->attributes->get('_api_resource_class');
  99.         $method $event->getRequest()->getMethod();
  100.         $responseStatus $event->getResponse()->getStatusCode();
  101.         if (
  102.             $entity === Property::class
  103.             && in_array($methodself::BUSINESS_INDICATION_ALLOWED_METHODStrue)
  104.             && in_array($responseStatusself::BUSINESS_INDICATION_ALLOWED_STATUS_CODEtrue)
  105.         ) {
  106.             $responseContent json_decode($event->getResponse()->getContent(), true);
  107.             if ($this->isBusinessIndicationProcess($responseContent)) {
  108.                 try {
  109.                     $message = new GenerateBusinessIndicationMandateMessage((int) $responseContent['id']);
  110.                     $this->bus->dispatch($message);
  111.                 } catch (\Throwable $exception) {
  112.                     $this->logger->error(
  113.                         'Failed to publish GeneratBusinessIndicationMandateMessage !',
  114.                         [
  115.                             'trace' => $exception->getTrace(),
  116.                             'line' => $exception->getLine(),
  117.                             'message' => $exception->getMessage(),
  118.                             'file' => $exception->getFile(),
  119.                         ]
  120.                     );
  121.                 }
  122.             }
  123.         }
  124.     }
  125.     /**
  126.      * @param array $propertyResponse
  127.      *
  128.      * @return bool
  129.      */
  130.     private function isBusinessIndicationProcess(array $propertyResponse): bool
  131.     {
  132.         return
  133.             $propertyResponse['referencePropertyDomain']['code'] ===
  134.             ReferencePropertyDomain::REFERENCE_CODE_PROPERTY_DOMAIN_NEW
  135.             && $propertyResponse['referenceServiceType']['code'] ===
  136.             ReferencePropertyServiceType::REFERENCE_CODE_PROPERTY_SERVICE_TYPE_SELL
  137.             && $propertyResponse['contact']['personType'] === AbstractPeople::PERSON_TYPE_MORAL
  138.             && $propertyResponse['contact']['isBusinessIndication'];
  139.     }
  140.     public static function getSubscribedEvents(): array
  141.     {
  142.         return [
  143.             KernelEvents::VIEW => [
  144.                 ['postUpdate'EventPriorities::POST_WRITE],
  145.                 ['generateKey'EventPriorities::POST_WRITE],
  146.             ],
  147.             KernelEvents::TERMINATE => [['handleBusinessIndicationProcess'0]]
  148.         ];
  149.     }
  150. }