-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSender.php
140 lines (120 loc) · 3.88 KB
/
Sender.php
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
<?php
namespace San\EmailBundle;
use Doctrine\Common\Persistence\ObjectManager;
use Exercise\Sendgrid\Common\Sendgrid;
use San\EmailBundle\Model\Email;
use San\EmailBundle\Model\EmailSend;
use San\UserListBundle\Admin\UserListAdmin;
use Symfony\Component\HttpKernel\Event\PostResponseEvent;
class Sender
{
/**
* @var \Exercose\Sendgrid\Marketing\MarketingClient
*/
protected $marketing;
/**
* @var \Doctrine\Common\Persistence\ObjectManager
*/
protected $om;
/**
* @var \San\UserListBundle\Admin\UserListAdmin
*/
protected $userListAdmin;
/**
* @param Sendgrid $sendgrid
*/
public function __construct(Sendgrid $sendgrid, ObjectManager $om, UserListAdmin $userListAdmin)
{
$this->marketing = $sendgrid->get('marketing');
$this->om = $om;
$this->userListAdmin = $userListAdmin;
}
/**
* @param PostResponseEvent $event
* @return boolean
*/
public function onKernelTerminate(PostResponseEvent $event)
{
$emailSend = $this->getEmailSendRepository()->findOldestNonProcessed();
if (!$emailSend) {
return;
}
$listCreated = $this->createList($emailSend);
if (!$listCreated->isSuccessful()) {
return false;
}
$emailSend->setIsProcessing(true);
$this->om->flush($emailSend);
$receivers = array();
if ($emailSend->getIsTest()) {
$receivers = array_combine($emailSend->getTestEmails(), $emailSend->getTestEmails());
} else {
foreach ($emailSend->getUserLists() as $userList) {
$listUsers = $this->userListAdmin->getUsers($userList);
foreach ($listUsers as $value) {
$receivers[$value->getEmail()] = $value->getUsername();
}
}
}
$batches = (int) (count($receivers) / 1000) + 1;
for ($i = 0; $i < $batches; $i++) {
$this->addEmailsToList($emailSend, array_slice($receivers, $i * 1000, 1000));
}
$this->createEmail($emailSend);
$emailSend
->setIsProcessing(false)
->setHasProcessed(true)
;
$this->om->flush($emailSend);
return true;
}
/**
* @param EmailSend $emailSend
* @return \Guzzle\Http\Message\Response
*/
protected function createEmail(EmailSend $emailSend)
{
$parameters = array(
'identity' => $emailSend->getSender()->getDescription(),
'name' => $emailSend->getSendgridEmailName(),
'subject' => $emailSend->getSubject(),
'text' => $emailSend->getText(),
);
if ($emailSend->getIsHtmlContent()) {
$parameters['html'] = $emailSend->getEmail()->getHtml();
}
return $this->marketing->addEmail($parameters);
}
/**
* @param EmailSend $email
* @return \Guzzle\Http\Message\Response
*/
protected function createList(EmailSend $emailSend)
{
return $this->marketing->addList(array(
'list' => $emailSend->getSendgridListName()
));
}
/**
* @param EmailSend $emailSend
* @return \Guzzle\Http\Message\Response
*/
protected function addEmailsToList(EmailSend $emailSend, array $emails)
{
$preparedEmails = array();
foreach ($emails as $email => $name) {
$preparedEmails[] = json_encode(array('email' => $email, 'name' => $name));
}
return $this->marketing->addEmailsToList(array(
'list' => $emailSend->getSendgridListName(),
'data' => $preparedEmails,
));
}
/**
* @return \San\EmailBundle\Entity\EmailSendRepository|\San\EmailBundle\Document\EmailSendRepository
*/
protected function getEmailSendRepository()
{
return $this->om->getRepository('SanEmailBundle:EmailSend');
}
}