<?php
namespace App\Modules\Chat\Event\Subscriber;
use App\Modules\Chat\DTO\ChatDTO;
use App\Modules\Chat\Entity\Chat;
use App\Modules\Chat\Service\ChatService;
use App\Modules\Contract\Entity\Contract;
use App\Modules\Contract\StateMachine\FormState;
use App\Modules\Contract\Event\ContractCreated;
use App\Modules\Contract\Event\FormAccepted;
use App\Modules\Contract\Event\FormDeclined;
use App\Modules\Contract\Event\FormFinalized;
use App\Modules\Contract\Event\FormUpdated;
use App\Modules\Contract\Repository\ContractRepositoryInterface;
use App\Modules\Contract\Service\ContractService;
use App\Modules\Contract\ValueObject\FormValueObject;
use App\Modules\User\Service\RepresentativesService;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class CreateChatForContract implements EventSubscriberInterface
{
/**
* @var RepresentativesService
*/
private $representativesService;
/**
* @var ChatService
*/
private $chatService;
/**
* @var ContractRepositoryInterface
*/
private $contractRepository;
public function __construct(RepresentativesService $representativesService, ChatService $chatService, ContractRepositoryInterface $contractRepository)
{
$this->representativesService = $representativesService;
$this->chatService = $chatService;
$this->contractRepository = $contractRepository;
}
/**
* Returns an array of event names this subscriber wants to listen to.
*
* The array keys are event names and the value can be:
*
* * The method name to call (priority defaults to 0)
* * An array composed of the method name to call and the priority
* * An array of arrays composed of the method names to call and respective
* priorities, or 0 if unset
*
* For instance:
*
* * ['eventName' => 'methodName']
* * ['eventName' => ['methodName', $priority]]
* * ['eventName' => [['methodName1', $priority], ['methodName2']]]
*
* @return array The event names to listen to
*/
public static function getSubscribedEvents()
{
return [
ContractCreated::class => 'onContractCreated'
];
}
public function onContractCreated(ContractCreated $event)
{
$contract = $event->getContract();
$chat = $this->createChat($contract);
$this->addChatToContract($contract, $chat);
}
private function createChat(Contract $contract): Chat
{
$initiator = $contract->getInitiator();
$executor = $contract->getExecutor();
$initiatorRepresentatives = $this->representativesService->get($initiator->getId(), $initiator->getType());
$executorRepresentatives = $this->representativesService->get($executor->getId(), $executor->getType());
$participants = new ArrayCollection();
foreach ($initiatorRepresentatives as $representative) {
$participants->add($representative);
}
foreach ($executorRepresentatives as $representative) {
$participants->add($representative);
}
$dto = new ChatDTO($participants);
$chat = $this->chatService->create($dto);
return $chat;
}
private function addChatToContract(Contract $contract, Chat $chat)
{
$contract->setChat($chat);
$this->contractRepository->save($contract);
}
}