NexmoBroadcastInboundCallCommand.php 2.26 KB
<?php

namespace NexmoBundle\Command;

use AppBundle\Entity\AdminUser;
use AppBundle\Entity\OpentokSessionTypes;
use AppBundle\Entity\Patient;
use AppBundle\Entity\VoiceCall;
use Doctrine\Bundle\DoctrineBundle\Registry;
use Monolog\Logger;
use NexmoBundle\Manager\InboundCallManager;
use OpenTokBundle\Pool\GlobalAdminOpenTokPool;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class NexmoBroadcastInboundCallCommand extends ContainerAwareCommand
{
    /**
     * @var Logger
     */
    private $logger;

    protected function configure()
    {
        $this
            ->setName('nexmo:broadcast-inbound-call')
            ->setDescription('Broadcast inbound voice call')
            ->addArgument('voice_call', InputArgument::REQUIRED, 'Voice call id')
            ->addOption("cobrand", "c", InputOption::VALUE_OPTIONAL, 'Cobrand code of executing context')
        ;
    }

    private function logString($s)
    {
        return "[{$this->getName()}]: {$s}";
    }

    /**
     * @param InputInterface  $input
     * @param OutputInterface $output
     * @return int|void|null
     * @throws \Exception
     */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $this->logger = $this->getContainer()->get("logger");

        /** @var InboundCallManager $manager */
        $manager = $this->getContainer()->get("nexmo.manager.inbound_call");
        /** @var VoiceCall $voiceCall */
        $voiceCall = $manager->getObjectOrHalt($input->getArgument('voice_call'));
        $manager->initThreadContent($voiceCall);
        $opentokData = $manager->asOpenTokSignalData($voiceCall);

        if ($voiceCall->getRecipient() instanceof AdminUser) {
            /** @var GlobalAdminOpenTokPool $pool */
            $pool = $this->getContainer()->get("opentok_pool_factory")->get(OpentokSessionTypes::ADMIN_POOL);
            // send opentok signal to admin user
            $pool->sendSignal("inbound-call", $opentokData);
        }

        $this->logger->info($this->logString("Broadcasting voice call #{$voiceCall->getId()}"));
    }
}