NexmoCallManager.php
2.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?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;
}
}