ThreadContentVoiceCallEventSubscriber.php 1.86 KB
<?php

namespace NexmoBundle\Event\Subscriber;

use AppBundle\Entity\ThreadContent;
use AppBundle\Entity\ThreadContentTypes;
use AppBundle\Entity\VoiceCall;
use AppBundle\Event\VoiceCallEvent;
use AppBundle\Event\VoiceCallEvents;
use AppBundle\Service\OpentokSignalingService;
use Doctrine\Bundle\DoctrineBundle\Registry;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
 * Class ThreadContentVoiceCallEventSubscriber
 *
 */
class ThreadContentVoiceCallEventSubscriber implements EventSubscriberInterface
{
    /**
     * @var Registry
     */
    protected $doctrine;

    /**
     * @var OpentokSignalingService
     */
    protected $opentokSignaling;

    public function setDoctrine(Registry $v)
    {
        $this->doctrine = $v;
    }

    public function setOpentokSignaling(OpentokSignalingService $v)
    {
        $this->opentokSignaling = $v;
    }

    public function onStatusChange(VoiceCallEvent $event)
    {
        if (!$event->getVoiceCall() instanceof VoiceCall) {
            return;
        }

        /** @var ThreadContent $threadContent */
        $threadContent = $this->doctrine->getRepository("AppBundle:ThreadContent")
            ->findOneBy(["contentId" => $event->getVoiceCall()->getId(), "type" => ThreadContentTypes::VOICE_CALL_LOG]);

        if (!$threadContent instanceof ThreadContent) {
            return;
        }


        $this->opentokSignaling->sendSignal($threadContent->getThread(), "voice_call_update_content", ["thread_content" => $threadContent->getId()]);
    }

    public static function getSubscribedEvents()
    {
        return [
            VoiceCallEvents::STARTED_VOICE_CALL => 'onStatusChange',
            VoiceCallEvents::ANSWERED_VOICE_CALL => 'onStatusChange',
            VoiceCallEvents::COMPLETED_VOICE_CALL => 'onStatusChange',
            VoiceCallEvents::FAILED_VOICE_CALL => 'onStatusChange'
        ];
    }
}