src/Modules/Chat/Event/Subscriber/CreateChatForContract.php line 69

Open in your IDE?
  1. <?php
  2. namespace App\Modules\Chat\Event\Subscriber;
  3. use App\Modules\Chat\DTO\ChatDTO;
  4. use App\Modules\Chat\Entity\Chat;
  5. use App\Modules\Chat\Service\ChatService;
  6. use App\Modules\Contract\Entity\Contract;
  7. use App\Modules\Contract\StateMachine\FormState;
  8. use App\Modules\Contract\Event\ContractCreated;
  9. use App\Modules\Contract\Event\FormAccepted;
  10. use App\Modules\Contract\Event\FormDeclined;
  11. use App\Modules\Contract\Event\FormFinalized;
  12. use App\Modules\Contract\Event\FormUpdated;
  13. use App\Modules\Contract\Repository\ContractRepositoryInterface;
  14. use App\Modules\Contract\Service\ContractService;
  15. use App\Modules\Contract\ValueObject\FormValueObject;
  16. use App\Modules\User\Service\RepresentativesService;
  17. use Doctrine\Common\Collections\ArrayCollection;
  18. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  19. class CreateChatForContract implements EventSubscriberInterface
  20. {
  21.     /**
  22.      * @var RepresentativesService
  23.      */
  24.     private $representativesService;
  25.     /**
  26.      * @var ChatService
  27.      */
  28.     private $chatService;
  29.     /**
  30.      * @var ContractRepositoryInterface
  31.      */
  32.     private $contractRepository;
  33.     public function __construct(RepresentativesService $representativesServiceChatService $chatServiceContractRepositoryInterface $contractRepository)
  34.     {
  35.         $this->representativesService $representativesService;
  36.         $this->chatService $chatService;
  37.         $this->contractRepository $contractRepository;
  38.     }
  39.     /**
  40.      * Returns an array of event names this subscriber wants to listen to.
  41.      *
  42.      * The array keys are event names and the value can be:
  43.      *
  44.      *  * The method name to call (priority defaults to 0)
  45.      *  * An array composed of the method name to call and the priority
  46.      *  * An array of arrays composed of the method names to call and respective
  47.      *    priorities, or 0 if unset
  48.      *
  49.      * For instance:
  50.      *
  51.      *  * ['eventName' => 'methodName']
  52.      *  * ['eventName' => ['methodName', $priority]]
  53.      *  * ['eventName' => [['methodName1', $priority], ['methodName2']]]
  54.      *
  55.      * @return array The event names to listen to
  56.      */
  57.     public static function getSubscribedEvents()
  58.     {
  59.         return [
  60.             ContractCreated::class => 'onContractCreated'
  61.         ];
  62.     }
  63.     public function onContractCreated(ContractCreated $event)
  64.     {
  65.         $contract $event->getContract();
  66.         $chat $this->createChat($contract);
  67.         $this->addChatToContract($contract$chat);
  68.     }
  69.     private function createChat(Contract $contract): Chat
  70.     {
  71.         $initiator $contract->getInitiator();
  72.         $executor $contract->getExecutor();
  73.         $initiatorRepresentatives $this->representativesService->get($initiator->getId(), $initiator->getType());
  74.         $executorRepresentatives $this->representativesService->get($executor->getId(), $executor->getType());
  75.         $participants = new ArrayCollection();
  76.         foreach ($initiatorRepresentatives as $representative) {
  77.             $participants->add($representative);
  78.         }
  79.         foreach ($executorRepresentatives as $representative) {
  80.             $participants->add($representative);
  81.         }
  82.         $dto = new ChatDTO($participants);
  83.         $chat $this->chatService->create($dto);
  84.         return $chat;
  85.     }
  86.     private function addChatToContract(Contract $contractChat $chat)
  87.     {
  88.         $contract->setChat($chat);
  89.         $this->contractRepository->save($contract);
  90.     }
  91. }