src/App/Api/EventListener/ExemptionStatusChangeListener.php line 37

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Api\EventListener;
  4. use App\Api\Entity\Document;
  5. use App\Api\Entity\DocumentType;
  6. use App\Api\Entity\Exemption;
  7. use App\Api\Enum\ExemptionStatus;
  8. use App\Api\Event\ExemptionUpdateEvent;
  9. use App\Api\Messenger\Message\Email\ExemptionToValidateToEmployee;
  10. use App\Api\Messenger\Message\Email\ExemptionToValidateToScope;
  11. use App\Api\Messenger\MessageHandler\Email\ExemptionToValidateToEmployeeHandler;
  12. use App\Api\Messenger\MessageHandler\Email\ExemptionToValidateToScopeHandler;
  13. use Symfony\Component\Messenger\MessageBusInterface;
  14. class ExemptionStatusChangeListener
  15. {
  16.     private $messageBus;
  17.     private $exemptionToValidateToEmployeeHandler;
  18.     private $exemptionToValidateToScopeHandler;
  19.     private $isRabbitMqAvailable;
  20.     public function __construct(
  21.         MessageBusInterface $messageBus,
  22.         ExemptionToValidateToEmployeeHandler $exemptionToValidateToEmployeeHandler,
  23.         ExemptionToValidateToScopeHandler $exemptionToValidateToScopeHandler,
  24.         bool $isRabbitMqAvailable
  25.     ) {
  26.         $this->messageBus $messageBus;
  27.         $this->exemptionToValidateToEmployeeHandler $exemptionToValidateToEmployeeHandler;
  28.         $this->exemptionToValidateToScopeHandler $exemptionToValidateToScopeHandler;
  29.         $this->isRabbitMqAvailable $isRabbitMqAvailable;
  30.     }
  31.     public function handle(ExemptionUpdateEvent $exemptionUpdateEvent)
  32.     {
  33.         $exemption $exemptionUpdateEvent->getExemption();
  34.         if (ExemptionStatus::TO_FINALISE()->equals($exemption->getStatus())) {
  35.             if ($this->isReadyToValidate($exemption)) {
  36.                 $exemption->setStatus(ExemptionStatus::TO_VALIDATE());
  37.                 // because platform sometimes has problem with RabbitMQ container
  38.                 $exemptionToValidateToEmployee = new ExemptionToValidateToEmployee($exemption->getId());
  39.                 $exemptionToValidateToScope = new ExemptionToValidateToScope($exemption->getId(), $exemption->getEmail());
  40.                 if ($this->isRabbitMqAvailable) {
  41.                     $this->messageBus->dispatch($exemptionToValidateToEmployee);
  42.                     $this->messageBus->dispatch($exemptionToValidateToScope);
  43.                 } else {
  44.                     $this->exemptionToValidateToEmployeeHandler->handle($exemptionToValidateToEmployee);
  45.                     $this->exemptionToValidateToScopeHandler->handle($exemptionToValidateToScope);
  46.                 }
  47.                 // because platform sometimes has problem with RabbitMQ container
  48.             }
  49.         }
  50.         if (ExemptionStatus::TO_RENEW()->equals($exemption->getStatus())) {
  51.             $exemption->setStatus(ExemptionStatus::TO_FINALISE());
  52.         }
  53.     }
  54.     public function isReadyToValidate(Exemption $entity): bool
  55.     {
  56.         $exemptionReason $entity->getExemptionReason();
  57.         if (!$entity->hasDocument() || !$exemptionReason) {
  58.             return false;
  59.         }
  60.         $existingTypes = [];
  61.         /** @var Document $document */
  62.         foreach ($exemptionReason->getDocuments() as $document) {
  63.             $existingTypes[] = $document->getTypeCode()->getValue();
  64.         }
  65.         $supportedDocumentTypes $exemptionReason->getReason()->getDocumentTypes();
  66.         /** @var DocumentType $type */
  67.         foreach ($supportedDocumentTypes as $type) {
  68.             if (!in_array($type->getCode()->getValue(), $existingTypes)) {
  69.                 return false;
  70.             }
  71.         }
  72.         return true;
  73.     }
  74. }