<?php
namespace App\Modules\Chat\Event\Subscriber;
use App\Modules\Chat\Event\MessageSent;
use App\Modules\Chat\Repository\ChatRepositoryInterface;
use App\Modules\Contract\Repository\ContractRepositoryInterface;
use App\Modules\Contract\ValueObject\Participant;
use App\Modules\User\Event\AdministratorAddedToCircleMembers;
use App\Modules\User\Event\CircleMemberUpdated;
use App\Modules\User\Event\PartnerUpdated;
use App\Modules\User\Service\RepresentativesService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class UpdateContractChatParticipants implements EventSubscriberInterface
{
/**
* @var ChatRepositoryInterface
*/
private $chatRepository;
/**
* @var ContractRepositoryInterface
*/
private $contractRepository;
/**
* @var RepresentativesService
*/
private $representativesService;
public function __construct(
ChatRepositoryInterface $chatRepository,
ContractRepositoryInterface $contractRepository,
RepresentativesService $representativesService
)
{
$this->chatRepository = $chatRepository;
$this->contractRepository = $contractRepository;
$this->representativesService = $representativesService;
}
/**
* 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 [
PartnerUpdated::class => 'onPartnerUpdated',
CircleMemberUpdated::class => 'onCircleMemberUpdated',
AdministratorAddedToCircleMembers::class => 'onAdministratorAddedToCircleMember'
];
}
public function onPartnerUpdated(PartnerUpdated $event)
{
$partner = $event->getPartner();
$participants[] = [
'id' => $partner->getId(),
'type' => Participant::PARTNER
];
$this->updateParticipantsContractsChats($participants);
}
public function onCircleMemberUpdated(CircleMemberUpdated $event)
{
$circleMember = $event->getCircleMember();
$participants[] = [
'id' => $circleMember->getId(),
'type' => Participant::CIRCLE_MEMBER
];
$this->updateParticipantsContractsChats($participants);
}
public function onAdministratorAddedToCircleMember(AdministratorAddedToCircleMembers $event)
{
$circleMembersIds = $event->getCircleMembersIds();
$participants = [];
foreach ($circleMembersIds as $circleMemberId) {
$participants[] = [
'id' => $circleMemberId,
'type' => Participant::CIRCLE_MEMBER
];
}
$this->updateParticipantsContractsChats($participants);
}
private function updateParticipantsContractsChats(array $participants)
{
$contractsIds = $this->contractRepository->findIdsByParticipants($participants);
if (empty($contractsIds)) {
return;
}
$chats = $this->chatRepository->findByContracts($contractsIds);
foreach ($chats as $chat) {
if (!is_null($contract = $chat->getContract())) {
$initiator = $contract->getInitiator();
$executor = $contract->getExecutor();
$initiatorRepresentatives = $this->representativesService->get($initiator->getId(), $initiator->getType());
$executorRepresentatives = $this->representativesService->get($executor->getId(), $executor->getType());
$chat->removeParticipants();
$chat->addParticipant(... $initiatorRepresentatives);
$chat->addParticipant(... $executorRepresentatives);
$this->chatRepository->save($chat);
}
}
}
}