InboundCallManager.php
6.06 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<?php
namespace NexmoBundle\Manager;
use AppBundle\Entity\AdminUser;
use AppBundle\Entity\OpentokSessionTypes;
use AppBundle\Entity\Patient;
use AppBundle\Entity\ThreadContent;
use AppBundle\Entity\VoiceCall;
use AppBundle\Exception\NotExistEntityException;
use AppBundle\Service\Manager\ContactNumberManager;
use AppBundle\Service\Manager\PatientManager;
use AppBundle\Service\Manager\ThreadNoteManager;
use AppBundle\Service\ThreadContentFactory;
use JMS\DiExtraBundle\Annotation\Inject;
use JMS\DiExtraBundle\Annotation\InjectParams;
use JMS\DiExtraBundle\Annotation\Service;
use NexmoBundle\VoiceCall\VoiceCallOrigins;
use OpenTokBundle\Pool\GlobalAdminOpenTokPool;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
/**
* Class InboundCallManager
* @Service("nexmo.manager.inbound_call", parent="nexmo.base_manager", autowire=true, public=true)
*/
class InboundCallManager extends NexmoCallManager
{
/**
* @var ThreadContentFactory
*/
private $threadContentFactory;
/**
* @var PatientManager
*/
private $patientManager;
/**
* @var ThreadNoteManager
*/
private $threadNoteManager;
/**
* @var ContactNumberManager
*/
private $contactNumberManager;
/**
* Setter Injector for PatientManager
* @param $patientManager
* @InjectParams({
* "patientManager" = @Inject("spoke.manager.patient", required=false)
* })
*/
public function setPatientManager(PatientManager $patientManager)
{
$this->patientManager = $patientManager;
}
/**
* Setter Injector for ThreadContentFactory
* @param $threadContentFactory
* @InjectParams({
* "threadContentFactory" = @Inject("app.service.thread_content_factory", required=true)
* })
*/
public function setThreadContentFactory(ThreadContentFactory $threadContentFactory)
{
$this->threadContentFactory = $threadContentFactory;
}
/**
* Setter injector for ThreadNoteManager
* @param ThreadNoteManager $manager
* @InjectParams({
* "manager" = @Inject("spoke.manager.thread_note", required=true)
* })
*/
public function setThreadNoteManager(ThreadNoteManager $manager)
{
$this->threadNoteManager = $manager;
}
/**
* Setter injector for ContactNumberManager
* @param ContactNumberManager $contactNumberManager
* @InjectParams({
* "contactNumberManager" = @Inject("spoke.manager.contactNumber", required=true)
* })
*/
public function setContactNumberManager(ContactNumberManager $contactNumberManager)
{
$this->contactNumberManager = $contactNumberManager;
}
public function create()
{
$voiceCall = parent::create();
$voiceCall->setOriginType(VoiceCallOrigins::MEMBER_USER_INBOUND_CALL);
return $voiceCall;
}
/**
* Set up thread content for this voice call
* @param VoiceCall $voiceCall
* @return null
* @throws \Exception
*/
public function initThreadContent(VoiceCall $voiceCall)
{
// already created thread content
if ($voiceCall->getThreadContent()) {
return null;
}
$patient = $voiceCall->getActor();
if ($patient instanceof Patient) {
$voiceCall->setThreadContent($this->threadContentFactory->createContent($patient->getThread(), $voiceCall, $patient));
}
}
public function asOpenTokSignalData(VoiceCall $voiceCall)
{
$actorData = [
"number" => $voiceCall->getFromNumber()
];
$patient = $voiceCall->getActor();
if ($patient instanceof Patient) {
$actorData = array_merge($actorData, [
"id" => $patient ? $patient->getId() : 0,
"name" => $patient->getProfile()->getFullName(),
"company" => $patient->getCompany()->getName()
]);
}
$now = new \DateTime('now');
return [
"voiceCall" => $voiceCall->getId(),
"recipient" => $voiceCall->getRecipient() ? $voiceCall->getRecipient()->getId() : null,
"actor" => $actorData,
"isCallOngoing" => $voiceCall->getIsCallOngoing(),
"duration" => $voiceCall->getDuration(),
"runningDuration" => $now->getTimestamp()-$voiceCall->getCreatedAt()->getTimestamp(),
"fromNumber" => $voiceCall->getFromNumber()
];
}
protected function getUpdateMappers(VoiceCall $voiceCall)
{
return [
"actor" => function ($value) use ($voiceCall) {
$patient = $this->patientManager->getFind($value);
if (!$patient) {
throw new \Exception("Invalid patient actor for voice call.");
}
$voiceCallNumber = $voiceCall->getFromNumber();
$contactNumber = $this->contactNumberManager->create(
[
'number' => substr($voiceCallNumber, -10),
'country_number' => substr($voiceCallNumber, 0, strlen($voiceCallNumber) % 10),
'primary' => false
]
);
$this->patientManager->update($patient, ['contactNumber' => $contactNumber]);
$voiceCall->setActor($patient);
$this->initThreadContent($voiceCall);
},
// create ThreadNote for this VoiceCall
"notes" => function ($value) use ($voiceCall) {
$parentThreadContent = $voiceCall->getThreadContent();
$recipient = $voiceCall->getRecipient();
// must have existing thread content for main voice call log,
// must have a recipient, automatically set as actor for notes
if ($parentThreadContent && $recipient) {
$threadNote = $this->threadNoteManager->create($value);
$this->threadContentFactory->createContent($parentThreadContent->getThread(), $threadNote, $recipient, $parentThreadContent);
}
}
];
}
}