src/Modules/Chat/Event/Subscriber/UpdateContractChatParticipants.php line 67

Open in your IDE?
  1. <?php
  2. namespace App\Modules\Chat\Event\Subscriber;
  3. use App\Modules\Chat\Event\MessageSent;
  4. use App\Modules\Chat\Repository\ChatRepositoryInterface;
  5. use App\Modules\Contract\Repository\ContractRepositoryInterface;
  6. use App\Modules\Contract\ValueObject\Participant;
  7. use App\Modules\User\Event\AdministratorAddedToCircleMembers;
  8. use App\Modules\User\Event\CircleMemberUpdated;
  9. use App\Modules\User\Event\PartnerUpdated;
  10. use App\Modules\User\Service\RepresentativesService;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. class UpdateContractChatParticipants implements EventSubscriberInterface
  13. {
  14.     /**
  15.      * @var ChatRepositoryInterface
  16.      */
  17.     private $chatRepository;
  18.     /**
  19.      * @var ContractRepositoryInterface
  20.      */
  21.     private $contractRepository;
  22.     /**
  23.      * @var RepresentativesService
  24.      */
  25.     private $representativesService;
  26.     public function __construct(
  27.         ChatRepositoryInterface $chatRepository,
  28.         ContractRepositoryInterface $contractRepository,
  29.         RepresentativesService $representativesService
  30.     )
  31.     {
  32.         $this->chatRepository $chatRepository;
  33.         $this->contractRepository $contractRepository;
  34.         $this->representativesService $representativesService;
  35.     }
  36.     /**
  37.      * Returns an array of event names this subscriber wants to listen to.
  38.      *
  39.      * The array keys are event names and the value can be:
  40.      *
  41.      *  * The method name to call (priority defaults to 0)
  42.      *  * An array composed of the method name to call and the priority
  43.      *  * An array of arrays composed of the method names to call and respective
  44.      *    priorities, or 0 if unset
  45.      *
  46.      * For instance:
  47.      *
  48.      *  * ['eventName' => 'methodName']
  49.      *  * ['eventName' => ['methodName', $priority]]
  50.      *  * ['eventName' => [['methodName1', $priority], ['methodName2']]]
  51.      *
  52.      * @return array The event names to listen to
  53.      */
  54.     public static function getSubscribedEvents()
  55.     {
  56.         return [
  57.             PartnerUpdated::class => 'onPartnerUpdated',
  58.             CircleMemberUpdated::class => 'onCircleMemberUpdated',
  59.             AdministratorAddedToCircleMembers::class => 'onAdministratorAddedToCircleMember'
  60.         ];
  61.     }
  62.     public function onPartnerUpdated(PartnerUpdated $event)
  63.     {
  64.         $partner $event->getPartner();
  65.         $participants[] = [
  66.             'id' => $partner->getId(),
  67.             'type' => Participant::PARTNER
  68.         ];
  69.         $this->updateParticipantsContractsChats($participants);
  70.     }
  71.     public function onCircleMemberUpdated(CircleMemberUpdated $event)
  72.     {
  73.         $circleMember $event->getCircleMember();
  74.         $participants[] = [
  75.             'id' => $circleMember->getId(),
  76.             'type' => Participant::CIRCLE_MEMBER
  77.         ];
  78.         $this->updateParticipantsContractsChats($participants);
  79.     }
  80.     public function onAdministratorAddedToCircleMember(AdministratorAddedToCircleMembers $event)
  81.     {
  82.         $circleMembersIds $event->getCircleMembersIds();
  83.         $participants = [];
  84.         foreach ($circleMembersIds as $circleMemberId) {
  85.             $participants[] = [
  86.                 'id' => $circleMemberId,
  87.                 'type' => Participant::CIRCLE_MEMBER
  88.             ];
  89.         }
  90.         $this->updateParticipantsContractsChats($participants);
  91.     }
  92.     private function updateParticipantsContractsChats(array $participants)
  93.     {
  94.         $contractsIds $this->contractRepository->findIdsByParticipants($participants);
  95.         if (empty($contractsIds)) {
  96.             return;
  97.         }
  98.         $chats $this->chatRepository->findByContracts($contractsIds);
  99.         foreach ($chats as $chat) {
  100.             if (!is_null($contract $chat->getContract())) {
  101.                 $initiator $contract->getInitiator();
  102.                 $executor $contract->getExecutor();
  103.                 $initiatorRepresentatives $this->representativesService->get($initiator->getId(), $initiator->getType());
  104.                 $executorRepresentatives $this->representativesService->get($executor->getId(), $executor->getType());
  105.                 $chat->removeParticipants();
  106.                 $chat->addParticipant(... $initiatorRepresentatives);
  107.                 $chat->addParticipant(... $executorRepresentatives);
  108.                 $this->chatRepository->save($chat);
  109.             }
  110.         }
  111.     }
  112. }