NexmoCallManager.php 2.95 KB
<?php
namespace NexmoBundle\Manager;

use AppBundle\Entity\VoiceCall;
use AppBundle\Repository\VoiceCallRepository;
use AppBundle\Service\Manager\BaseManager;
use Doctrine\Bundle\DoctrineBundle\Registry;
use Doctrine\ORM\EntityManager;
use JMS\DiExtraBundle\Annotation\Inject;
use JMS\DiExtraBundle\Annotation\InjectParams;
use JMS\DiExtraBundle\Annotation\Service;
use Sentry\SentryBundle\SentrySymfonyClient;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

/**
 * Class NexmoCallManager
 * @Service("nexmo.base_manager", autowire=true, public=true)
 */
abstract class NexmoCallManager extends BaseManager
{
    /**
     * @var VoiceCallRepository
     */
    protected $repository;

    /**
     * @var SentrySymfonyClient
     */
    protected $sentryClient;

    /**
     * @param VoiceCallRepository $voiceCallRepository
     */
    public function __construct(VoiceCallRepository $voiceCallRepository)
    {
        $this->repository = $voiceCallRepository;
    }

    /**
     * @param SentrySymfonyClient $client
     * @InjectParams({
     *     "client" = @Inject("sentry.client", required=false)
     * })
     */
    public function setSentryClient(SentrySymfonyClient $client)
    {
        $this->sentryClient = $client;
    }

    /**
     * Common query function for all Nexmo related voice call logs since most interact via uuid.
     * @param $uuid
     * @return VoiceCall
     */
    public function findOneByUUID($uuid)
    {
        return $this->repository->findOneBy(["uuid" => $uuid]);
    }

    /**
     * @param string $uuid
     * @return VoiceCall
     */
    public function findOneByUuidOrHalt($uuid)
    {
        $obj = $this->findOneByUUID($uuid);
        if (!$obj) {
            throw new NotFoundHttpException("Invalid voice call uuid.");
        }

        return $obj;
    }

    abstract protected function getUpdateMappers(VoiceCall $voiceCall);

    /**
     * @param VoiceCall $voiceCall
     * @param array     $params
     * @throws \Exception
     */
    final public function patch(VoiceCall $voiceCall, array $params = [])
    {
        try {
            $this->beginTransaction();
            $mappers = $this->getUpdateMappers($voiceCall);

            foreach ($params as $key => $value) {
                if (isset($mappers[$key]) && !is_null($value)) {
                    $mappers[$key]($value);
                }
            }

            $this->save($voiceCall);
            $this->commit();
        } catch (\Exception $exception) {
            $this->rollback();
            $this->sentryClient->captureException($exception);
            throw new BadRequestHttpException("Failed to apply patch changes for VoiceCall", $exception);
        }
    }

    /**
     * @return VoiceCall
     * @throws \Exception
     */
    public function create()
    {
        $voiceCall = new VoiceCall();
        $voiceCall->setCreatedAt(new \DateTime());

        return $voiceCall;
    }
}